这是我所拥有的:
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'))
但这显然是有限和笨拙的。
当前回答
简化版的Johan Dahlin的回答,没有fnmatch。
import os
matches = []
for root, dirnames, filenames in os.walk('src'):
matches += [os.path.join(root, f) for f in filenames if f[-2:] == '.c']
其他回答
最近我不得不恢复我的图片扩展。jpg。我运行photorec,恢复了4579个目录,其中有220万个文件,扩展名种类繁多。使用下面的脚本,我能够在几分钟内选择50133个扩展名为。jpg的文件:
#!/usr/binenv python2.7
import glob
import shutil
import os
src_dir = "/home/mustafa/Masaüstü/yedek"
dst_dir = "/home/mustafa/Genel/media"
for mediafile in glob.iglob(os.path.join(src_dir, "*", "*.jpg")): #"*" is for subdirectory
shutil.copy(mediafile, dst_dir)
对于python >= 3.5,可以使用**,递归=True:
import glob
for f in glob.glob('/path/**/*.c', recursive=True):
print(f)
如果递归为True(默认为False),模式**将匹配任何文件和零 或者更多的目录和子目录。如果模式后面跟着 一个操作系统。Sep,只有目录和子目录匹配。
Python 3演示
或者使用列表推导式:
>>> base = r"c:\User\xtofl"
>>> binfiles = [ os.path.join(base,f)
for base, _, files in os.walk(root)
for f in files if f.endswith(".jpg") ]
下面是一种解决方案,它将根据完整路径匹配模式,而不仅仅是基本文件名。
它使用fnmatch。Translate将全局样式的模式转换为正则表达式,然后在遍历目录时与找到的每个文件的完整路径进行匹配。
re.IGNORECASE是可选的,但在Windows上是可取的,因为文件系统本身不区分大小写。(我没有费心编译正则表达式,因为文档表明它应该在内部缓存。)
import fnmatch
import os
import re
def findfiles(dir, pattern):
patternregex = fnmatch.translate(pattern)
for root, dirs, files in os.walk(dir):
for basename in files:
filename = os.path.join(root, basename)
if re.search(patternregex, filename, re.IGNORECASE):
yield filename
我需要一个python 2的解决方案。在大目录上快速工作的X。 我得出了这个结论:
import subprocess
foundfiles= subprocess.check_output("ls src/*.c src/**/*.c", shell=True)
for foundfile in foundfiles.splitlines():
print foundfile
注意,您可能需要一些异常处理,以防ls找不到任何匹配的文件。