cat_on_the_mat
cat_on_the_mat

Reputation: 120

search for the directory locations with os.walk (and not to check the other directories the same level)

I'm going to fetch all the filenames and locations in the particular sub directory (if fit the pattern). These sub directories are located in the different locations with different depth (as it was discussed: how to get all the files from multiple folders with the same names).

My problem: asap I find 'xyz' in the directory , I don't want to check if 'xyz' is a sub directory for any other directories on the same level (for an example, I don't want to check if 'D:/qwer/lkj' may contain 'xyz'.

D:/qwer/xyz
D:/qwerty/qwertyui/xyz
D:/qwerty/zxc/zxc1/zxcv12/zx/xyz

The code:

for dirpath, dirnames, filenames in os.walk(path_to_main_search = 'D:\\'):
    if 'xyz' in dirpath:
      filenames = [fn for fn in filenames if fnmatch.fnmatch(fn, pattern)]

Upvotes: 0

Views: 36

Answers (1)

blhsing
blhsing

Reputation: 106553

You can remove all directory names other than 'xyz' from dirnames in-place so that they will not be traversed:

for dirpath, dirnames, filenames in os.walk(path_to_main_search):
    if 'xyz' in dirnames:
        dirnames[:] = ['xyz']
    if os.path.basename(dirpath) == 'xyz':
        # process filenames

Excerpt from os.walk's documentation:

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again.

Upvotes: 1

Related Questions