Skip to content

Optimize pathlib.Path.iterdir() and friends by using os.DirEntry.path #113225

Closed
@barneygale

Description

@barneygale

pathlib.Path.glob() calls os.scandir() under the hood, converting os.DirEntry objects to path objects using a private _make_child_relpath() method. This builds a child path from a given basename. The basename is obtained from the dir entry's name attribute.

I've just spotted (or realised) that dir entries have a path attribute that we could use, rather than constructing our own string path from the basename. This seems to be a fair bit faster in my testing.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions