https://github.com/python/cpython/commit/30f0643e36d2c9a5849c76ca0b27b748448d0567
commit: 30f0643e36d2c9a5849c76ca0b27b748448d0567
branch: main
author: Barney Gale <[email protected]>
committer: barneygale <[email protected]>
date: 2024-04-12T22:02:39Z
summary:
GH-117727: Speed up `pathlib.Path.iterdir()` by using `os.scandir()` (#117728)
Replace use of `os.listdir()` with `os.scandir()`. Forgo setting `_drv`,
`_root` and `_tail_cached`, as these usually aren't needed. Use
`os.DirEntry.path` to set `_str`.
files:
A Misc/NEWS.d/next/Library/2024-04-10-21-30-37.gh-issue-117727.uAYNVS.rst
M Lib/pathlib/__init__.py
diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py
index 746cbcd9d83d86..66eb08a45b1bb3 100644
--- a/Lib/pathlib/__init__.py
+++ b/Lib/pathlib/__init__.py
@@ -584,26 +584,12 @@ def iterdir(self):
The children are yielded in arbitrary order, and the
special entries '.' and '..' are not included.
"""
- return (self._make_child_relpath(name) for name in os.listdir(self))
-
-
- def _make_child_relpath(self, name):
- if not name:
- return self
- path_str = str(self)
- tail = self._tail
- if tail:
- path_str = f'{path_str}{self.parser.sep}{name}'
- elif path_str != '.':
- path_str = f'{path_str}{name}'
- else:
- path_str = name
- path = self.with_segments(path_str)
- path._str = path_str
- path._drv = self.drive
- path._root = self.root
- path._tail_cached = tail + [name]
- return path
+ root_dir = str(self)
+ with os.scandir(root_dir) as scandir_it:
+ paths = [entry.path for entry in scandir_it]
+ if root_dir == '.':
+ paths = map(self._remove_leading_dot, paths)
+ return map(self._from_parsed_string, paths)
def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=False):
"""Iterate over this subtree and yield all existing files (of any
diff --git
a/Misc/NEWS.d/next/Library/2024-04-10-21-30-37.gh-issue-117727.uAYNVS.rst
b/Misc/NEWS.d/next/Library/2024-04-10-21-30-37.gh-issue-117727.uAYNVS.rst
new file mode 100644
index 00000000000000..3a0b6834e91f18
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-04-10-21-30-37.gh-issue-117727.uAYNVS.rst
@@ -0,0 +1,2 @@
+Speed up :meth:`pathlib.Path.iterdir` by using :func:`os.scandir`
+internally.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]