mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +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
37
Lib/glob.py
37
Lib/glob.py
|
@ -519,43 +519,6 @@ class _Globber:
|
|||
elif self.lexists(path):
|
||||
yield path
|
||||
|
||||
@classmethod
|
||||
def walk(cls, root, top_down, on_error, follow_symlinks):
|
||||
"""Walk the directory tree from the given root, similar to os.walk().
|
||||
"""
|
||||
paths = [root]
|
||||
while paths:
|
||||
path = paths.pop()
|
||||
if isinstance(path, tuple):
|
||||
yield path
|
||||
continue
|
||||
try:
|
||||
with cls.scandir(path) as scandir_it:
|
||||
dirnames = []
|
||||
filenames = []
|
||||
if not top_down:
|
||||
paths.append((path, dirnames, filenames))
|
||||
for entry in scandir_it:
|
||||
name = entry.name
|
||||
try:
|
||||
if entry.is_dir(follow_symlinks=follow_symlinks):
|
||||
if not top_down:
|
||||
paths.append(cls.parse_entry(entry))
|
||||
dirnames.append(name)
|
||||
else:
|
||||
filenames.append(name)
|
||||
except OSError:
|
||||
filenames.append(name)
|
||||
except OSError as error:
|
||||
if on_error is not None:
|
||||
on_error(error)
|
||||
else:
|
||||
if top_down:
|
||||
yield path, dirnames, filenames
|
||||
if dirnames:
|
||||
prefix = cls.add_slash(path)
|
||||
paths += [cls.concat_path(prefix, d) for d in reversed(dirnames)]
|
||||
|
||||
|
||||
class _StringGlobber(_Globber):
|
||||
lexists = staticmethod(os.path.lexists)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue