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

Victor Stinner victor.stinner at gmail.com
Wed Jul 9 15:05:05 CEST 2014


2014-07-08 22:09 GMT+02:00 Ben Hoyt <benhoyt at gmail.com>:

I think you're misunderstanding isdir() and isfile(), as these don't actually call os.stat(). All DirEntry methods either call nothing or os.lstat() to get the stat info on the entry itself (not the destination of the symlink).

Oh. Extract of your PEP: "isdir(): like os.path.isdir(), but much cheaper". genericpath.isdir() and genericpath.isfile() use os.stat(), whereas posixpath.islink() uses os.lstat(). Is it a mistake in the PEP? Ah, you're dead right -- this is basically a bug in the PEP, as DirEntry.isdir() is not like os.path.isdir() in that it is based on the entry itself (like lstat), not following the link. I'll improve the wording here and update the PEP.

Ok, so it means that your example grouping files per type, files and directories, is also wrong. Or at least, it behaves differently than os.walk(). You should put symbolic links to directories in the "dirs" list too.

if entry.is_dir(): # is_dir() checks os.lstat() dirs.append(entry) elif entry.is_symlink() and os.path.isdir(entry): # isdir() checks os.stat() dirs.append(entry) else: non_dirs.append(entry)

Victor



More information about the Python-Dev mailing list