【发布时间】:2014-02-12 21:08:53
【问题描述】:
我在一个文件夹中有许多文件,它们的名称遵循约定:
0.1.txt, 0.15.txt, 0.2.txt, 0.25.txt, 0.3.txt, ...
我需要一一阅读并操作其中的数据。目前我使用以下命令打开每个文件:
import os
# This is the path where all the files are stored.
folder path = '/home/user/some_folder/'
# Open one of the files,
for data_file in os.listdir(folder_path):
...
不幸的是,这会以没有特定顺序读取文件(不确定如何选择它们),我需要从文件名最小编号的文件开始读取它们,然后是编号较大的文件,依此类推,直到最后一个。
【问题讨论】:
-
您需要使用自然排序对文件进行排序。