【问题标题】:Glob files for a range of numbers [duplicate]一系列数字的全局文件[重复]
【发布时间】:2019-07-17 00:10:48
【问题描述】:

我有一个名为 10.txt、11.txt .. 29.txt、30.txt 的文件目录

如何选择文件 12 到 21?

我试过了:

glob.glob('path/[12-21].txt')

【问题讨论】:

标签: python glob


【解决方案1】:

如果您不知道什么文件名并且只需要一个粗略的模式,则 Glob 很好,但是,在您的情况下,您确实知道,因此您可能不需要使用 glob

最好简单地生成您期望的文件列表并检查它们是否存在,例如

from os.path import isfile

# Generate a list of expected file names
expected_files = ["path/{}.txt".format(i) for i in range(12, 22)]

# Filter the list to just the files that actually exist.
actual_files = [f for f in expected_files if isfile(f)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2023-02-08
    • 1970-01-01
    相关资源
    最近更新 更多