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

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

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

#先遍历文件名
for filename in filenames:
    filepath = filedir+'/'+filename
    #遍历单个文件,读取行数
    for line in open(filepath,encoding='utf8'):
        f.writelines(line)
    f.write('
')
#关闭文件
f.close()
原文地址:https://www.cnblogs.com/hherbk/p/14119193.html