【发布时间】:2019-01-22 17:49:31
【问题描述】:
我将获取特定子目录中的所有文件名和位置(如果符合模式)。这些子目录位于不同深度的不同位置(讨论过:how to get all the files from multiple folders with the same names)。
我的问题:我尽快在目录中找到“xyz”,我不想检查“xyz”是否是同一级别上任何其他目录的子目录(例如,我不想检查“D:/qwer/lkj”是否可能包含“xyz”。
D:/qwer/xyz
D:/qwerty/qwertyui/xyz
D:/qwerty/zxc/zxc1/zxcv12/zx/xyz
代码:
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)]
【问题讨论】:
-
在找到 D:/qwer/xyz 后是否要检查 D:/qwerty/qwertyui/xyz 的其他内容?
-
是的,我想继续在 D:/qwerty/ 中搜索。例如,即使有 /qwerty/qwertyui2 和 /qwerty/qwertyui3 我也需要去那里。如果 'xyz' 在 /qwerty/qwertyui2 我也想找到它,但不需要去 D:/qwerty/qwertyui/new/xyz
标签: python python-3.x os.walk