GeezerVison is showing github.com — Expose `os.DirEntry` objects from pathlib · Issue #125413 · python/cpython
Expose os.DirEntry objects from pathlib #125413
Description
Feature or enhancement
I propose we add a new Path.status attribute that stores an os.DirEntry object in paths yielded from Path.iterdir(), or a pathlib-specific type with a similar interface in other paths.
This would:
- Allow users to access to the cached
os.DirEntry after calling Path.iterdir(), which is useful for efficiently determining files' types and often doesn't involve a system call.
- Allow users to switch on the type of any path without repeatedly making system calls, or having to resort to
S_ISREG(st.st_mode) and other holy incantations.
- In the pathlib ABCs, allows us to entirely banish
PathBase.stat() and the stat_result interface, which is too low-level and local filesystem-specific
See discussion: https://discuss.python.org/t/is-there-a-pathlib-equivalent-of-os-scandir/46626
Linked PRs
Reacted by Peter Bierma, blhsing and Nice Zombies
Activity
ncoghlan commented on Oct 23, 2024
I put this feedback on the PR, but it's probably better placed here: while I like the general idea, I don't think this specific API is the right way to do it.
dir_entry potentially being None based on how the instance was created is inconvenient
- the docs having to excuse
dir_entry existing on PurePath objects is awkward
I think we can eliminate both of those bits of awkwardness:
- define a new alternative construction method on
os.DirEntry objects that allows one to be created from arbitrary os.PathLike objects
- make the slot on
PurePath private rather than public (presumably as _dir_entry)
- define
PathBase.dir_entry as a read-only property that returns the cached entry if it is already set, otherwise it uses the new constructor API to create a cached DirEntry instance for itself
If it's impractical to add os.DirEntry.from_path, then a pathlib._DirEntry class that just emulated the os.DirEntry API based on the real underlying Path object would also be fine
changed the title
[-]Add `pathlib.Path.dir_entry`[/-] [+]Expose `os.DirEntry` objects from pathlib[/+] on Oct 28, 2024barneygale commented on Nov 1, 2024
ContributorAuthor
More actions
To tie up the above loose ends, we went with a Path.scandir() method in the end.
added 6 commits that reference this issue
on Feb 8, 2025GH-125413: Move `pathlib.Path.copy()` implementation alongside `Path.…
added 7 commits that reference this issue
on Feb 17, 2025GH-125413: pathlib ABCs: use caching `path.info.exists()` when globbi…
GH-125413: Fix stale metadata from `pathlib.Path.copy()` and `move()` (…
Metadata
Metadata
Development
No branches or pull requestsIssue actions