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 8000
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
Apply suggestions from code review
Co-authored-by: Brett Cannon <brett@python.org>
  • Loading branch information
Ovsyanka83 and brettcannon authored Jul 9, 2022
commit 15d96b99db69e65f6210f813335fab808bdeda12
55 changes: 27 additions & 28 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -952,24 +952,24 @@ call fails (for example because the path doesn't exist).
either top-down or bottom-up.

For each directory in the directory tree rooted at *self* (including
*self* but excluding '.' and '..'), yields a 3-tuple
*self* but excluding '.' and '..'), the method yields a 3-tuple of
``(dirpath, dirnames, filenames)``

*dirpath* is a :class:`Path` to the directory, *dirnames* is a list of the names
of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``), and
*filenames* is a list of the names of the non-directory files in *dirpath*.
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.
*dirpath* is a :class:`Path` to the directory currently being walked,
*dirnames* is a list of strings for the names of subdirectories in *dirpath*
(excluding ``'.'`` and ``'..'``), and *filenames* is a list of strings for
the names of the non-directory files in *dirpath*. 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 is file
system-dependent.

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
(directories are generated top-down). If *top_down* is ``False``, the triple
(directories are walked 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). No matter the value of *top_down*, the
list of subdirectories is retrieved before the tuples for the directory and
its subdirectories are generated.
its subdirectories are walked.

When *top_down* is True, the caller can modify the *dirnames* list in-place
(For example, using :keyword:`del` or slice assignment), and :meth:`Path.walk`
Expand All @@ -981,42 +981,41 @@ call fails (for example because the path doesn't exist).
directories in *dirnames* have already been generated by the time *dirnames*
is yielded to the caller.

By default errors from :func:`os.scandir` call are ignored. If
By default, errors from :func:`os.scandir` are ignored. If the
optional argument *on_error* is specified, it should be a callable; it
will be called with one argument, an :exc:`OSError` instance. It can
report the error to continue with the walk, or raise the exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "report the error to continue the walk"? Do you mean suppress/consume the exception, or else re-raise it?

Copy link
Contributor Author
@zmievsa zmievsa Jul 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly that. Please, take a look at these lines again. I tried to clarify and simplify it a little bit.

to abort the walk. Note that the filename is available as the
``filename`` attribute of the exception object.

By default, :meth:`Path.walk` will not walk down into symbolic links that
By default, :meth:`Path.walk` will not follow symbolic links that
resolve to directories. Set *follow_symlinks* to ``True`` to visit directories
pointed to by symlinks, on systems that support them.
pointed to by symlinks (where supported).

.. note::

Be aware that setting *follow_symlinks* to ``True`` can lead to infinite
recursion if a link points to a parent directory of itself. :meth:`Path.walk`
does not keep track of the directories it visited already.
does not keep track of the directories it has already visited.

.. note::
This method assumes that the current working directory is not changing while
This method assumes that the current working directory does not change while
walking relative paths.
.. note::

:meth:`Path.walk` assumes the directories have not been modified between
its resumptions. For example, if a directory from *dirnames* has been replaced
.. note::
:meth:`Path.walk` assumes the directories it walks are not been modified during
execution. For example, if a directory from *dirnames* has been replaced
with a symlink and *follow_symlinks* = ``False``, :meth:`Path.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 to
descend into them anymore.
from *dirnames* as appropriate.

.. note::

Unlike :func:`os.walk`, :meth:`Path.walk` adds symlinks to directories into *filenames*
if *follow_symlinks* is ``True``
Unlike :func:`os.walk`, :meth:`Path.walk` lists symlinks to directories into
*filenames* if *follow_symlinks* is ``True``.

This example displays the number of bytes taken by non-directory files in each
directory under the starting directory, except that it doesn't look under any
This example displays the number of bytes used by all files in each directory,
while ignoring `__pycache__` directories.
:file:`__pycache__` subdirectory::

from pathlib import Path
Expand All @@ -1032,14 +1031,14 @@ call fails (for example because the path doesn't exist).
if '__pycache__' in dirs:
dirs.remove('__pycache__')

In the next example (simple implementation of :func:`shutil.rmtree`),
walking the tree bottom-up is essential, :func:`rmdir` doesn't allow
deleting a directory before the directory is empty::
This next example is asimple implementation of :func:`shutil.rmtree`.
Walking the tree bottom-up is essential as :func:`rmdir` doesn't allow
deleting a directory before it is empty::

# Delete everything reachable from the directory "top",
# assuming there are no symbolic links.
# CAUTION: This is dangerous! For example, if top == Path('/'),
# it could delete all your disk files.
# it could delete all of your files.
for root, dirs, files in top.walk(topdown=False):
for name in files:
(root / name).unlink()
Expand Down
0