需求:有多个日志文件,一个一个打开比较麻烦,希望把多个日志文件合并在一起
并且加上序号,以及文件的名称,例如:
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 22 15:50:13 2019
@author: lizhenzhu
"""
from os import listdir
dirPath = "C:/Users/lizhenzhu/Desktop/python/log"
files=listdir('C:/Users/lizhenzhu/Desktop/python/log')
res = ""
i = 0
for file in files:
if file.endswith(".log"):
i += 1
title = "第%s个 %s" % (i, file[0:len(file)-4])
with open("C:/Users/lizhenzhu/Desktop/python/log/" + file, "r", encoding='utf-8') as file:
content = file.read()
file.close()
append = "\n%s\n\n%s" % (title, content)
res += append
with open("C:/Users/lizhenzhu/Desktop/python/log/aaa_dag_all.log", "w", encoding='utf-8' ) as outFile :
outFile.write(res)
outFile.close()
print(len(res))