我如何使用Pathlib和Glob在Python中在Python中亮相所有文件
我想使用Pathlib的Glob创建目录中所有文件的列表。文件名可能具有扩展名(例如:'text.txt','abc.csv')或不('text','abc')。 我确实意识到这可以通过相同的解决方案来处理,这是对' pathlib“>如何用pathlib将两个模式覆盖?'但是目前,我不敢相信这么简单的解决方案没有简单的解决方案(尽管很难理解为什么Glob不允许列出模式列表)。
编辑:
- 文件名只是示例;因此,为他们过滤不是我的目标。
- 使用两个呼叫的解决方案并添加结果。这个问题更具体:是否有更简单的解决方案。
- 使用模式'**/*'包括所有子目录,该子目录不打算
- 使用OS。ListDir仅转移问题:现在您必须过滤目录。
I want to create a list of all files in a directory using pathlib's glob. The file names may have an extension (ex.: 'text.txt', 'abc.csv') or not ('text', 'abc').
I do realize that this can be handled by the same solution which is the answer to 'How to glob two patterns with pathlib?' but at the moment I cannot believe that something so simple has no simple solution (though it is difficult to understand why glob doesn't allow a list of patterns).
Edit:
- The file names are just examples; so filtering for them is not my goal.
- There is a solution using two calls of glob and adding the results. The question is more specific: is there a simpler solution.
- using the pattern '**/*' is including all subdirectories, which is not intended
- using os.listdir only shifts the problem: now you have to filter out the directories.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论