[Python-Dev] PEP 471 "scandir" accepted
4kir4.1i at gmail.com
Tue Jul 22 09:33:41 CEST 2014
Ben Hoyt <benhoyt at gmail.com> writes:
> I think if I were doing this from scratch I'd reimplement listdir() in
> Python as "return [e.name for e in scandir(path)]".
> So my basic plan is to have an internal helper function in
> posixmodule.c that either yields DirEntry objects or strings. And then
> listdir() would simply be defined something like "return
> list(_scandir(path, yield_strings=True))" in C or in Python.
> My reasoning is that then there'll be much less (if any) code
> duplication between scandir() and listdir().
> Does this sound like a reasonable approach?
Note: listdir() accepts an integer path (an open file descriptor that
refers to a directory) that is passed to fdopendir() on POSIX  i.e.,
*you can't use scandir() to replace listdir() in this case* (as I've
already mentioned in ). See the corresponding tests from .
>From os.listdir() docs :
> This function can also support specifying a file descriptor; the file
> descriptor must refer to a directory.
More information about the Python-Dev