1. 程式人生 > >python 讀取資料夾下面的日誌檔案並提取資訊

python 讀取資料夾下面的日誌檔案並提取資訊

import os
import re
import string
from sys import argv
script,logPath = argv

def calculateTime(logFile):
    print("Open %s"%(logFile))
    file = open(logFile,'r')
    fileM = open("m.txt",'a')
    fileM.write("\n")
    fileM.write(logFile+"\n\n")
    p1 = re.compile(r".*Duration: (\d\.\d*)")
    re.compile(p1)
    min=100
    max=0
    count=0
    total=0
    g1=0
    g3=0
    for line in file:
        m = re.match(p1,line)
        
        if m:
            t=m.group(1)
            t=float(t)
            if t<min:
                min=t
            if t>max:
                max=t
            if t>1:
                g1+=1
            if t>3:
                g3+=1
            total+=t
            count+=1
    print("max time is: %.3f \n"%(max))
    fileM.write("max time is:             %.3f \n"%(max))
    print("min time is: %.3f \n"%(min))
    fileM.write("min time is:             %.3f \n"%(min))
    print("total time is: %.3f \n"%(total))
    fileM.write("total time is:           %.3f \n"%(total))
    print("count is: %d \n"%(count))
    fileM.write("count is:                %d \n"%(count))
    if count>0:
        print("average time is %.3f \n"%(total/count))
        fileM.write("average time is          %.3f \n"%(total/count))
        print("more than 1s : %d \n"%(g1))
        fileM.write("more than 1s :           %d \n"%(g1))
        print("percent more than 1s : %.3f \n"%(g1/count))
        fileM.write("percent more than 1s :   %.3f \n"%(g1/count))
        print("more than 3s : %d \n"%(g3))
        fileM.write("more than 3s :           %d \n"%(g3))
        print("percent more than 1s : %.3f \n"%(g3/count))
        fileM.write("percent more than 1s :   %.3f \n"%(g3/count))
    file.close()
if os.path.exists(logPath):
    print("Path %s exists."%(logPath))
else:
    print("File %s doesn't exists"%s(logPath))
    exit()
if (os.path.isfile(logPath)):
    print("%s is a file."%(logPath))
    calculateTime(logPath)
if (os.path.isdir(logPath)):
    print("%s is a folder."%(logPath))
    list = os.listdir(logPath)
    for logFile in list:
        print("File: %s"%(logFile))
        logFile=os.path.join(logPath, logFile)
        if(os.path.isfile(logFile)):
            print("Join path is: %s"%(logFile))
            calculateTime(logFile)