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

Ben Hoyt benhoyt at gmail.com
Sat May 11 03:29:00 CEST 2013


> In the python-ideas list there's a thread "PEP: Extended stat_result"
> about adding methods to stat_result.
>
> Using that, you wouldn't necessarily have to look at st.st_mode. The method
> could perform an additional os.stat() if the field was None. For
>
> example:
>
> # Build lists of files and directories in path
> files = []
> dirs = []
> for name, st in os.scandir(path):
>      if st.is_dir():
>          dirs.append(name)
>      else:
>          files.append(name)

That's not too bad. However, the st.is_dir() function could
potentially call os.stat(), so you'd have to be specific about how
errors are handled. Also, I'm not too enthusiastic about how much "API
weight" this would add -- do you need st.is_link() and st.size() and
st.everything_else() as well?

-Ben


More information about the Python-Dev mailing list