简体   繁体   English

使用glob在三个子文件夹中递归查找所有zip个文件

[英]Using glob to find all zip files recursively in three sub folder

I am trying to look only in three specific subfolders and then recursively create a list of all zip files within the folders.我试图只查看三个特定的子文件夹,然后递归地创建文件夹内所有 zip 文件的列表。 I can easily do this with just 1 folder and recursively look through all subfolders that are within the inputpath, but there are other folders that get created that we cannot use plus we do not know what the folder names will be.我可以只用 1 个文件夹轻松地做到这一点,并递归地查看输入路径中的所有子文件夹,但是还有其他创建的文件夹我们无法使用,而且我们不知道文件夹名称是什么。 So This is where I am at and I am not sure how to pass three subfolders to glob correctly.所以这就是我所在的位置,我不确定如何将三个子文件夹正确传递给 glob。

# using  glob, create a list of all the zip files in specified sub directories COMM, NMR, and NMH inside of input_path
    zip_file = glob.glob(os.path.join(inputpath, "/comm/*.zip,/nmr/*.zip,/nmh/*.zip"), recursive=True)
    #print(zip_file)
    print(f"Found {len(zip_file)} zip files")

The string with commas in it is... just a string.带逗号的字符串是...只是一个字符串。 If you want to perform three globs, you need something like如果你想执行三个 globs,你需要类似的东西

zip_file = []
for dir in {"comm", "nmr", "nmh"}:
    zip_file.extend(glob.glob(os.path.join(inputpath, dir, "*.zip"), recursive=True)

As noted by @Barmar in comments, if you want to look for zip files anywhere within these folders, the pattern needs to be ...(os.path.join(inputpath, dir, "**/*.zip") . If not, perhaps edit your question to provide an example of the structure you want to traverse.正如@Barmar 在评论中指出的那样,如果您想在这些文件夹中的任何位置查找 zip 文件,则模式需要为...(os.path.join(inputpath, dir, "**/*.zip") 。如果没有,也许编辑您的问题以提供您要遍历的结构的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM