Python 指令碼,計算相鄰兩條帶 token 的 log 的時間差
阿新 • • 發佈:2020-12-03
import sys, os def get_interval(file_name, key_word): cnt = 0; prev = 0; interval_list = []; with open(file_name,"r") as f: for line in f.readlines(): line = line.strip() #print (line) if(key_word in line): now = line[12:18]; interval= int(now) - prev if (prev == 0): interval = 0; prev = int(now) if(interval != 0): interval_list.append(interval) if(interval < 0): print (line) print ("Interval %d, now %d, prev %d\n" %(interval, int(now), prev)) cnt = cnt + 1 if(len(interval_list) != 0): print ("File: %s, Key Word %s, Max Interval %d, Min Interval %d\n" %(file_name, key_word, max(interval_list), min(interval_list))) #print(start) #print(end) def loop_over(dir_name, key_word): files= os.listdir(dir_name) for file in files: if not file.endswith('.log'): continue get_interval(file, key_word) def main(argv): loop_over(argv[1], argv[2]) if __name__ == '__main__': main(sys.argv)