Skip to content

Expose os.DirEntry objects from pathlib #125413

Description

@barneygale

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

Activity

  1. added 2 commits that reference this issue on Oct 13, 2024
  2. ncoghlan commented on Oct 23, 2024

    @ncoghlan
    Contributor

    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

  3. added a commit that references this issue on Oct 25, 2024
  4. changed the title [-]Add `pathlib.Path.dir_entry`[/-] [+]Expose `os.DirEntry` objects from pathlib[/+] on Oct 28, 2024
  5. added 3 commits that reference this issue on Oct 28, 2024
  6. barneygale commented on Nov 1, 2024

    @barneygale
    ContributorAuthor

    To tie up the above loose ends, we went with a Path.scandir() method in the end.

  7. added 3 commits that reference this issue on Nov 1, 2024
  8. 37 remaining items

  9. added 6 commits that reference this issue on Feb 8, 2025
  10. added a commit that references this issue on Feb 17, 2025
  11. added 7 commits that reference this issue on Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytopic-pathlibtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions