[Python-Dev] Updates to PEP 471, the os.scandir() proposal (original) (raw)

Ethan Furman 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 .fullname attributes are present * if info is 'type', scandir ensures the isdir/isfile/issymlink attributes are present and either True or False * if info is 'lstat', scandir additionally ensures a .lstat is present and is a full statresult object * if info is 'os', scandir returns the attributes the OS provides (everything on Windows, only isX -- 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 isdir/isfile/issymlink 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.isdir:" 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.

-- Ethan



More information about the Python-Dev mailing list