Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2024

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Oct 28, 2024

Add pathlib.Path.scandir() as a trivial wrapper of os.scandir().

In a future PR, I will rework pathlib._abc.PathBase.glob(), walk() and copy() methods to call scandir() and make use of cached directory entry information, and thereby improve performance. Because the Path.copy() method is provided by PathBase, this also speeds up traversal when copying local files and directories.


📚 Documentation preview 📚: https://cpython-previews--126060.org.readthedocs.build/

Add `pathlib.Path.scandir()` as a trivial wrapper of `os.scandir()`.

In the private `pathlib._abc.PathBase` class, we can rework the
`iterdir()`, `glob()`, `walk()` and `copy()` methods to call `scandir()`
and make use of cached directory entry information, and thereby improve
performance. Because the `Path.copy()` method is provided by `PathBase`,
this also speeds up traversal when copying local files and directories.
Copy link
Contributor

@ncoghlan ncoghlan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@barneygale
Copy link
Contributor Author

Woot! Thanks very much for your feedback on this, it's been most helpful in clarifying what's needed.

@barneygale barneygale merged commit 260843d into python:main Nov 1, 2024
39 of 40 checks passed
@Ferdi265
Copy link

Ferdi265 commented Nov 17, 2024

I'm not sure if this is the correct place to put this feedback, but Path.scandir() returning an iterator of os.DirEntry directly, whose .path member is a str or bytes, rather than a Path is unexpected.

At least on first glance, it's unexpected that a member named "path" on an object returned from a method of Path is not actually of type Path.

with path.scandir() as entries:
  for entry in entries:
    assert isinstance(entry.path, Path), "unexpected type"

@barneygale
Copy link
Contributor Author

barneygale commented Nov 19, 2024

Would you care to open a topic on discuss.python.org? It's a good thought and worth bringing to wider attention I think.

At the moment you can get a path object with path / entry.name (edit: or pathlib.Path(entry)).

@Ferdi265
Copy link

Would you care to open a topic on discuss.python.org?

done

picnixz pushed a commit to picnixz/cpython that referenced this pull request Dec 8, 2024
Add `pathlib.Path.scandir()` as a trivial wrapper of `os.scandir()`. This
will be used to implement several `PathBase` methods more efficiently,
including methods that provide `Path.copy()`.
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
Add `pathlib.Path.scandir()` as a trivial wrapper of `os.scandir()`. This
will be used to implement several `PathBase` methods more efficiently,
including methods that provide `Path.copy()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-pathlib type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants