Reputation: 1124
I have a root directory, which is for example ~/abc
. To get the full path of this root directory, I am using
root_dir = os.path.expanduser('~/abc')
Within, abc
, I have sub directories xyz
and bin
. To get the full paths of these, I am using
for path, dirs, files in os.walk(root_dir, topdown=False):
print path
I get the following output
/home/user/abc/xyz/bin
/home/user/abc/xyz
/home/user/abc
Now, suppose I want to extract only the full path of the bin
, how do I go about?. I am not interested in the paths of xyz
or abc
Upvotes: 0
Views: 110
Reputation: 20456
for path, dirs, files in os.walk(root_dir, topdown=False):
if 'bin' in path:
print path
Another way would be using split
:
for path, dirs, files in os.walk(root_dir, topdown=False):
if path.split('/')[-1] == 'bin':
print path
Upvotes: 2