环境 win7 先来new一点log 日记 日记包含 "reason=", "error=" 两个log级别
存放在D盘下得LOG目录下 先来 生成一年LOG 2017-**-**.log 日记 conent 随机
#coding: utf-8
import datetime,string,random
#wret_conent
def wretlog():
rea = ["reason=", "error="]
str = ""
for i in range(random.randint(1, 10)):
con = random.choice(string.ascii_letters)
str += con
strn = rea[random.randint(0, 1)] + str
return strn
#new DayLog
def NewDateName():
date_list = []
begin_date = datetime.datetime.strptime('2017-01-01', "%Y-%m-%d")
end_date = datetime.datetime.strptime('2017-12-01', "%Y-%m-%d")
while begin_date <= end_date:
date_str = begin_date.strftime("%m%d")
date_list.append("2017"+date_str+'.log')
begin_date += datetime.timedelta(days=1)
return date_list
#netLogFile
def newFile():
for a in NewDateName():
logfile=open(r'D:\log\\'+str(a),'w+')
for i in range(random.randint(30,50)):
logfile.writelines(wretlog()+ '\n')
logfile.flush()
if __name__ == "__main__":
newFile()
好了现在开始统记LOG error log:
#coding: utf-8
import datetime
#日期
def Newday(inpDate,outDate):
date_list = []
begin_date = datetime.datetime.strptime(inpDate, "%Y%m%d")
end_date = datetime.datetime.strptime(outDate, "%Y%m%d")
while begin_date <= end_date:
date_str = begin_date.strftime("%m%d")
date_list.append("2017"+date_str+'.log')
begin_date += datetime.timedelta(days=1)
return date_list
#input_Day
def inputDay():
inp=raw_input("输入开始:").split()
oup=raw_input("输入结束:").split()
ContrastFile(inp,oup)
#ContrastDate
def ContrastFile(inp,oup):
for f in Newday(str(inp[0]),str(oup[0])):
day, num, errlog=ContrastDay(f)
print(day, num, errlog)
#Contrastday
def ContrastDay(fil):
day=[]
err =[]
a = 0
re = open("d:\log\\" + fil, 'r')
day.append(fil)
for line in re:
if line.split('=')[0] in 'error':
err.append(line.rsplit())
a += 1
return day, a, err
if __name__=='__main__':
inputDay()
测试一下: