【发布时间】:2016-05-03 19:10:59
【问题描述】:
我有这样的文件名:savedrecs(1)、savedrecs(2)...savedrecs(11) 等,我使用代码:
import sys
import os
import glob
directory = ["A", "B", "C", "D", "E"]
for folder in directory:
for file in glob.glob(os.path.join(folder, "savedrecs(*).txt")):
print file
with open(file) as f:
a=sum(not line.strip() for line in f)
print a
这里,(*) 代表文件编号。但是,当我运行代码时,结果分别对应编号为 1,11,12,13,2,3,4 的文件,而不是这个首选顺序:1,2,3,4,(...), 11,12,13。有没有办法可以将 * 定义为整数,然后以某种方式使其适应此代码?
【问题讨论】:
-
也许您可以将其保留为元组
(11, "savedrecs(11).txt")并对元组进行排序。
标签: python list python-2.7 sorting integer