[New-bugs-announce] [issue37935] Improve performance of pathlib.scandir()

Shai report at bugs.python.org
Sat Aug 24 02:31:41 EDT 2019


New submission from Shai <shaia1112011 at gmail.com>:

I recently have taken a look in the source code of the pathlib module, and I saw something weird there:

when the module used the scandir function, it first converted its iterator into a list and then used it in a for loop. The list wasn't used anywhere else, so I think the conversion to list is just a waste of performance.

In addition, I noticed that the scandir iterator is never closed (it's not used in a with statement and its close method isn't called). I know that the iterator is closed automatically when it's garbaged collected, but according to the docs, it's advisable to close it explicitly.

I've created a pull request that fixes these issues:
PR 15331

In the PR, I changed the code so the scandir iterator is used directly instead of being converted into a list and I wrapped its usage in a with statement to close resources properly.

----------
components: Library (Lib)
messages: 350354
nosy: Shai
priority: normal
pull_requests: 15142
severity: normal
status: open
title: Improve performance of pathlib.scandir()
type: performance
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37935>
_______________________________________


More information about the New-bugs-announce mailing list