◐ Shell
reader mode source ↗
Skip to content

GH-119169: Slightly speed up os.walk(topdown=True)#121431

Merged
encukou merged 3 commits into
python:mainfrom
barneygale:gh-119186-joining
Sep 3, 2025
Merged

GH-119169: Slightly speed up os.walk(topdown=True)#121431
encukou merged 3 commits into
python:mainfrom
barneygale:gh-119186-joining

Conversation

@barneygale

@barneygale barneygale commented Jul 6, 2024

Copy link
Copy Markdown
Contributor

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.

(this trick is already used in os.fwalk())

$ ./python -m timeit -s 'import os' 'list(os.walk("."))'
5 loops, best of 5: 41.4 msec per loop
5 loops, best of 5: 40.7 msec per loop
# --> 1.7% faster

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.
@barneygale barneygale added the performance Performance or resource usage label Jul 6, 2024
@barneygale barneygale changed the title GH-119186: Slightly speed up os.walk(topdown=True) Jul 6, 2024
Hide details View details @encukou encukou merged commit b19ad11 into python:main Sep 3, 2025
45 checks passed
lkollar pushed a commit to lkollar/cpython that referenced this pull request Sep 9, 2025
…121431)

pythonGH-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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants