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

MRAB 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 isdir automatically I would write that as: for entry in os.scandir(path): if ignoreentry(entry.name): continue if os.path.isdir(entry.fullname): # 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 isdir 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

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 """

DirEntry.isdir() Return True if this is a directory-type entry; may call os.lstat if the cache is empty. DirEntry.isfile() Return True if this is a file-type entry; may call os.lstat if the cache is empty. DirEntry.issymlink() 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. Why is "is_dir", et al, functions, but "stat" not a function?

This way both paradigms are supported.



More information about the Python-Dev mailing list