8000 gh-90385: Add `pathlib.Path.walk()` method by zmievsa · Pull Request #92517 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-90385: Add pathlib.Path.walk() method #92517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ac622b7
Add Path.walk and Path.walk_bottom_up methods
zmievsa May 8, 2022
14f031a
Fix errors in Path.walk docstrings and add caching of entries
zmievsa May 9, 2022
b203517
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
Ovsyanka83 May 9, 2022
3ad60a9
Refactor symlink handling
zmievsa May 9, 2022
889d7fe
Merge branch 'bpo-46227/add-pathlib.Path.walk-method' of github.com:O…
zmievsa May 9, 2022
2f98823
Add Path.walk docs and unite Path.walk interfaces
zmievsa May 10, 2022
513030a
Remove Path.walk_bottom_up definition
zmievsa May 10, 2022
5fdd72e
📜🤖 Added by blurb_it.
blurb-it[bot] May 10, 2022
452f24e
Add Path.walk tests
zmievsa May 10, 2022
3702a12
Make Path.walk variable naming consistent
zmievsa May 10, 2022
fabc925
Remove redundant FIXME
zmievsa May 10, 2022
b387b54
Minor Path.walk docs and tests fixes
zmievsa May 10, 2022
097fbbf
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
merwok Jun 27, 2022
76fadfc
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
0c19871
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
50b4a2b
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
cade3e9
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
b32627c
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
d1a0833
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
e367f1f
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
bf8b0eb
Fix 'no blank lines' error
zmievsa Jun 30, 2022
d8667c7
Apply suggestions from code review
Ovsyanka83 Jul 3, 2022
4509797
More code review fixes for Path.walk
zmievsa Jul 3, 2022
20a73ed
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
Ovsyanka83 Jul 3, 2022
e61d57b
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
brettcannon Jul 8, 2022
15d96b9
Apply suggestions from code review
Ovsyanka83 Jul 9, 2022
92e1a7a
Apply suggestions from code review
Ovsyanka83 Jul 9, 2022
c509da3
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
Ovsyanka83 Jul 9, 2022
cfa730d
Code review fixes
zmievsa Jul 10, 2022
7aec96d
Clarify pathlib.Path.walk() error handling
zmievsa Jul 10, 2022
38fe1e5
Apply suggestions from code review
Ovsyanka83 Jul 10, 2022
eef3ba3
Code review fixes
zmievsa Jul 10, 2022
4dfdcd7
Merge branch 'bpo-46227/add-pathlib.Path.walk-method' of github.com:O…
zmievsa Jul 10, 2022
8fe3b62
Apply suggestions from code review
Ovsyanka83 Jul 12, 2022
e8ea6ba
Code review fixes
zmievsa Jul 12, 2022
79cf8fd
Remove backticks around True and False
zmievsa Jul 13, 2022
bed850e
Apply suggestions from code review
Ovsyanka83 Jul 17, 2022
203ec3d
Apply suggestions from code review
zmievsa Jul 17, 2022
eef6054
Apply suggestions from code review
brettcannon Jul 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add Path.walk and Path.walk_bottom_up methods
  • Loading branch information
zmievsa committed May 8, 2022
commit ac622b7fbbd245a29d501ba91c59b4ff44aeac03
126 changes: 126 additions & 0 deletions Lib/pathlib.py
A5B2
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,132 @@ def expanduser(self):

return self

def walk(self, on_error=None, follow_links=False):
"""Generate a top-down directory tree from this directory

For each directory in the directory tree rooted at self (including
self but excluding '.' and '..'), yields a 3-tuple

root_directory, child_directory_names, child_file_names

The caller can modify the child_directory_names list in-place
(e.g., via del or slice assignment), and walk will only recurse into
the subdirectories whose names remain in child_directory_names; this
can be used to prune the search, or to impose a specific order of
visiting.

By default errors from Path._scandir() call are ignored. If
optional arg 'on_error' is specified, it should be a function; it
will be called with one argument, an OSError instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename attribute of the exception object.

By default, Path.walk does not follow symbolic links to subdirectories
on systems that support them. In order to get this functionality, set
the optional argument 'follow_links' to true.

Caution: if self is a relative Path, don't change the
current working directory between resumptions of walk. walk never
changes the current directory, and assumes that the client doesn't
either.

Example:

from pathlib import Path
for root, dirs, files in Path('python/Lib/email'):
print(root, "consumes", end="")
print(sum((root / file).stat().st_size for file in files), end="")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
"""
sys.audit("pathlib.Path.walk", self, on_error, follow_links)
return self._walk(True, on_error, follow_links)

def walk_bottom_up(self, on_error=None, follow_links=False):
"""Generate a bottom up directory tree from this directory

The return type and arguments are identical to Path.walk.
However, the caller cannot modify the child_directory_names to prune
the search or to impose a specific order of visiting because the list
of directories to visit is calculated before we yield it.
"""
sys.audit("pathlib.Path.walk_bottom_up", self, on_error, follow_links)
return self._walk(False, on_error, follow_links)

def _walk(self, topdown, on_error, follow_links):
dirs = []
nondirs = []
walk_dirs = []

# We may not have read permission for self, in which case we can't
# get a list of the files the directory contains. os.walk
# always suppressed the exception then, rather than blow up for a
# minor reason when (say) a thousand readable directories are still
# left to visit. That logic is copied here.
try:
scandir_it = self._scandir()
except OSError as error:
if on_error is not None:
on_error(error)
return

with scandir_it:
while True:
try:
entry = next(scandir_it, ...)
if entry is ...:
break
except OSError as error:
if on_error is not None:
on_error(error)
return

try:
is_dir = entry.is_dir()
except OSError:
# If is_dir() raises an OSError, consider that the entry
# is not a directory, same behavior as os.path.isdir()
is_dir = False

if is_dir:
dirs.append(entry.name)
else:
nondirs.append(entry.name)

if not topdown and is_dir:
# Bottom-up: recurse into sub-directory, but exclude symlinks to
# directories if follow_links is False
if follow_links:
walk_into = True
else:
try:
is_symlink = entry.is_symlink()
except OSError:
# If is_symlink() raises an OSError, consider that
# the entry is not a symbolic link, same behavior
# as os.path.islink()
is_symlink = False
walk_into = not is_symlink

if walk_into:
walk_dirs.append(entry)
if topdown:
for raw_new_path in dirs:
# Path.is_symlink() is used instead of caching entry.is_symlink()
# result during the loop on Path._scandir() because the caller
# can replace the directory entry during the "yield" above.
new_path = self._make_child_relpath(raw_new_path)
if follow_links or not new_path.is_symlink():
yield from new_path._walk(topdown, on_error, follow_links)
else:
for raw_new_path in walk_dirs:
new_path = self._make_child_relpath(raw_new_path.name)
yield from new_path._walk(topdown, on_error, follow_links)
# Yield after recursion if going bottom up
yield self, dirs, nondirs


class PosixPath(Path, PurePosixPath):
"""Path subclass for non-Windows systems.
Expand Down
0