Suppose I have a file known as xyz in a folder as shown below:
Directory
|
|+ Subdirectory_n
|
|+ Subdirectory_(n-1)
|
|+ Subdirectory_(n-2)
|
|+ Subdirectory_(n-3)
........
|+ Subdirectory_1
|
|+ xyz
And if I want the nth subdirectory from the file, how do I obtain it python? For e.g. for the 2nd sub directory I can do something like:
import os
file = open('xyz.dat', 'w+')
print os.path.dirname(os.path.dirname(__file__))
for the third
import os
file = open('xyz.dat', 'w+')
print os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
This method looks a bit awkward. Is there a better way to do this, so that I can generalize it for an nth subdirectory in Python?