[issue44069] pathlib.Path.glob's generator is not a real generator

Terry J. Reedy report at bugs.python.org
Fri May 21 12:32:39 EDT 2021


Terry J. Reedy <tjreedy at udel.edu> added the comment:

I agree that from the outside is seems slightly bizarre to make an internal list to implement a function documented as returning an iterator.  However, list(scandir) was added by Serhiy in #26032 with the comment that it made globbing 1.5-4 times faster.  This is, of course, if one runs the iterator to completion, as is the normal use.

For your presented use case, I suggest something like the following:

next(f for f in scandir(path) if os.path.splitext(f)[1] == '.txt')

----------
nosy: +terry.reedy
versions:  -Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

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


More information about the Python-bugs-list mailing list