[Python-Dev] PEP 471 "scandir" accepted

Ben Hoyt benhoyt at gmail.com
Tue Jul 22 17:52:45 CEST 2014


> Note: listdir() accepts an integer path (an open file descriptor that
> refers to a directory) that is passed to fdopendir() on POSIX [4] i.e.,
> *you can't use scandir() to replace listdir() in this case* (as I've
> already mentioned in [1]). See the corresponding tests from [2].
>
> [1] https://mail.python.org/pipermail/python-dev/2014-July/135296.html
> [2] https://mail.python.org/pipermail/python-dev/2014-June/135265.html
>
> From os.listdir() docs [3]:
>
>> This function can also support specifying a file descriptor; the file
>> descriptor must refer to a directory.
>
> [3] https://docs.python.org/3.4/library/os.html#os.listdir
> [4] http://hg.python.org/cpython/file/3.4/Modules/posixmodule.c#l3736

Fair point.

Yes, I hadn't realized listdir supported dir_fd (must have been
looking at 2.x docs), though you've pointed it out at [1] above. and I
guess I wasn't thinking about implementation at the time.

It would be easy enough (I think) to have the helper function support
both, but raise an error in the scandir() function if the type of path
is an integer.

However, given that we have to support this for listdir() anyway, I
think it's worth reconsidering whether scandir()'s directory argument
can be an integer FD. Given that listdir() already supports it, it
will almost certainly be asked for later anyway for someone who's
porting some listdir code that uses an FD. Thoughts, Victor?

-Ben


More information about the Python-Dev mailing list