Movatterモバイル変換


[0]ホーム

URL:


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

Nick Coghlanncoghlan at gmail.com
Sat May 11 16:34:14 CEST 2013


On Sat, May 11, 2013 at 2:24 PM, Ben Hoyt <benhoyt at gmail.com> wrote:> In all the *practical* examples I've seen (and written myself), I> iterate over a directory and I just need to know whether it's a file> or directory (or maybe a link). Occassionally you need the size as> well, but that would just mean a similar check "if st.st_size is None:> st = os.stat(...)", which on Linux/OS X would call stat(), but it'd> still be free and fast on Windows.Here's the full set of fields on a current stat object:st_atimest_atime_nsst_blksizest_blocksst_ctimest_ctime_nsst_devst_gidst_inost_modest_mtimest_mtime_nsst_nlinkst_rdevst_sizest_uidDo we really want to publish an object with all of those as attributespotentially set to None, when the abstraction we're trying to presentis intended primarily for the benefit of os.walk?And if we're creating a custom object instead, why return a 2-tuplerather than making the entry's name an attribute of the custom object?To me, that suggests a more reasonable API for os.scandir() might befor 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)On all platforms, the query methods would not require a separatestat() call. On Windows, cached_stat would be populated with a fullstat 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.If we find other details that we can reliably provide cross-platformfrom the dir information, then we can add more query methods orattributes to the dir_entry object.Cheers,Nick.--Nick Coghlan   |ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2026 Movatter.jp