#############################合并多个txt文件为一个txt文件
import os
dirPath = "/media/pc" #所有txt位于的文件夹路径
files = os.listdir(dirPath)
res = ""
i = 0
for file in files:
if file.endswith(".txt"):
i += 1
title = "第%s章 %s" % (i, file[0:len(file)-4])
with open("/media/pc/" + file, "r", encoding='utf-8') as file:
content = file.read()
file.close()
append = "\n%s\n\n%s" % (title,content)
# append = "\n%s\n\n%s" % (title, content)
res += append
with open("/media/zong.txt", "w", encoding='utf-8') as outFile:
outFile.write(res)
outFile.close()
print(len(res))
##############################对合并的txt内容进行排序
b=[]
count = 1
all_count = 1
filename = "/media/zong.txt"
output_file='/media/d.txt'
country_list = []
with open(filename, "r") as ins: #打开文件
for line in ins: #循环文件的每一行
a = line[2:-5]
b.append(a)
b.sort()
writer = open(output_file, 'w')
writer.write('\n'.join('%s' %id for id in b))
writer.close()