[Python-Dev] Issue 11406: adding os.scandir(), a directory iterator returning stat-like info

Christian Heimes christian at python.org
Sat May 11 17:42:39 CEST 2013


Am 11.05.2013 16:34, schrieb Nick Coghlan:
> Here's the full set of fields on a current stat object:
> 
> st_atime
> st_atime_ns
> st_blksize
> st_blocks
> st_ctime
> st_ctime_ns
> st_dev
> st_gid
> st_ino
> st_mode
> st_mtime
> st_mtime_ns
> st_nlink
> st_rdev
> st_size
> st_uid

And there are more fields on some platforms, e.g. st_birthtime.

> To me, that suggests a more reasonable API for os.scandir() might be
> for it to be an iterator over "dir_entry" objects:
> 
>     name (as a string)
>     is_file()
>     is_dir()
>     is_link()
>     stat()
>     cached_stat (None or a stat object)

I suggest that we call it .lstat() and .cached_lstat to make clear that
we are talking about no-follow stat() here. On platforms that support
fstatat() it should use fstatat(dir_fd, name, &buf, AT_SYMLINK_NOFOLLOW)
where dir_fd is the fd from dirfd() of opendir()'s return value.

> On all platforms, the query methods would not require a separate
> stat() call. On Windows, cached_stat would be populated with a full
> stat object when scandir builds the entry. On non-Windows platforms,
> cached_stat would initially be None, and you would have to call stat()
> to populate it.

+1

> If we find other details that we can reliably provide cross-platform
> from the dir information, then we can add more query methods orst
> attributes to the dir_entry object.

I'd like to see d_type and d_ino, too. d_type should default to
DT_UNKNOWN, d_ino to None.

Christian




More information about the Python-Dev mailing list