[Python-Dev] Updates to PEP 471, the os.scandir() proposal
ethan at stoneleaf.us
Wed Jul 9 15:17:40 CEST 2014
On 07/09/2014 05:48 AM, Ben Hoyt wrote:
> So how about tweaking option #2 a tiny bit more to this:
> def scandir(path='.', info=None, onerror=None): ...
> * if info is None (the default), only the .name and .full_name
> attributes are present
> * if info is 'type', scandir ensures the is_dir/is_file/is_symlink
> attributes are present and either True or False
> * if info is 'lstat', scandir additionally ensures a .lstat is present
> and is a full stat_result object
> * if info is 'os', scandir returns the attributes the OS provides
> (everything on Windows, only is_X -- most of the time -- on POSIX)
I would rather have the default for info be 'os': cross-platform is good, but there is no reason to force it on some
poor script that is meant to run on a local machine and will never leave it.
> * if onerror is not None and errors occur during any internal lstat()
> call, onerror(exc) is called with the OSError exception object
As Paul mentioned, 'onerror(exc, DirEntry)' would be better.
> Further point -- because the is_dir/is_file/is_symlink attributes are
> booleans, it would be very bad for them to be present but None if you
> didn't ask for (or the OS didn't return) the type information. Because
> then "if entry.is_dir:" would be None and your code would think it
> wasn't a directory, when actually you don't know. For this reason, all
> attributes should fail with AttributeError if not fetched.
Fair point, and agreed.
More information about the Python-Dev