0

I am searching for directories that contain a certain string. Here is my code. Please substitute your own path.

rootdir = "/mnt/data/cases"

for pathname, subdirs, files in os.walk(rootdir):     # grabs subdirectories of /cases/
    for subdir in subdirs:                            # iterates through subdirectories
        for path, sub, f in os.walk(rootdir + subdir): # grabs subdirectories of subdir
            for s in sub:                              # iterates through each subsub
                if s.startswith("multi") 
                    cases.append(s)

I have tried to update the code, by just grabbing each set of directories one at a time, in separate loops.

pathname, subdirs, files = os.walk(rootdir)

i = 0
for pathname_1, subdirs_1, files_1 in os.walk(rootdir + "/" + subdirs[i]):
    for subdir in subdirs_1:
        if subdir.startswith("multi"):
            print("Got one!")
McM
  • 471
  • 5
  • 21
  • Why are you nesting calls to os.walk? –  Oct 19 '20 at 22:01
  • I begin in a directory containing subdirectories. This subdirectory contains subsubdirectories. I have to find the right subdirectory (one that starts with multi) and then search for a certain subsubsubdirectory. So above, I use os.walk to get the subdirectories, and then again i call os.walk to get the next set. – McM Oct 19 '20 at 22:22
  • Re-look at your code and then answer @JustinEzequiel's question again. You've got nested calls to os.walk, before you start looking for a subdirectory starting with multi. – Frank Yellin Oct 19 '20 at 22:31
  • That is correct, but I think that is necessary (I'm probably wrong). I update my code with comments. My understanding is that I call os.walk every time I want to retrieve subdirectories of a directory. If I intend to search through the subdirectories of each subdirectory (3 deep), does os.walk need to be called multiple times? – McM Oct 19 '20 at 22:53
  • I have added a second revised version, but it's still not working @JustinEzequiel . I removed the nested loop. – McM Oct 19 '20 at 23:19

1 Answers1

0

I solved this using the answer from this question.

To search through the subdirectories for a name, we use find_dir().

import os

def find_dir(name, start):
    for root, dirs, files in os.walk(start):
        for d in dirs:
            if d == name:
                return os.path.abspath(os.path.join(root, d))

I've changed my code completely. Here is a piece of it, so you can see find_dir() in action.

for key in dictionary:
    name = "goal_dir"
    
    if find_dir(name, key) != None:
        # perform analysis on the directory
McM
  • 471
  • 5
  • 21