https://github.com/python/cpython/commit/b19ad110278344d01a6f8d008c4cda3e07dc50e9
commit: b19ad110278344d01a6f8d008c4cda3e07dc50e9
branch: main
author: Barney Gale <[email protected]>
committer: encukou <[email protected]>
date: 2025-09-03T13:57:01+02:00
summary:
GH-119169: Slightly speed up `os.walk(topdown=True)` (GH-121431)
GH-119186: Slightly speed up `os.walk(topdown=True)`
When `os.walk()` traverses into subdirectories in top-down mode, call
`os.path.join()` once to add a trailing slash, and use string concatenation
thereafter to generate child paths.
files:
A Misc/NEWS.d/next/Library/2024-07-06-14-32-30.gh-issue-119186.E5B1HQ.rst
M Lib/os.py
diff --git a/Lib/os.py b/Lib/os.py
index 12926c832f5ba5..710d6f8cfcdf74 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -417,14 +417,16 @@ def walk(top, topdown=True, onerror=None,
followlinks=False):
# Yield before sub-directory traversal if going top down
yield top, dirs, nondirs
# Traverse into sub-directories
- for dirname in reversed(dirs):
- new_path = join(top, dirname)
- # bpo-23605: os.path.islink() is used instead of caching
- # entry.is_symlink() result during the loop on os.scandir()
because
- # the caller can replace the directory entry during the "yield"
- # above.
- if followlinks or not islink(new_path):
- stack.append(new_path)
+ if dirs:
+ prefix = join(top, top[:0]) # Add trailing slash
+ for dirname in reversed(dirs):
+ new_path = prefix + dirname
+ # bpo-23605: os.path.islink() is used instead of caching
+ # entry.is_symlink() result during the loop on
os.scandir() because
+ # the caller can replace the directory entry during the
"yield"
+ # above.
+ if followlinks or not islink(new_path):
+ stack.append(new_path)
else:
# Yield after sub-directory traversal if going bottom up
stack.append((top, dirs, nondirs))
diff --git
a/Misc/NEWS.d/next/Library/2024-07-06-14-32-30.gh-issue-119186.E5B1HQ.rst
b/Misc/NEWS.d/next/Library/2024-07-06-14-32-30.gh-issue-119186.E5B1HQ.rst
new file mode 100644
index 00000000000000..fbb4f58b59358f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-07-06-14-32-30.gh-issue-119186.E5B1HQ.rst
@@ -0,0 +1,2 @@
+Slightly speed up :func:`os.walk` by calling :func:`os.path.join` less
+often.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]