Picking a particular directory path using os path in Python

hypersonics

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

Amit Verma
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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding particular path in directory in Python

From Dev

How to check if PATH already contains a particular directory

From Dev

How to get the full path of a particular directory?

From Dev

renaming particular files in the subfolders with full directory path

From Dev

How to check if PATH already contains a particular directory

From Dev

Makefile path directory using ~/

From Dev

If a path is file or directory (Python)

From Dev

HTACCESS Not Picking Up The Path

From Dev

RegExp-filtering particular file path using directory and file extension blacklisting

From Dev

RegExp-filtering particular file path using directory and file extension blacklisting

From Dev

Python os.path.dirname returns unexpected path when changing directory

From Dev

Using os.path.dirname on windows path

From Dev

Using os.path.dirname on windows path

From Dev

Using the directory path returned from QTFile Dialogue in Python Code

From Dev

Not able to save the file in custom path directory using Python Telegram bot

From Dev

Print path of an object in current working directory using Python

From Dev

Change directory in python, relative path

From Dev

Python - set path to the directory in project

From Dev

Mac OS: how to determine path is file or directory

From Dev

correct way of using os.path.join() in python

From Dev

Python os.path.dirname and os.path.sep in Java?

From Dev

python os.path.exists() failing for nfs mounted directory file that exists

From Dev

How to find the path of webapps directory using java

From Dev

Is there a way to find directory path using regex on cmake?

From Dev

How to find the path of webapps directory using java

From Dev

specify directory or path using slash or dot slash

From Dev

Bash path variable using ~ resulting in 'No such file or directory'

From Dev

Using sed to Replace Environment Variable with directory path

From Dev

IIS path issue when using virtual directory

Related Related

  1. 1

    Finding particular path in directory in Python

  2. 2

    How to check if PATH already contains a particular directory

  3. 3

    How to get the full path of a particular directory?

  4. 4

    renaming particular files in the subfolders with full directory path

  5. 5

    How to check if PATH already contains a particular directory

  6. 6

    Makefile path directory using ~/

  7. 7

    If a path is file or directory (Python)

  8. 8

    HTACCESS Not Picking Up The Path

  9. 9

    RegExp-filtering particular file path using directory and file extension blacklisting

  10. 10

    RegExp-filtering particular file path using directory and file extension blacklisting

  11. 11

    Python os.path.dirname returns unexpected path when changing directory

  12. 12

    Using os.path.dirname on windows path

  13. 13

    Using os.path.dirname on windows path

  14. 14

    Using the directory path returned from QTFile Dialogue in Python Code

  15. 15

    Not able to save the file in custom path directory using Python Telegram bot

  16. 16

    Print path of an object in current working directory using Python

  17. 17

    Change directory in python, relative path

  18. 18

    Python - set path to the directory in project

  19. 19

    Mac OS: how to determine path is file or directory

  20. 20

    correct way of using os.path.join() in python

  21. 21

    Python os.path.dirname and os.path.sep in Java?

  22. 22

    python os.path.exists() failing for nfs mounted directory file that exists

  23. 23

    How to find the path of webapps directory using java

  24. 24

    Is there a way to find directory path using regex on cmake?

  25. 25

    How to find the path of webapps directory using java

  26. 26

    specify directory or path using slash or dot slash

  27. 27

    Bash path variable using ~ resulting in 'No such file or directory'

  28. 28

    Using sed to Replace Environment Variable with directory path

  29. 29

    IIS path issue when using virtual directory

HotTag

Archive