简体   繁体   English

使用 pathlib glob 提取文件名以 0/ 1 开头的路径的模式

[英]Pattern for extracting paths where filename starts with 0/ 1 using pathlib glob

I tried using normal python regex patterns in pathlib glob but it's not working.我尝试在pathlib glob中使用普通的 python regex patterns ,但它不起作用。
While reading online I came to know that the glob patterns are quite different.在网上阅读时,我开始知道 glob 模式是完全不同的。
I did not found the pattern online which I was looking for.我没有在网上找到我正在寻找的模式。

I have:我有:
a folder named images like:一个名为images的文件夹,例如:

from pathlib import Path

print(list(Path('./images').glob("*")))

gives:给出:

[WindowsPath('images/01.jpg'),
 WindowsPath('images/11.jpg'),
 WindowsPath('images/010.jpg'),
... ]

I want glob to extract only those images whose name starts with 0 like 01.jpg & 010.jpg & not 11.jpg我希望 glob 只提取名称以 0 开头的图像,例如01.jpg & 010.jpg & not 11.jpg
What will be the pattern to achieve this!实现这一目标的模式是什么!

Try using pathlib's iterdir and startswith尝试使用pathlib's iterdirstartswith

p = Path('./images')
for pth in p.iterdir():
    if pth.name.startswith('0'):
        print(pth)

images/01.jpg
images/010.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM