mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.13] gh-127541: Update os.walk example (GH-127765) (GH-131869)
gh-127541: Update os.walk example (GH-127765)
Update os.walk example to be more modern, skip `__pycache__` dirs rather than `CVS` dirs.
(cherry picked from commit a5949986d6
)
Co-authored-by: Totosuki <116938397+totosuki@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
1059b0cc84
commit
9ffa80f21e
2 changed files with 14 additions and 14 deletions
12
Lib/os.py
12
Lib/os.py
|
@ -337,12 +337,12 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
|
|||
|
||||
import os
|
||||
from os.path import join, getsize
|
||||
for root, dirs, files in os.walk('python/Lib/email'):
|
||||
for root, dirs, files in os.walk('python/Lib/xml'):
|
||||
print(root, "consumes ")
|
||||
print(sum(getsize(join(root, name)) for name in files), end=" ")
|
||||
print("bytes in", len(files), "non-directory files")
|
||||
if 'CVS' in dirs:
|
||||
dirs.remove('CVS') # don't visit CVS directories
|
||||
if '__pycache__' in dirs:
|
||||
dirs.remove('__pycache__') # don't visit __pycache__ directories
|
||||
|
||||
"""
|
||||
sys.audit("os.walk", top, topdown, onerror, followlinks)
|
||||
|
@ -468,13 +468,13 @@ if {open, stat} <= supports_dir_fd and {scandir, stat} <= supports_fd:
|
|||
Example:
|
||||
|
||||
import os
|
||||
for root, dirs, files, rootfd in os.fwalk('python/Lib/email'):
|
||||
for root, dirs, files, rootfd in os.fwalk('python/Lib/xml'):
|
||||
print(root, "consumes", end="")
|
||||
print(sum(os.stat(name, dir_fd=rootfd).st_size for name in files),
|
||||
end="")
|
||||
print("bytes in", len(files), "non-directory files")
|
||||
if 'CVS' in dirs:
|
||||
dirs.remove('CVS') # don't visit CVS directories
|
||||
if '__pycache__' in dirs:
|
||||
dirs.remove('__pycache__') # don't visit __pycache__ directories
|
||||
"""
|
||||
sys.audit("os.fwalk", top, topdown, onerror, follow_symlinks, dir_fd)
|
||||
top = fspath(top)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue