Skip to content

Commit 34793ff

Browse files
committed
Move thorough docstring from os.walk to pathlib.Path.walk
pathlib's Path.walk() is generally more pleasant to use, but up to now its docstring didn't describe the functionality except by reference to os.walk. This change makes the docstring more helpful, and points users of the old os.walk() API to pathlib.
1 parent 4238a97 commit 34793ff

2 files changed

Lines changed: 58 additions & 60 deletions

File tree

Lib/os.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -306,65 +306,7 @@ def renames(old, new):
306306
_walk_symlinks_as_files = object()
307307

308308
def walk(top, topdown=True, onerror=None, followlinks=False):
309-
"""Directory tree generator.
310-
311-
For each directory in the directory tree rooted at top (including top
312-
itself, but excluding '.' and '..'), yields a 3-tuple
313-
314-
dirpath, dirnames, filenames
315-
316-
dirpath is a string, the path to the directory. dirnames is a list of
317-
the names of the subdirectories in dirpath (including symlinks to directories,
318-
and excluding '.' and '..').
319-
filenames is a list of the names of the non-directory files in dirpath.
320-
Note that the names in the lists are just names, with no path components.
321-
To get a full path (which begins with top) to a file or directory in
322-
dirpath, do os.path.join(dirpath, name).
323-
324-
If optional arg 'topdown' is true or not specified, the triple for a
325-
directory is generated before the triples for any of its subdirectories
326-
(directories are generated top down). If topdown is false, the triple
327-
for a directory is generated after the triples for all of its
328-
subdirectories (directories are generated bottom up).
329-
330-
When topdown is true, the caller can modify the dirnames list in-place
331-
(e.g., via del or slice assignment), and walk will only recurse into the
332-
subdirectories whose names remain in dirnames; this can be used to prune the
333-
search, or to impose a specific order of visiting. Modifying dirnames when
334-
topdown is false has no effect on the behavior of os.walk(), since the
335-
directories in dirnames have already been generated by the time dirnames
336-
itself is generated. No matter the value of topdown, the list of
337-
subdirectories is retrieved before the tuples for the directory and its
338-
subdirectories are generated.
339-
340-
By default errors from the os.scandir() call are ignored. If
341-
optional arg 'onerror' is specified, it should be a function; it
342-
will be called with one argument, an OSError instance. It can
343-
report the error to continue with the walk, or raise the exception
344-
to abort the walk. Note that the filename is available as the
345-
filename attribute of the exception object.
346-
347-
By default, os.walk does not follow symbolic links to subdirectories on
348-
systems that support them. In order to get this functionality, set the
349-
optional argument 'followlinks' to true.
350-
351-
Caution: if you pass a relative pathname for top, don't change the
352-
current working directory between resumptions of walk. walk never
353-
changes the current directory, and assumes that the client doesn't
354-
either.
355-
356-
Example:
357-
358-
import os
359-
from os.path import join, getsize
360-
for root, dirs, files in os.walk('python/Lib/xml'):
361-
print(root, "consumes ")
362-
print(sum(getsize(join(root, name)) for name in files), end=" ")
363-
print("bytes in", len(files), "non-directory files")
364-
if '__pycache__' in dirs:
365-
dirs.remove('__pycache__') # don't visit __pycache__ directories
366-
367-
"""
309+
"""Walk the directory tree from 'top', similar to pathlib.Path.walk()."""
368310
sys.audit("os.walk", top, topdown, onerror, followlinks)
369311

370312
stack = [fspath(top)]

Lib/pathlib/__init__.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,63 @@ def rglob(self, pattern, *, case_sensitive=None, recurse_symlinks=False):
10781078
return self.glob(pattern, case_sensitive=case_sensitive, recurse_symlinks=recurse_symlinks)
10791079

10801080
def walk(self, top_down=True, on_error=None, follow_symlinks=False):
1081-
"""Walk the directory tree from this directory, similar to os.walk()."""
1081+
"""
1082+
Directory tree generator.
1083+
1084+
For each directory in the directory tree rooted at self (including self, but excluding '.' and '..'), yields a 3-tuple
1085+
1086+
dirpath, dirnames, filenames
1087+
1088+
'dirpath' is a Path, the path to the directory relative to self. 'dirnames'
1089+
is a list of the names of the subdirectories in 'dirpath' (including
1090+
symlinks to directories, and excluding '.' and '..'). 'filenames' is a
1091+
list of the names of the non-directory files in dirpath. Note that the
1092+
names in the lists are just names, with no path components. To get a
1093+
full path (which begins with top) to a file or directory in dirpath, use
1094+
(dirpath / name).
1095+
1096+
If optional arg 'top_down' is true or not specified, the triple for a
1097+
directory is generated before the triples for any of its subdirectories
1098+
(directories are generated top down). If top_down is false, the
1099+
triple for a directory is generated after the triples for all of its
1100+
subdirectories (directories are generated bottom up).
1101+
1102+
When top_down is true, the caller can modify the dirnames list in-place
1103+
(e.g., via del or slice assignment), and walk will only recurse into
1104+
the subdirectories whose names remain in dirnames; this can be used to
1105+
prune the search, or to impose a specific order of visiting. Modifying
1106+
dirnames when top_down is false has no effect on the behavior of walk(),
1107+
since the directories in dirnames have already been generated by the
1108+
time dirnames itself is generated. No matter the value of topdown, the
1109+
list of subdirectories is retrieved before the tuples for the directory
1110+
and its subdirectories are generated.
1111+
1112+
By default errors from the os.scandir() call are ignored. If optional
1113+
arg 'onerror' is specified, it should be a function; it will be called
1114+
with one argument, an OSError instance. It can report the error to
1115+
continue with the walk, or raise the exception to abort the walk.
1116+
Note that the filename is available as the filename attribute of the
1117+
exception object.
1118+
1119+
By default, walk() does not follow symbolic links to subdirectories on
1120+
systems that support them. In order to get this functionality, set the
1121+
optional argument 'follow_symlinks' to true.
1122+
1123+
Caution: if 'self' is relative, don't change the current working
1124+
directory between resumptions of walk. walk never changes the current
1125+
directory, and assumes that the client doesn't either.
1126+
1127+
Example:
1128+
1129+
from pathlib import Path
1130+
for root, dirs, files in Path('python/Lib/xml').walk():
1131+
print(root, "consumes ")
1132+
print(sum((root / name).stat().st_size for name in files), end=" ")
1133+
print("bytes in", len(files), "non-directory files")
1134+
if '__pycache__' in dirs:
1135+
dirs.remove('__pycache__') # don't visit __pycache__ directories
1136+
1137+
"""
10821138
sys.audit("pathlib.Path.walk", self, on_error, follow_symlinks)
10831139
root_dir = str(self)
10841140
if not follow_symlinks:

0 commit comments

Comments
 (0)