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
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
More code review fixes for Path.walk
  • Loading branch information
zmievsa committed Jul 3, 2022
commit 4509797b2b1e774953216893c2c17cfea821585e
4 changes: 1 addition & 3 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -961,9 +961,7 @@ call fails (for example because the path doesn't exist).
Note that the names in the lists contain no path components. To get a full
path (which begins with *self*) to a file or directory in *dirpath*, do
``dirpath / name``. Whether or not the lists are sorted depends on the file
system. If a file or a directory is removed from or added to the *dirpath*
during the generation of *dirnames* and *filenames*, it is uncertain whether
the new entry will appear in the generated lists.
system.

If optional argument *top_down* is ``True`` or not specified, the triple for a
directory is generated before the triples for any of its subdirectories
Expand Down
77 changes: 3 additions & 74 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,84 +1311,11 @@ def expanduser(self):
return self

def walk(self, top_down=True, on_error=None, follow_symlinks=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

dirpath, dirnames, filenames

dirpath is the Path to the directory. dirnames is a list of
the names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists are just names, with no path components.
To get a full path (which begins with top) to a file or directory in
dirpath, do dirpath / name.

If optional arg 'top_down' is true or not specified, the triple for a
directory is generated before the triples for any of its subdirectories
(directories are generated top down). If top_down is false, the triple
for a directory is generated after the triples for all of its
subdirectories (directories are generated bottom up).

When top_down is True, the caller can modify the dirnames list in-place
(e.g., via del or slice assignment), and walk will only recurse
into the subdirectories whose names remain in dirnames; this
can be used to prune the search, or to impose a specific order of
visiting. Modifying dirnames when
top_down is False has no effect on the behavior of Path.walk(), since the
directories in dirnames have already been generated by the time dirnames
itself is generated. No matter the value of top_down, the list of
subdirectories is retrieved before the tuples for the directory and its
subdirectories are generated.

By default errors from Path._scandir() call are ignored. If
optional arg 'on_error' is specified, it should be a callable; 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_symlinks' to true. Unlike os.walk,
Path.walk only adds symbolic links to dirnames if follow_symlinks=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 caller doesn't
either.

Caution: Unlike os.walk, Path.walk assumes the directories have not
been modified between its resumptions. I.e. If a directory from
dirnames has been replaced with a symlink and follow_symlinks=False,
walk will still try to descend into it. To prevent such behavior,
remove directories from dirnames if they have been modified and you
do not want Path.walk to descend into them anymore.

Example:

from pathlib import Path
for root, dirs, files in Path().walk(on_error=print):
print(
root,
"consumes",
sum((root / file).stat().st_size for file in files),
"bytes in",
len(files),
"non-directory files"
)
# don't visit __pycache__ directories
if '__pycache__' in dirs:
dirs.remove('__pycache__')
"""
"""Generate a top-down directory tree from this directory, similar to os.walk()"""
sys.audit("pathlib.Path.walk", self, on_error, follow_symlinks)
return self._walk(top_down, on_error, follow_symlinks)

def _walk(self, top_down, on_error, follow_symlinks):
dirnames = []
filenames = []

# 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
Expand All @@ -1402,6 +1329,8 @@ def _walk(self, top_down, on_error, follow_symlinks):
return

with scandir_it:
dirnames = []
filenames = []
for entry in scandir_it:
try:
is_dir = entry.is_dir(follow_symlinks=follow_symlinks)
Expand Down
0