-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
Conversation
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.
I'll save these for another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Woot! Thanks very much for your feedback on this, it's been most helpful in clarifying what's needed. |
I'm not sure if this is the correct place to put this feedback, but 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" |
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 |
|
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()`.
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()`.
Add
pathlib.Path.scandir()
as a trivial wrapper ofos.scandir()
.In a future PR, I will rework
pathlib._abc.PathBase.glob()
,walk()
andcopy()
methods to callscandir()
and make use of cached directory entry information, and thereby improve performance. Because thePath.copy()
method is provided byPathBase
, this also speeds up traversal when copying local files and directories.os.DirEntry
objects from pathlib #125413📚 Documentation preview 📚: https://cpython-previews--126060.org.readthedocs.build/