summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-11-01 01:19:01 (GMT)
committerGitHub <noreply@github.com>2024-11-01 01:19:01 (GMT)
commit260843df1bd8a28596b9a377d8266e2547f7eedc (patch)
treedb6186d963dee64b6448445ea726a9e611453813 /Lib/pathlib
parentd0abd0b826cfa574d1515c6f8459c9901939388f (diff)
downloadcpython-260843df1bd8a28596b9a377d8266e2547f7eedc.zip
cpython-260843df1bd8a28596b9a377d8266e2547f7eedc.tar.gz
cpython-260843df1bd8a28596b9a377d8266e2547f7eedc.tar.bz2
GH-125413: Add `pathlib.Path.scandir()` method (#126060)
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()`.
Diffstat (limited to 'Lib/pathlib')
-rw-r--r--Lib/pathlib/_abc.py12
-rw-r--r--Lib/pathlib/_local.py8
2 files changed, 19 insertions, 1 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index 11c8018..dfff8b4 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -639,13 +639,23 @@ class PathBase(PurePathBase):
with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f:
return f.write(data)
+ def scandir(self):
+ """Yield os.DirEntry objects of the directory contents.
+
+ The children are yielded in arbitrary order, and the
+ special entries '.' and '..' are not included.
+ """
+ raise UnsupportedOperation(self._unsupported_msg('scandir()'))
+
def iterdir(self):
"""Yield path objects of the directory contents.
The children are yielded in arbitrary order, and the
special entries '.' and '..' are not included.
"""
- raise UnsupportedOperation(self._unsupported_msg('iterdir()'))
+ with self.scandir() as entries:
+ names = [entry.name for entry in entries]
+ return map(self.joinpath, names)
def _glob_selector(self, parts, case_sensitive, recurse_symlinks):
if case_sensitive is None:
diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py
index a789971..ef072b8 100644
--- a/Lib/pathlib/_local.py
+++ b/Lib/pathlib/_local.py
@@ -615,6 +615,14 @@ class Path(PathBase, PurePath):
path_str = path_str[:-1]
yield path_str
+ def scandir(self):
+ """Yield os.DirEntry objects of the directory contents.
+
+ The children are yielded in arbitrary order, and the
+ special entries '.' and '..' are not included.
+ """
+ return os.scandir(self)
+
def iterdir(self):
"""Yield path objects of the directory contents.