python - 使用 Python os.walk 识别文件列表

标签 python

<分区>

Possible Duplicate:
In Python, fastest way to build a list of files in a directory with a certain extension

我目前正在使用 os.walk 递归扫描一个识别 .MOV 文件的目录

def fileList():
    matches = []
    for root, dirnames, filenames in os.walk(source):
        for filename in fnmatch.filter(filenames, '*.mov'):
            matches.append(os.path.join(root, filename))
    return matches

我如何扩展它以识别多个文件,例如 .mov、.MOV、.avi、.mpg?

谢谢。

最佳答案

尝试这样的事情:

def fileList(source):
    matches = []
    for root, dirnames, filenames in os.walk(source):
        for filename in filenames:
            if filename.endswith(('.mov', '.MOV', '.avi', '.mpg')):
                matches.append(os.path.join(root, filename))
    return matches

关于python - 使用 Python os.walk 识别文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8625991/

相关文章:

python - 如何在Python中使用group-by函数保留列名?

python - 使用 Pillow (PIL) 在 Python 中生成图像

python, "urlparse.urlparse(url).hostname"返回无值

python - 从列表中过滤掉附近的点

python - 如何从 pandas 中所有列的字符串中提取数字并取数字的中位数?

python - 类型错误 : closeEvent() takes exactly 4 arguments (2 given)

python - 根据字典值计算列表中字典中的项目数

python - 在 pymongo 中获取嵌入式文档的最佳方式?

python - 如何使用 `GridSpec()` 和 `subplots()`

python - 如何在 Pandas 中选择具有特定字符串模式的行?