GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Expose `os.DirEntry` objects from pathlib · Issue #125413 · python/cpython

Expose os.DirEntry objects from pathlib #125413

New issue
Copy link
New issue
Copy link
Closed
Closed
Expose os.DirEntry objects from pathlib#125413
Copy link
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

@barneygale
barneygale
opened on Oct 13, 2024
Issue body actions

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

  • GH-125413: Add pathlib.Path.dir_entry attribute #125419
  • GH-125413: pathlib.Path.copy(): get common metadata keys only once #125990
  • GH-125413: Add pathlib.Path.scandir() method #126060
  • GH-125413: pathlib ABCs: use scandir() to speed up glob() #126261
  • GH-125413: pathlib ABCs: use scandir() to speed up walk() #126262
  • GH-125413: pathlib: use scandir() to speed up copy() #126263
  • GH-125413: Revert addition of pathlib.Path.scandir() method #127377
  • GH-125413: Add pathlib.Path.info attribute #127730
  • GH-125413: Move pathlib.Path.copy() implementation alongside Path.info #129856
  • GH-125413: Add private metadata methods to pathlib.Path.info #129897
  • GH-125413: Add private pathlib.Path method to write metadata #130238
  • GH-125413: pathlib ABCs: use caching path.info.exists() when globbing #130422
  • GH-125413: Fix stale metadata from pathlib.Path.copy() and move() #130424
Reacted by Peter Bierma, blhsing and Nice Zombies

Activity

Next
  1. barneygale
    added
    type-featureA feature request or enhancementA feature request or enhancement
    performancePerformance or resource usagePerformance or resource usage
    topic-pathlib
    on Oct 13, 2024
  2. barneygale
    added 2 commits that reference this issue on Oct 13, 2024

    pythonGH-125413: Add `pathlib.Path.dir_entry` attribute

    3a3780d

    pythonGH-125413: Add `pathlib.Path.dir_entry` attribute

    06477e4
  3. ncoghlan commented on Oct 23, 2024

    @ncoghlan
    ncoghlan
    on Oct 23, 2024
    Contributor
    More actions

    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

    Reacted by Barney Gale
  4. barneygale
    added a commit that references this issue on Oct 25, 2024

    pythonGH-125413: `pathlib.Path.copy()`: get common metadata keys only…

    f8ffd49
  5. barneygale
    changed the title [-]Add `pathlib.Path.dir_entry`[/-] [+]Expose `os.DirEntry` objects from pathlib[/+] on Oct 28, 2024
  6. barneygale
    added 3 commits that reference this issue on Oct 28, 2024

    pythonGH-125413: Add `pathlib.Path.scandir()` method

    d7f13d4

    GH-125413: Add `pathlib.Path.scandir()` method (#126060)

    260843d

    pythonGH-125413: pathlib ABCs: use `scandir()` to speed up `glob()`

    c7ec3c9
  7. barneygale commented on Nov 1, 2024

    @barneygale
    barneygale
    on Nov 1, 2024
    ContributorAuthor
    More actions

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

  8. barneygale
    added 3 commits that reference this issue on Nov 1, 2024

    pythonGH-125413: pathlib ABCs: use `scandir()` to speed up `walk()`

    5e1325b

    pythonGH-125413: pathlib: use `scandir()` to speed up `copy()`

    3c90a9c

    pythonGH-125413: pathlib: use `scandir()` to speed up `copy()`

    9b223ca
  9. 37 remaining items

  10. barneygale
    added 6 commits that reference this issue on Feb 8, 2025

    GH-125413: Add `pathlib.Path.info` attribute (#127730)

    718ab66

    pythonGH-125413: Move `pathlib.Path.copy()` implementation alongside …

    6ac725e

    GH-125413: Move `pathlib.Path.copy()` implementation alongside `Path.…

    c88dacb

    pythonGH-125413: Add private metadata methods to `pathlib.Path.info`

    4dd7c21

    Merge branch 'main' into pythongh-125413-merge-copyreader

    47616de

    Merge branch 'main' into pythongh-125413-merge-copyreader

    bc9032b
  11. encukou
    added a commit that references this issue on Feb 17, 2025

    Merge branch 'main' into pythongh-125413-merge-copyreader

    6c03d51
  12. barneygale
    added 7 commits that reference this issue on Feb 17, 2025

    GH-125413: Add private metadata methods to `pathlib.Path.info` (#129897)

    7fcace9

    pythonGH-125413: pathlib ABCs: use caching `path.info.exists()` when …

    87d37e3

    pythonGH-125413: Fix stale metadata from `pathlib.Path.copy()` and `m…

    be7cc64

    pythonGH-125413: Fix stale metadata from `pathlib.Path.copy()` and `m…

    abba0bb

    GH-125413: pathlib ABCs: use caching `path.info.exists()` when globbi…

    48c84a4

    GH-125413: Fix stale metadata from `pathlib.Path.copy()` and `move()` (…

    78e09a4

    GH-125413: Add private `pathlib.Path` method to write metadata (#130238)

    b251d40
  13. barneygale
    closed this as completedon Apr 10, 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 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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.