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

StackOverflow https://stackoverflow.com/questions/18599836

  •  27-06-2022
  •  | 
  •  

Вопрос

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 ()
Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top