Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Open
@septatrix

Description

@septatrix
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.