Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python에서 이름별로 정렬된 디렉토리 목록을 얻는 방법은 무엇입니까?

<시간/>

os.listdir 함수를 호출하여 디렉토리 내용 목록을 가져오고 sorted 함수를 사용하여 이 목록을 정렬할 수 있습니다.

예를 들어

>>> import os
>>> list_dir = os.listdir('.')
>>> list_dir = [f.lower() for f in list_dir]   # Convert to lower case
>>> sorted(list_dir)
['dlls', 'doc', 'etc', 'include', 'lib', 'libs', 'license.txt', 'news.txt', 'python.exe', 'pythonw.exe', 'readme.txt', 'scripts', 'share', 'tcl', 'tools', 'w9xpopen.exe']