glob“一个或多个”在Python中
我需要获取所有具有数字名称的文本文件:1.txt, 2.txt, 13.txt
可以用glob
来做吗?
import glob
for file in glob.glob('[0-9].txt'):
print(file)
不返回 13.txt。
并且似乎没有正则表达式的一个或多个 +
运算符。
我能做些什么?
I need to get all text files with numeric names: 1.txt, 2.txt, 13.txt
Is it possible to do with glob
?
import glob
for file in glob.glob('[0-9].txt'):
print(file)
Does not return 13.txt.
And there seems to be no regex's one or more +
operator.
What can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 TFM:
因此,不,正则表达式中没有
+
运算符。您可以使用glob
作为第一遍(如glob('*.txt')
中所示),然后使用regex
进一步过滤它。From TFM:
So, no, there are no
+
operators as in regex. You can useglob
as a first-pass (as inglob('*.txt')
), and filter it further withregex
.我认为
glob()
不应该是可定制的。您可能想尝试 os.listdir() 来代替:I don't think that
glob()
is meant to be that customisable. You might want to tryos.listdir()
instead: