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

Ethan Furman ethan at stoneleaf.us
Thu Jul 10 00:50:28 CEST 2014


On 07/09/2014 02:33 PM, Ben Hoyt wrote:
>>
>> On a system which did not supply is_dir automatically I would write that as:
>>
>>    for entry in os.scandir(path):
>>        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.
>
> No, but how do you know whether you're on "a system which did not
> supply is_dir automatically"? The above is not cross-platform, or at
> least, not efficient cross-platform, which defeats the whole point of
> scandir -- the above is no better than listdir().

Hit a directory with 100,000 entries and you'll change your mind.  ;)

Okay, so the issue is you /want/ to write an efficient, cross-platform routine...

hrmmm.....

thinking........

Okay, marry the two ideas together:

   scandir(path, info=None, onerror=None)
       """
       Return a generator that returns one directory entry at a time in a DirEntry object
       info:  None --> DirEntries will have whatever attributes the O/S provides
              'type'  --> DirEntries will already have at least the file/dir distinction
              'stat'  --> DirEntries will also already have stat information
       """

   DirEntry.is_dir()
      Return True if this is a directory-type entry; may call os.lstat if the cache is empty.

   DirEntry.is_file()
      Return True if this is a file-type entry; may call os.lstat if the cache is empty.

   DirEntry.is_symlink()
      Return True if this is a symbolic link; may call os.lstat if the cache is empty.

   DirEntry.stat
      Return the stat info for this link; may call os.lstat if the cache is empty.


This way both paradigms are supported.

--
~Ethan~


More information about the Python-Dev mailing list