gpt4 book ai didi

Python glob 多种文件类型

转载 作者:IT老高 更新时间:2023-10-28 21:06:24 28 4
gpt4 key购买 nike

有没有更好的方法在 python 中使用 glob.glob 来获取多个文件类型的列表,例如 .txt、.mdown 和 .markdown?现在我有这样的东西:

projectFiles1 = glob.glob( os.path.join(projectDir, '*.txt') )
projectFiles2 = glob.glob( os.path.join(projectDir, '*.mdown') )
projectFiles3 = glob.glob( os.path.join(projectDir, '*.markdown') )

最佳答案

也许有更好的方法,但是怎么样:

import glob
types = ('*.pdf', '*.cpp') # the tuple of file types
files_grabbed = []
for files in types:
files_grabbed.extend(glob.glob(files))

# files_grabbed is the list of pdf and cpp files

也许还有其他方法,所以请等待其他人提出更好的答案。

关于Python glob 多种文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4568580/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com