1

In a directory, I am searching for files that meet a certain condition. I am using code provided by an other stackoverflow post, but it's sending me into an infinite loop for a reason I can't see.

I iterate through directories in a subdirectory. Then, I iterate through the subsubdirectories in the subdirectory, looking for ones that meet a condition. Here is my code:

import os

rootdir ="/mnt/data/SHAVE_cases"

cases = []
for subdirs, dirs, files in os.walk(rootdir):
    for subdir in subdirs:
        print("1")
        # now we are in the file subdirectory. 
        # we need to see if these subsubdirectories start with "multi"
        for subdirs2, d, f in os.walk(rootdir + subdir):
            for s in subdirs2:
                if s.startswith("multi"):
                    cases.append(s)

Here is another code following the advice of msi

for pathname, subdirs, files in os.walk(rootdir):
    for subdir in subdirs:
        for path, sub, f in os.walk(rootdir + subdir):
            print(sub)

It still returns an infinite loop.

McM
  • 471
  • 5
  • 21
  • I see your folder is /mnt/... Is it mounted? – msi_gerva Oct 19 '20 at 18:42
  • yes, it is mounted. Does that change things? – McM Oct 19 '20 at 18:43
  • ok, tried your code and subdirs in first loop is wrong. The first loop has to be: `for pathname, subdirs, files in os.walk(...):`. Yes, it could have some impact when the mounted drive is not mounted anymore. Sometimes doing "ls..." and other operations with drives like that (for example mounted network drives that somehow gets disconnected) can make the system hang. – msi_gerva Oct 19 '20 at 18:46
  • Hmm. It still didn't work. My task has changed since then, so I'm now working on something else and no longer need to do what's above. I will come back to this later today to see if I can get it to work. – McM Oct 19 '20 at 19:01
  • Ok. Problem changed and I need the code again. I've added to my post another piece of code that is like yours. – McM Oct 19 '20 at 21:13

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