使用 glob 选择文件的正确模式

问题描述 投票:0回答:1

我有一组名为

austin1.tif
austin2.tif
...
austin36.tif
的文件。 对于8个不同的城市。我正在尝试将
austin1.tif
...
austin5.tif
austin6.tif
...
austin36.tif
分开。我想出了很多解决方案,但只想用 glob 函数过滤这些文件。

root_dir = os.path.join(DIR, "train")
pattern = "[a-zA-Z]*[0-5].tif"
images = glob.glob(os.path.join(root_dir, "images", pattern))

print(sorted(images[:10]))

上面的代码片段给了我,文件名为

austin1.tif
以及
austin11.tif
。我知道这是因为模式不正确。可以直接在 glob 中使用的正确模式是什么?

python file split operating-system glob
1个回答
0
投票

glob
不支持Kleene star
*
,即多次重复上一场比赛。
正则表达式
中的等于
*
中的
glob
。你可以试试

pattern = "austin[0-5].tif"
© www.soinside.com 2019 - 2024. All rights reserved.