mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
GH-119169: Implement pathlib.Path.walk()
using os.walk()
(#119573)
For silly reasons, pathlib's generic implementation of `walk()` currently resides in `glob._Globber`. This commit moves it into `pathlib._abc.PathBase.walk()` where it really belongs, and makes `pathlib.Path.walk()` call `os.walk()`.
This commit is contained in:
parent
a150679f90
commit
7ff61f51b6
3 changed files with 34 additions and 39 deletions
|
@ -621,7 +621,37 @@ class PathBase(PurePathBase):
|
|||
|
||||
def walk(self, top_down=True, on_error=None, follow_symlinks=False):
|
||||
"""Walk the directory tree from this directory, similar to os.walk()."""
|
||||
return self._globber.walk(self, top_down, on_error, follow_symlinks)
|
||||
paths = [self]
|
||||
while paths:
|
||||
path = paths.pop()
|
||||
if isinstance(path, tuple):
|
||||
yield path
|
||||
continue
|
||||
dirnames = []
|
||||
filenames = []
|
||||
if not top_down:
|
||||
paths.append((path, dirnames, filenames))
|
||||
try:
|
||||
for child in path.iterdir():
|
||||
try:
|
||||
if child.is_dir(follow_symlinks=follow_symlinks):
|
||||
if not top_down:
|
||||
paths.append(child)
|
||||
dirnames.append(child.name)
|
||||
else:
|
||||
filenames.append(child.name)
|
||||
except OSError:
|
||||
filenames.append(child.name)
|
||||
except OSError as error:
|
||||
if on_error is not None:
|
||||
on_error(error)
|
||||
if not top_down:
|
||||
while not isinstance(paths.pop(), tuple):
|
||||
pass
|
||||
continue
|
||||
if top_down:
|
||||
yield path, dirnames, filenames
|
||||
paths += [path.joinpath(d) for d in reversed(dirnames)]
|
||||
|
||||
def absolute(self):
|
||||
"""Return an absolute version of this path
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue