Reputation: 120
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
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
isTrue
, the caller can modify thedirnames
list in-place (perhaps usingdel
or slice assignment), andwalk()
will only recurse into the subdirectories whose names remain indirnames
; this can be used to prune the search, impose a specific order of visiting, or even to informwalk()
about directories the caller creates or renames before it resumeswalk()
again.
Upvotes: 1