[Python-Dev] PEP 471 -- os.scandir() function -- a better and faster directory iterator

Victor Stinner victor.stinner at gmail.com
Tue Jul 1 08:55:12 CEST 2014

2014-07-01 4:04 GMT+02:00 Glenn Linderman <v+python at g.nevcal.com>:
>> +0 for stat fields to be None on all platforms unless ensure_lstat=True.
> This won't work well if lstat info is only needed for some entries. Is
> that a common use-case? It was mentioned earlier in the thread.
> If it is, use ensure_lstat=False, and use the proposed (by me) .refresh()
> API to update the data for those that need it.

We should make DirEntry as simple as possible. In Python, the classic
behaviour is to not define an attribute if it's not available on a
platform. For example, stat().st_file_attributes is only available on

I don't like the idea of the ensure_lstat parameter because os.scandir
would have to call two system calls, it makes harder to guess which
syscall failed (readdir or lstat). If you need lstat on UNIX, write:

if hasattr(entry, 'lstat_result'):
    size = entry.lstat_result.st_size
    size = os.lstat(entry.fullname()).st_size


More information about the Python-Dev mailing list