Open
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
Metadata
Metadata
Assignees
Labels
Python modules in the Lib dirPython modules in the Lib dirA feature request or enhancementA feature request or enhancement