我试图使用Python获取目录中的文件列表,但我不想要所有文件的列表。

我本质上想要的是做以下事情的能力,但使用Python而不执行ls。

ls 145592*.jpg

如果没有内置方法,我目前正在考虑编写一个for循环来遍历os.listdir()的结果,并将所有匹配的文件附加到一个新列表中。

但是,该目录中有很多文件,因此我希望有一个更有效的方法(或内置方法)。


当前回答

您可以使用列表推导式和其中的正则表达式检查器来简化它,包括带有指定后缀的图像文件。

import re
import os

dir_name = "."
files = [os.path.join(dir_name, f) for f in os.listdir(dir_name) if re.match(r'.*\.(jpg|jpeg|png)', f)]

其他回答

您可以使用列表推导式和其中的正则表达式检查器来简化它,包括带有指定后缀的图像文件。

import re
import os

dir_name = "."
files = [os.path.join(dir_name, f) for f in os.listdir(dir_name) if re.match(r'.*\.(jpg|jpeg|png)', f)]

你可能也喜欢更高级的方法(我已经实现并打包为findtools):

from findtools.find_files import (find_files, Match)


# Recursively find all *.txt files in **/home/**
txt_files_pattern = Match(filetype='f', name='*.txt')
found_files = find_files(path='/home', match=txt_files_pattern)

for found_file in found_files:
    print found_file

可与

pip install findtools
import glob

jpgFilenamesList = glob.glob('145592*.jpg')

参见python文档中的glob

您可以使用Python标准库3.4及以上版本中提供的pathlib。

from pathlib import Path

files = [f for f in Path.cwd().iterdir() if f.match("145592*.jpg")]

保持简单:

import os
relevant_path = "[path to folder]"
included_extensions = ['jpg','jpeg', 'bmp', 'png', 'gif']
file_names = [fn for fn in os.listdir(relevant_path)
              if any(fn.endswith(ext) for ext in included_extensions)]

我更喜欢这种形式的列表推导式,因为它的英文可读性很好。

我把第四行读成: 对于os中的每一个fn。Listdir为我的路径,只给我匹配任何一个我所包含的扩展。

对于python程序员新手来说,可能很难真正习惯使用列表推导式进行过滤,而且对于非常大的数据集可能会有一些内存开销,但对于列出目录和其他简单的字符串过滤任务,列表推导式会带来更干净的可记录的代码。

关于这种设计的唯一问题是,它不能防止您犯传递字符串而不是列表的错误。例如,如果您意外地将一个字符串转换为一个列表,并最终检查字符串的所有字符,您可能最终会得到大量的假阳性结果。

但有一个容易解决的问题总比有一个难以理解的解决方案要好。