[Python-Dev] PEP 471 -- os.scandir() function -- a better and faster directory iterator
Eric V. Smith
eric at trueblade.com
Tue Jul 1 04:59:33 CEST 2014
On 6/30/2014 10:17 PM, Nick Coghlan wrote:
> On 30 Jun 2014 19:13, "Glenn Linderman" <v+python at g.nevcal.com
> <mailto:v%2Bpython at g.nevcal.com>> wrote:
>> If it is, use ensure_lstat=False, and use the proposed (by me)
> .refresh() API to update the data for those that need it.
> I'm -1 on a refresh API for DirEntry - just use pathlib in that case.
I'm not sure refresh() is the best name, but I think a
"get_stat_info_from_direntry_or_call_stat()" (hah!) makes sense. If you
really need the stat info, then you can write simple code like:
for entry in os.scandir(path):
mtime = entry.get_stat_info_from_direntry_or_call_stat().st_mtime
And it won't call stat() any more times than needed. Once per file on
Posix, zero times per file on Windows.
Without an API like this, you'll need a check in the application code on
whether or not to call stat().
More information about the Python-Dev