GeezerVison is showing github.com — gh-136059: docs: pathlib: Mention that iterdir() is surprisingly not streaming by nh2 · Pull Request #136060 · python/cpython
Conversation
nh2
commented
Jun 28, 2025
•
edited by github-actions
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
This undocumented gotcha can cause excessive memory usage when "iterating" over very large directories.
This is because iterdir() does
entries = list(scandir_it)
Fixing (if at all desired) will likel need least significant amounts of discussion and testing, so first document the behaviour.
See:
📚 Documentation preview 📚: https://cpython-previews--136060.org.readthedocs.build/
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This undocumented gotcha can cause excessive memory usage
when "iterating" over very large directories.
This is because iterdir() does
entries = list(scandir_it)
Fixing (if at all desired) will likel need least significant amounts of
discussion and testing, so first document the behaviour.
|
All commit authors signed the Contributor License Agreement.

|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
If the path is not a directory or otherwise inaccessible, :exc:`OSError` is
|
|
|
raised.
|
|
|
|
|
|
.. warning::
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning and even note directives attract too much attention. We usually avoid using them except in serious cases. Try also to shorten the text and don't describe the current behavior as a bug. For example, somethink like this:
"The contents of the directory are read in one go, but path objects are created during iteration. If you need to reduce latency for large directories, use :func:`os.scandir` instead."
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 30 days with no activity.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
barneygale
Awaiting requested review from barneygale
barneygale is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.