[Python-Dev] Updates to PEP 471, the os.scandir() proposal

Ethan Furman ethan at stoneleaf.us
Wed Jul 9 23:28:07 CEST 2014


On 07/09/2014 01:57 PM, Paul Moore wrote:
> On 9 July 2014 21:24, Victor Stinner wrote:
>>
>> Example where you may sometimes need is_dir(), but not always
>> ---
>> for entry in os.scandir(path):
>>    if ignore_entry(entry.name):
>>       # this entry is not interesting, lstat_result is useless here
>>       continue
>>    if entry.is_dir():  # fetch required data if needed
>>       continue
>>    ...
>
> That is an extremely good point, and articulates why I've always been
> a bit uncomfortable with the whole ensure_stat idea.

On a system which did not supply is_dir automatically I would write that as:

   for entry in os.scandir(path):  # info defaults to 'os', which is basically None in this case
       if ignore_entry(entry.name):
           continue
       if os.path.isdir(entry.full_name):
           # do something interesting

Not hard to read or understand, no time wasted in unnecessary lstat calls.

--
~Ethan~


More information about the Python-Dev mailing list