0

Under a Linux system, I want to get a list of directory names six directories down from the root.

From what I can tell, I can't use wildcards using os.listdir like I can by merely using an 'ls' command under Linux, but if I know that the directories I want are 'x' number of directories down, but I do not know the names of the intermediate directories, I am not sure how to do this in Python.

IE: /my/dir/is/*/*/*/here

I know the first few directory names, but there are a few where the patterns will change before I get to the level of what directory names I want, aka, "here," from my example path.

Nor can I easily predict the name of the directories I am looking to list.

I'd provide my code, but it's inside a client-controlled lab space that is not connected to the world as we know it nor can I remove content from the space. Apologies.

I am new to Python, so I am not quite sure what the correct question is that I'm looking to ask is.

Thx -Bruce

BSimmons
  • 7
  • 2
  • Hi Bruce, if you are a Linux man maybe the [tree](https://www.geeksforgeeks.org/tree-command-unixlinux/) command could help you out and avoid python possibly. To get directories 6 levels deep it would be something like `tree -d -L 6` be warned it does show all directories above also to make the "tree" – WK123 Mar 30 '21 at 23:38
  • The question was closed but here is answer `import os depth = 6 for root, dirs, files in os.walk(".", topdown=False): temp = root.split('/') if len(temp) == depth: for name in dirs: print(os.path.join(root,name))` – WK123 Mar 30 '21 at 23:56

0 Answers0