Recurse through selected level of subdirectories

189 views Asked by At

I am new to python. I have the following piece of code which works well by retrieving selected directories into a list for me. But because there are quite a lot of sub-directories and files, the code is rather slow, compared to the Perl code which I have upgraded it from.

using re
using os

foundarr = []
allpaths = ["X:\\Storage", "Y:\\Storage"]

for path in allpaths:
    for root, dirs, files in os.walk(path):
        for dir in dirs:
            if re.match("[DILMPY]\d{8}", dir):
                foundarr.append(os.path.join(root, dir))
                break

My question: Is there a way to recurse through ONLY a selected level of directories using os.walk ? Or somehow prune the ones I do not want to recurse through? I have added the break in the for loop assuming it will break after it finds my selected dir and moves on, but I dont think this helps as it still has to go through thousands of sub-directories and files.

In the Perl code a simple $File::Find::prune = 1 if /[DILMPY]\d{8}$/; prevents the compiler from recursing through the rest of the sub-directories and files.

1

There are 1 answers

0
cutteeth On BEST ANSWER

If the depth is fixed using glob is a good idea. As per this SO post you can set the depth of traversal using glob.

import glob
import os.path
depth2 = glob.glob('*/*')
depth2 = filter(lambda f: os.path.isdir(f), depth2)

This will list all subdirectories with a depth of 2.