2016-11-04 1 views
0
count = 0 
path = input("Please enter the directory you want to get the files from. -> ") 
for filename in glob.glob(os.path.join(path, '*.ppm')): 
    file_obj = open(filename, "r", encoding="utf-8") 
    list_of_files.append(file_obj) 
    count += 1 

이 코드는 사용자가 지정한 디렉토리를 검색하고 해당 디렉토리에있는 .ppm 파일을 모두 열어서 발생합니다. 하나씩 열 때마다 (class - '_io.TextIOWrapper')를 목록에 추가합니다.Python - 이름으로 입력 파일을 여는 방법

사람이 파일을 이름순으로 어떻게 열 수 있습니까?

Ex. image1.ppm, image2.ppm, image3.ppm, image4.ppm은 내 디렉토리에 있습니다.

코드는 읽고 순서로 파일을 엽니 다

image2.ppm

image4.ppm

image1.ppm

image3.ppm

답변

1

당신은 사용할 수 있습니다 이상 glob.glob(os.path.join(path, '*.ppm')).

count = 0 
path = input("Please enter the directory you want to get the files from. -> ") 
for filename in sorted(glob.glob(os.path.join(path, '*.ppm'))): 
    file_obj = open(filename, "r", encoding="utf-8") 
    list_of_files.append(file_obj) 
    count += 1