多个文本内容合并一个
准备条件
import os
path = r"C:\\Users\\Administrator\\Desktop\\text\\"
result_file = os.path.join(path, 'result.txt')
filenames = os.listdir(path)
# filenames.sort() # 排序
with open(result_file, 'w') as f:
for i, filename in enumerate(filenames, start=1):
filepath = os.path.join(path, filename)
if filename.lower().endswith(".txt"):
# print(filepath)
with open(filepath) as file:
for line in file:
f.write(line)
print('合并完成!')
效果
查看数据
PS:祝大家新年快乐