Skip to content

Commit bed850e

Browse files
Apply suggestions from code review
Co-authored-by: Brett Cannon <[email protected]>
1 parent 79cf8fd commit bed850e

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

Doc/library/pathlib.rst

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ call fails (for example because the path doesn't exist).
953953

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

958958
*dirpath* is a :class:`Path` to the directory currently being walked,
959959
*dirnames* is a list of strings for the names of subdirectories in *dirpath*
@@ -963,27 +963,27 @@ call fails (for example because the path doesn't exist).
963963
``dirpath / name``. Whether or not the lists are sorted is file
964964
system-dependent.
965965

966-
If optional argument *top_down* is true or not specified, the triple for a
966+
If the optional argument *top_down* is true (which is the default), the triple for a
967967
directory is generated before the triples for any of its subdirectories
968968
(directories are walked top-down). If *top_down* is false, the triple
969969
for a directory is generated after the triples for all of its subdirectories
970970
(directories are walked bottom-up). No matter the value of *top_down*, the
971-
list of subdirectories is retrieved before the tuples for the directory and
971+
list of subdirectories is retrieved before the triples for the directory and
972972
its subdirectories are walked.
973973

974974
When *top_down* is true, the caller can modify the *dirnames* list in-place
975-
(For example, using :keyword:`del` or slice assignment), and :meth:`Path.walk`
976-
will only recurse into the subdirectories whose names remain in *dirnames*;
977-
this can be used to prune the search, or to impose a specific order of visiting,
975+
(for example, using :keyword:`del` or slice assignment), and :meth:`Path.walk`
976+
will only recurse into the subdirectories whose names remain in *dirnames*.
977+
This can be used to prune the search, or to impose a specific order of visiting,
978978
or even to inform :meth:`Path.walk` about directories the caller creates or
979979
renames before it resumes :meth:`Path.walk` again. Modifying *dirnames* when
980-
*top_down* is false has no effect on the behavior of :meth:`Path.walk()`, since the
980+
*top_down* is false has no effect on the behavior of :meth:`Path.walk()` since the
981981
directories in *dirnames* have already been generated by the time *dirnames*
982982
is yielded to the caller.
983983

984984
By default, errors from :func:`os.scandir` are ignored. If the optional
985985
argument *on_error* is specified, it should be a callable; it will be
986-
called with one argument, an :exc:`OSError` instance. It can handle the
986+
called with one argument, an :exc:`OSError` instance. The callable can handle the
987987
error to continue the walk or re-raise it to stop the walk. Note that the
988988
filename is available as the ``filename`` attribute of the exception object.
989989

@@ -999,20 +999,19 @@ call fails (for example because the path doesn't exist).
999999
does not keep track of the directories it has already visited.
10001000

10011001
.. note::
1002-
:meth:`Path.walk` assumes the directories it walks are not been modified during
1002+
:meth:`Path.walk` assumes the directories it walks are not modified during
10031003
execution. For example, if a directory from *dirnames* has been replaced
10041004
with a symlink and *follow_symlinks* is false, :meth:`Path.walk` will
10051005
still try to descend into it. To prevent such behavior, remove directories
10061006
from *dirnames* as appropriate.
10071007

10081008
.. note::
10091009

1010-
Unlike :func:`os.walk`, :meth:`Path.walk` lists symlinks to directories into
1010+
Unlike :func:`os.walk`, :meth:`Path.walk` lists symlinks to directories in
10111011
*filenames* if *follow_symlinks* is false.
10121012

10131013
This example displays the number of bytes used by all files in each directory,
1014-
while ignoring ``__pycache__`` directories.
1015-
:file:`__pycache__` subdirectory::
1014+
while ignoring ``__pycache__`` directories::
10161015

10171016
from pathlib import Path
10181017
for root, dirs, files in Path("cpython/Lib/concurrent").walk(on_error=print):

Lib/test/test_pathlib.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,15 +2479,6 @@ def test_complex_symlinks_relative_dot_dot(self):
24792479
self._check_complex_symlinks(os.path.join('dirA', '..'))
24802480

24812481
class WalkTests(unittest.TestCase):
2482-
"""Tests for Path.walk()
2483-
2484-
They are mostly just copies of os.walk tests converted to use Paths
2485-
because os.walk tests are already mature and cover many edge cases
2486-
that we could miss writing new tests.
2487-
2488-
It is not combined with Path* tests because its setup is aimed at
2489-
testing a bit more complex cases than Path's setup.
2490-
"""
24912482

24922483
def setUp(self):
24932484
P = pathlib.Path
@@ -2526,7 +2517,6 @@ def setUp(self):
25262517
broken_link2_path = self.sub2_path / "broken_link2"
25272518
broken_link3_path = self.sub2_path / "broken_link3"
25282519

2529-
# Create stuff.
25302520
os.makedirs(self.sub11_path)
25312521
os.makedirs(self.sub2_path)
25322522
os.makedirs(sub21_path)
@@ -2548,7 +2538,7 @@ def setUp(self):
25482538
self.sub2_tree = (self.sub2_path, ["SUB21"], ["tmp3"])
25492539

25502540
if not is_emscripten:
2551-
# Emscripten fails with inaccessible directory
2541+
# Emscripten fails with inaccessible directories.
25522542
os.chmod(sub21_path, 0)
25532543
try:
25542544
os.listdir(sub21_path)
@@ -2561,7 +2551,6 @@ def setUp(self):
25612551
del self.sub2_tree[1][:1]
25622552

25632553
def test_walk_topdown(self):
2564-
# Walk top-down.
25652554
P = pathlib.Path
25662555
all = list(self.walk_path.walk())
25672556

@@ -2585,7 +2574,6 @@ def test_walk_prune(self, walk_path=None):
25852574
all = []
25862575
for root, dirs, files in walk_path.walk():
25872576
all.append((root, dirs, files))
2588-
# Don't descend into SUB1.
25892577
if 'SUB1' in dirs:
25902578
# Note that this also mutates the dirs we appended to all!
25912579
dirs.remove('SUB1')
@@ -2601,7 +2589,6 @@ def test_file_like_path(self):
26012589
self.test_walk_prune(FakePath(self.walk_path).__fspath__())
26022590

26032591
def test_walk_bottom_up(self):
2604-
# Walk bottom-up.
26052592
all = list(self.walk_path.walk( top_down=False))
26062593

26072594
self.assertEqual(len(all), 4, all)
@@ -2623,7 +2610,6 @@ def test_walk_bottom_up(self):
26232610

26242611
@os_helper.skip_unless_symlink
26252612
def test_walk_follow_symlinks(self):
2626-
# Walk, following symlinks.
26272613
walk_it = self.walk_path.walk(follow_symlinks=True)
26282614
for root, dirs, files in walk_it:
26292615
if root == self.link_path:
@@ -2634,14 +2620,15 @@ def test_walk_follow_symlinks(self):
26342620
self.fail("Didn't follow symlink with follow_symlinks=True")
26352621

26362622
def test_walk_symlink_location(self):
2637-
""" Tests whether symlinks end up in filenames or dirnames depending
2638-
on the `follow_symlinks` argument
2639-
"""
2623+
# Tests whether symlinks end up in filenames or dirnames depending
2624+
# on the `follow_symlinks` argument.
26402625
walk_it = self.walk_path.walk(follow_symlinks=False)
26412626
for root, dirs, files in walk_it:
26422627
if root == self.sub2_path:
26432628
self.assertIn("link", files)
26442629
break
2630+
else:
2631+
self.fail("symlink not found")
26452632

26462633
walk_it = self.walk_path.walk(follow_symlinks=True)
26472634
for root, dirs, files in walk_it:
@@ -2650,7 +2637,6 @@ def test_walk_symlink_location(self):
26502637
break
26512638

26522639
def test_walk_bad_dir(self):
2653-
# Walk top-down.
26542640
errors = []
26552641
walk_it = self.walk_path.walk(on_error=errors.append)
26562642
root, dirs, files = next(walk_it)

0 commit comments

Comments
 (0)