GH-125413: Add pathlib.Path.scandir() method#126060
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.
ncoghlan
left a comment
There was a problem hiding this comment.
LGTM!
Sorry, something went wrong.
|
Woot! Thanks very much for your feedback on this, it's been most helpful in clarifying what's needed. |
Sorry, something went wrong.
|
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" |
Sorry, something went wrong.
|
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 |
Sorry, something went wrong.
|
Sorry, something went wrong.
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.DirEntryobjects from pathlib #125413📚 Documentation preview 📚: https://cpython-previews--126060.org.readthedocs.build/