pathlib.Path.glob() currently generates Path objects for intermediate paths that might never be yielded to the user, which is slow and unnecessary. For example, a pattern like **/*.mp3 is evaluated by creating a Path object for every directory visited.
There are already few tricks employed to avoid instantiation, but it would be better if only real results were converted to path objects.
Activity
pythonGH-117586: Speed up `pathlib.Path.glob()` by working with strings
GH-117586: Speed up `pathlib.Path.glob()` by working with strings (#1…
pythonGH-117586: Speed up `pathlib.Path.walk()` by working with strings
GH-117586: Speed up `pathlib.Path.walk()` by working with strings (#1…
pythonGH-117586: Speed up `pathlib.Path.glob()` by working with strin…
pythonGH-117586: Speed up `pathlib.Path.walk()` by working with strin…
papahippo commented on Oct 9, 2026
The documentation doesn't seems to have been updated yet:
3.14.8 Documentation » The Python standard library » File and Directory Access » pathlib — Object-oriented filesystem paths
The above still says that lists of paths are returned.