python / python/cpython

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

Open
#129,067 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-pathlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the existing (i)glob and Path.(r)glob entry points, comparing their behavior with {os,Path}.walk. Implement the requested top_down behavior for recursive globbing, then verify that patterns such as **/ support bottom-up traversal while preserving existing glob behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.