python / python/cpython

Add `top_down` parameter to `(i)glob` and `Path.(r)glob` matching `{os,Path}.walk`

未关闭
#129,067 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib topic-pathlib type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

Recursively searching for globs is a common use case when working with the filesystem. In cases where one modifies files while working in them it is useful to specify whether one first wants to recurse into directories or first get all the files in a directory before recursing into subdirs.

One simply example is when one wants to delete all empty directories:

for d in context.root.glob("**/", top_down=False):
    if d == context.root:
        continue

    if not any(d.iterdir()):
        d.rmdir()

Currently, one has two alternatives (apart from reimplementing glob) which have their respective drawbacks:

  • for d in reversed(sorted(context.root.glob("**/"))): ... This eagerly consumes and sorts the iterator. This has the drawback of requiring a lot of memory for large trees and taking additional time for the sorting.
  • for d, _, _ in context.root.walk(top_down=False): ... This works for this simple case but does not allow applying search patterns e.g. only looking for empty directories somewhere under .venv (e.g. **/.venv/**/)
Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先跟踪现有的 (i)globPath.(r)glob 入口点,将其行为与 {os,Path}.walk 进行比较。为递归 globbing 实现所请求的 top_down 行为,然后验证诸如 **/ 之类的模式支持自底向上的遍历,同时保留现有的 glob 行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。