hypersonics
hypersonics

Reputation: 1124

Picking a particular directory path using os path in Python

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

Answers (1)

Amit
Amit

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

Related Questions