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