我如何使用Pathlib和Glob在Python中在Python中亮相所有文件

发布于 2025-01-27 21:52:29 字数 506 浏览 3 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文