Python: where is the method listdir because there is no "def listdir()" in module os.py?

938 views Asked by At

Python: I wonder where this method listdir because he is not in the module os.py. In this module there is no a method:

def listdir ()
1

There are 1 answers

0
lmjohns3 On BEST ANSWER

The listdir method is actually implemented in a C module, and is imported dynamically depending on the operating system environment. You can see the imports near the top of os.py, in blocks like this :

if 'posix' in _names:
    name = 'posix'
    linesep = '\n'
    from posix import *

Then the file posixmodule.c in the Python source has the POSIX implementation of listdir : https://github.com/python-git/python/blob/master/Modules/posixmodule.c#L2068 (and likewise for other OSes).