python中 如何将多个文件内容合并成一个新文件

 

python中 如何将多个文件内容合并成一个新文件

 

#coding=utf-8
import os
# 目标文件夹的路径
filedir = r'D:\jmeter15\res'
#获取目标文件的文件名称列表  
filenames=os.listdir(filedir)
#打开一个新文件,如果没有则创建
f=open(r'D:\jmeter15\res\1.txt','w+',encoding='utf8')

#先遍历文件名
for filename in filenames:
    filepath = filedir+'/'+filename
    #遍历单个文件,读取行数
    for line in open(filepath,encoding='utf8'):
        f.writelines(line)
    f.write('\n')
#关闭文件
f.close()

 

posted @ 2020-12-11 10:49  learn_py_test  阅读(6446)  评论(0编辑  收藏  举报