Am 10.05.2013 12:55, schrieb Ben Hoyt:
Higher-level functions like os.walk() would then check the fields they needed are not None, and only call os.stat() if needed, for example:
# Build lists of files and directories in path files = [] dirs = [] for name, st in os.scandir(path): if st.st_mode is None: st = os.stat(os.path.join(path, name)) if stat.S_ISDIR(st.st_mode): dirs.append(name) else: files.append(name)
Have you actually tried the code? It can't give you correct answers. The struct dirent.d_type member as returned by readdir() has different values than stat.st_mode's file type. For example on my system readdir() returns DT_DIR for a directory but S_ISDIR() checks different bits: DT_DIR = 4 S_ISDIR(mode) ((mode) & 0170000) == 0040000 Or are you proposing to map d_type to st_mode? That's also problematic because st_mode would only have file type bits, not permission bits. Also POSIX standards state that new file types will not get additional S_IF* constant assigned to. Some operation systems have IFTODT() / DTTOIF() macros which convert bits between st_mode and d_type but the macros aren't part of POSIX standard. Hence I'm +1 on the general idea but -1 on something stat like. IMHO os.scandir() should yield four objects: * name * inode * file type or DT_UNKNOWN * stat_result or None stat_result shall only be returned when the operating systems provides a full stat result as returned by os.stat(). Christian