0

I'd like to know if it is possible to retrieve by index number a specific file in a specific subfolder inside an os.walk class.

I'd also like to know how can I list just the subdirs at a specific level inside the os.walk, for example just the subdirs of the root directory. I can list all subdirs and I can see that the list for the 1st level subdirs are grouped together first, but I can't find a way to filter just those out without using a loop...

Something like this works (adapted from another question) partially but I don't know how to filter the folders from just one level:

dirs[:] = [d for d in dirs if re.match('anyname_\d{3}', d, flags=0)]

The os.walk tuples show as 'list' when using the type command and some errors when trying to use index numbers refer to improper use of a dictionary object. I'm confused.

Thanks!

user2066480
  • 1,229
  • 2
  • 12
  • 24
  • 2
    Perhaps you are looking for simply `os.listdir('/path/to/dir')` rather than `os.walk`...? – janos Feb 13 '13 at 23:18

1 Answers1

1

It's not entirely clear what you want to do, but I can provide an example that (I think) does enough things that you can figure out how to do what you want: Print the pathname of the 3rd file in each directory named "anyname" that's exactly two levels below the top:

for dirpath, dirnames, filenames in os.walk(top):
    depth = os.path.relpath(dirpath, top).count(os.pathsep)
    if depth == 2 and os.path.basename(dirpath) == "anyname":
        print(os.path.join(dirpath, filenames[2]))

You can make this much more efficient by pruning the walk to not even look at directories more than 2 deep:

for dirpath, dirnames, filenames in os.walk(top):
    depth = os.path.relpath(dirpath, top).count(os.pathsep)
    if depth == 2:
        if os.path.basename(dirpath) == "anyname":
            print(os.path.join(dirpath, filenames[2]))
        dirnames.clear()

Or, even more efficiently, but a bit more complicatedly, prune all depth-2 directories that aren't named "anyname":

for dirpath, dirnames, filenames in os.walk(top):
    depth = os.path.relpath(dirpath, top).count(os.pathsep)
    if depth == 1:
        dirnames[:] = ["anyname"] if "anyname" in dirnames else []
    elif depth == 2:
        print(os.path.join(dirpath, filenames[2]))
        dirnames.clear()
abarnert
  • 354,177
  • 51
  • 601
  • 671