0

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

1
  • how you set your root_dir and loop it to show this output should get you again to do this with /home/user/abc/xyz/bin, what's the problem really? Commented Jan 22, 2015 at 23:46

1 Answer 1

2
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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.