[英]Python search all files in subfolders containing specific subfolder name
For example, I have 3 subfolders in the root directory and 2 subfolders in each subfolders, as shown below:比如我根目录下有3个子文件夹,每个子文件夹下有2个子文件夹,如下图:
C:/main/canada/May 2022
C:/main/canada/Jun 2022
C:/main/usa/May 2022
C:/main/usa/Jun 2022
C:/main/mexico/May 2022
C:/main/mexico/Jun 2022
I am hoping to only scan for files in subfolders with name contains substring 'May'.我希望只扫描名称包含 substring 'May' 的子文件夹中的文件。
I am beginner and tried os, glob, pathlib, but don't think my thinking is correct.我是初学者,尝试过 os、glob、pathlib,但不认为我的想法是正确的。
You could try first globbing for subdirectories that match using glob.glob
, and then finding their parents using pathlib.Path.parent
:您可以尝试首先使用
glob.glob
查找匹配的子目录,然后使用pathlib.Path.parent
查找它们的父目录:
import glob
import pathlib
for subdir in glob.glob("C:/main/*/*May*"):
print(pathlib.Path(subdir).parent)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.