Reputation: 438
How do I get the absolute path of a directory (it has to be any directory, not the current one!) in Python?
I tried with os.path.listdir()
but it yields only the relative one.
Thanks!
Upvotes: 2
Views: 2533
Reputation: 34047
Using os.path.abspath
:
In [259]: for fname in os.listdir('.'):
...: print os.path.abspath(fname)
...:
D:\Documents\Desktop\t\old-linear
D:\Documents\Desktop\t\ZC_a0_3.xml
If you're trying to pass a path
to os.listdir
, then use os.path.join
:
In [266]: path=r'D:\Documents\Desktop\ttt'
...: for fname in os.listdir(path):
...: print os.path.join(path, fname)
D:\Documents\Desktop\ttt\old-linear
D:\Documents\Desktop\ttt\t6916A_a0_0.xml
D:\Documents\Desktop\ttt\t6916A_a0_1.xml
Upvotes: 4