# 'C:\Users\SAM\Desktop\数据竞赛\个人征信_1108\个人征信\train\bank_detail_train.txt' # 反斜杠的写法会报编码错误 f=open('C:/Users/SAM/Desktop/数据竞赛/个人征信_1108/个人征信/train/bank_detail_train.txt') result= [] iter_f=iter(f) #用迭代器循环访问文件中的每一行 for line in iter_f: result.append(line) f.close() result.sort() f=open('C:/Users/SAM/Desktop/数据竞赛/个人征信_1108/个人征信/train/bank_detail_train.txt','w') f.writelines(result) f.close()