Skip to content

gh-136059: docs: pathlib: Mention that iterdir() is surprisingly not streaming - #136060

Open
nh2 wants to merge 1 commit into
python:mainfrom
nh2:patch-1
Open

gh-136059: docs: pathlib: Mention that iterdir() is surprisingly not streaming#136060
nh2 wants to merge 1 commit into
python:mainfrom
nh2:patch-1

Conversation

@nh2

@nh2 nh2 commented Jun 28, 2025

Copy link
Copy Markdown

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/

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.
@python-cla-bot

python-cla-bot Bot commented Jun 28, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Comment thread Doc/library/pathlib.rst
If the path is not a directory or otherwise inaccessible, :exc:`OSError` is
raised.

.. warning::

Copy link
Copy Markdown
Member

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."

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news stale Stale PR or inactive for long period of time.

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants