python的glob函数是否支持可变深度的通配符?
我正在编写一个使用这种尴尬的 glob 语法的 python 脚本。
import glob
F = glob.glob('./www.dmoz.org/Science/Environment/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/*/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/*/*/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/*/*/*/index.html')
似乎应该有一种方法可以将其包装为一行:
F = glob.glob('./www.dmoz.org/Science/Environment/[super_wildcard]/index.html')
但我不知道合适的超级通配符是什么。这样的事存在吗?
I'm writing a python script that uses this awkward glob syntax.
import glob
F = glob.glob('./www.dmoz.org/Science/Environment/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/*/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/*/*/index.html')
F += glob.glob('./www.dmoz.org/Science/Environment/*/*/*/*/index.html')
Seems like there ought to be a way to wrap this is one line:
F = glob.glob('./www.dmoz.org/Science/Environment/[super_wildcard]/index.html')
But I don't know what the appropriate super wildcard would be. Does such a thing exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
抱歉 - 事实并非如此。您可能需要使用 os.walk 编写几行代码:
Sorry - it does not. You will have to probably write few lines of code using os.walk:
我不知道这是否是新的,但 glob 现在可以做到这一点。
例如,
I don't know if this is new, but glob CAN do this now.
For example,
我刚刚发布了 Formic ,它在实现中准确实现了您需要的通配符 - '**' Apache Ant 的 FileSet 和 Glob。
可以执行搜索:
这将从当前目录开始搜索。我希望这有帮助。
I have just released Formic which implements exactly the wildcard you need - '**' - in an implementation of Apache Ant's FileSet and Globs.
The search can be implemented:
This will search from the current directory. I hope this helps.
它并不完美,但对我有用:
It's not perfect, but works for me:
10 年后...pathlib 解决方案
其中
[super_wildcard]
=**
。10 years later ... pathlib solution
Where
[super_wildcard]
=**
.