这是我所拥有的:
glob(os.path.join('src','*.c'))
但是我想搜索src的子文件夹。这样做是可行的:
glob(os.path.join('src','*.c'))
glob(os.path.join('src','*','*.c'))
glob(os.path.join('src','*','*','*.c'))
glob(os.path.join('src','*','*','*','*.c'))
但这显然是有限和笨拙的。
这是我所拥有的:
glob(os.path.join('src','*.c'))
但是我想搜索src的子文件夹。这样做是可行的:
glob(os.path.join('src','*.c'))
glob(os.path.join('src','*','*.c'))
glob(os.path.join('src','*','*','*.c'))
glob(os.path.join('src','*','*','*','*.c'))
但这显然是有限和笨拙的。
当前回答
下面是我的解决方案,使用列表理解在一个目录和所有子目录中递归地搜索多个文件扩展名:
import os, glob
def _globrec(path, *exts):
""" Glob recursively a directory and all subdirectories for multiple file extensions
Note: Glob is case-insensitive, i. e. for '\*.jpg' you will get files ending
with .jpg and .JPG
Parameters
----------
path : str
A directory name
exts : tuple
File extensions to glob for
Returns
-------
files : list
list of files matching extensions in exts in path and subfolders
"""
dirs = [a[0] for a in os.walk(path)]
f_filter = [d+e for d in dirs for e in exts]
return [f for files in [glob.iglob(files) for files in f_filter] for f in files]
my_pictures = _globrec(r'C:\Temp', '\*.jpg','\*.bmp','\*.png','\*.gif')
for f in my_pictures:
print f
其他回答
从Python 3.4开始,可以在新的pathlib模块中使用Path类之一的glob()方法,它支持**通配符。例如:
from pathlib import Path
for file_path in Path('src').glob('**/*.c'):
print(file_path) # do whatever you need with these files
更新: 从Python 3.5开始,glob.glob()也支持相同的语法。
import os
import fnmatch
def recursive_glob(treeroot, pattern):
results = []
for base, dirs, files in os.walk(treeroot):
goodfiles = fnmatch.filter(files, pattern)
results.extend(os.path.join(base, f) for f in goodfiles)
return results
Fnmatch提供了与glob完全相同的模式,因此这是glob的绝佳替代品。语义非常接近的Glob。迭代版本(例如生成器),替换glob。Iglob是一个简单的改编(只在执行过程中产生中间结果,而不是扩展一个结果列表到最后返回)。
这是Python 2.7上的一个工作代码。作为devops工作的一部分,我被要求编写一个脚本来移动标有live-appName的配置文件。属性到appName.properties。可能还有其他扩展文件,比如live-appName.xml。
下面是一个工作代码,它查找给定目录中的文件(嵌套级别),然后将其重命名(移动)到所需的文件名
def flipProperties(searchDir):
print "Flipping properties to point to live DB"
for root, dirnames, filenames in os.walk(searchDir):
for filename in fnmatch.filter(filenames, 'live-*.*'):
targetFileName = os.path.join(root, filename.split("live-")[1])
print "File "+ os.path.join(root, filename) + "will be moved to " + targetFileName
shutil.move(os.path.join(root, filename), targetFileName)
此函数从主脚本调用
flipProperties(searchDir)
希望这能帮助有类似问题的人。
import os, glob
for each in glob.glob('path/**/*.c', recursive=True):
print(f'Name with path: {each} \nName without path: {os.path.basename(each)}')
Glob.glob ('*.c'):匹配当前目录下所有以.c结尾的文件 Glob.glob ('*/*.c'):与1相同 Glob.glob ('**/*.c'):只匹配直接子目录中以.c结尾的所有文件,不匹配当前目录 glob.glob('*.c',recursive=True):与1相同 glob.glob('*/*.c',recursive=True):与3相同 glob.glob('**/*.c',recursive=True):匹配当前目录和所有子目录中以.c结尾的所有文件
除了建议的答案,你还可以用一些惰性生成和列表理解魔法来做到这一点:
import os, glob, itertools
results = itertools.chain.from_iterable(glob.iglob(os.path.join(root,'*.c'))
for root, dirs, files in os.walk('src'))
for f in results: print(f)
除了适合一行并避免内存中不必要的列表之外,这还有一个很好的副作用,你可以以类似于**操作符的方式使用它,例如,你可以使用os.path。Join (root, 'some/path/*.c')以获取SRC所有子目录下具有此结构的所有。c文件。