2016-11-04 5 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ファイルを開きます。 1つずつ開くと、(class - '_io.TextIOWrapper')がリストに追加されます。Python - 名前で入力ファイルを開く方法

名前でファイルをどのように順番に開くのですか?

Ex。 image1.ppm、image2.ppm、image3.ppm、image4.ppmは私のディレクトリにあります。

コードを読み取って、この順序でファイルを開きます。

image2.ppm

image4.ppm

image1.ppm

image3.ppm

答えて

1

あなたが使用することができますsorted()以上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 
関連する問題