Repository navigation
Expose os.DirEntry objects from pathlib #125413
Copy link
Copy link
Closed
Labels
performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-pathlibtype-featureA feature request or enhancementA feature request or enhancement
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancementperformancePerformance or resource usagePerformance or resource usage
on Oct 13, 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_entrypotentially beingNonebased on how the instance was created is inconvenient- the docs having to excuse
dir_entryexisting onPurePathobjects is awkward
I think we can eliminate both of those bits of awkwardness:
- define a new alternative construction method on
os.DirEntryobjects that allows one to be created from arbitraryos.PathLikeobjects - make the slot on
PurePathprivate rather than public (presumably as_dir_entry) - define
PathBase.dir_entryas a read-only property that returns the cached entry if it is already set, otherwise it uses the new constructor API to create a cachedDirEntryinstance for itself
If it's impractical to add
os.DirEntry.from_path, then apathlib._DirEntryclass that just emulated theos.DirEntryAPI based on the real underlyingPathobject would also be fineReacted by Barney Gale- changed the title
[-]Add `pathlib.Path.dir_entry`[/-][+]Expose `os.DirEntry` objects from pathlib[/+]on Oct 28, 2024 To tie up the above loose ends, we went with a
Path.scandir()method in the end.37 remaining items
- added 6 commits that reference this issue
on Feb 8, 2025 - added a commit that references this issue
on Feb 17, 2025 - added 7 commits that reference this issue
on Feb 17, 2025
Metadata
Metadata
Assignees
Labels
performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-pathlibtype-featureA feature request or enhancementA feature request or enhancement
Feature or enhancement
I propose we add a new
Path.statusattribute that stores anos.DirEntryobject in paths yielded fromPath.iterdir(), or a pathlib-specific type with a similar interface in other paths.This would:
os.DirEntryafter callingPath.iterdir(), which is useful for efficiently determining files' types and often doesn't involve a system call.S_ISREG(st.st_mode)and other holy incantations.PathBase.stat()and thestat_resultinterface, which is too low-level and local filesystem-specificSee discussion: https://discuss.python.org/t/is-there-a-pathlib-equivalent-of-os-scandir/46626
Linked PRs
pathlib.Path.dir_entryattribute #125419pathlib.Path.copy(): get common metadata keys only once #125990pathlib.Path.scandir()method #126060scandir()to speed upglob()#126261scandir()to speed upwalk()#126262scandir()to speed upcopy()#126263pathlib.Path.scandir()method #127377pathlib.Path.infoattribute #127730pathlib.Path.copy()implementation alongsidePath.info#129856pathlib.Path.info#129897pathlib.Pathmethod to write metadata #130238path.info.exists()when globbing #130422pathlib.Path.copy()andmove()#130424