[Python-Dev] Updates to PEP 471, the os.scandir() proposal
python at mrabarnett.plus.com
Thu Jul 10 01:22:21 CEST 2014
On 2014-07-09 23:50, Ethan Furman wrote:
> 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):
>>> 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...
> 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
Should that be "that yields one directory entry at a time"?
> 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
> Return True if this is a directory-type entry; may call os.lstat if the cache is empty.
> Return True if this is a file-type entry; may call os.lstat if the cache is empty.
> Return True if this is a symbolic link; may call os.lstat if the cache is empty.
> Return the stat info for this link; may call os.lstat if the cache is empty.
Why is "is_dir", et al, functions, but "stat" not a function?
> This way both paradigms are supported.
More information about the Python-Dev