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).