日志处理小脚本
处理jmeter产生大量数据的jtl文件的python脚本
1 import os 2 import easygui as g 3 4 def cut_log(path,num): 5 '''num:new file with hava num lines ''' 6 f=open(path,'r',encoding='utf-8') 7 count=1 8 while 1: 9 dirpath=os.path.dirname(path) 10 filename=path[len(dirpath):] 11 name=dirpath+filename.split('.')[0]+str(count)+'.'+filename.split('.')[1] 12 print(name) 13 f1=open(name,'w',encoding='utf-8') 14 for i in range(0,int(num)): 15 line=f.readline() 16 if not line: 17 return 'success' 18 f1.write(line) 19 f1.close() 20 count+=1 21 f.close() 22 23 def remove_someline(path,keyworld): 24 dirpath = os.path.dirname(path) 25 filename = path[len(dirpath)+1:] 26 name = dirpath +'\\new'+filename.split('.')[0] + '.' + filename.split('.')[1] 27 print(name) 28 f=open(path,'r',encoding='UTF-8') 29 f1=open(name,'w',encoding='utf-8') 30 while 1: 31 line=f.readline() 32 if not line: 33 break 34 if line.find(keyworld)==-1: 35 f1.write(line) 36 37 f.close() 38 f1.close() 39 40 if __name__=='__main__': 41 choice=g.choicebox(msg='选择要干的活',choices=['切割','删除行']) 42 if choice=='切割': 43 filtname = g.fileopenbox(msg='选择需要切割的文件') 44 num = g.enterbox(msg='选择生成小文件行数') 45 message = cut_log(filtname, num) 46 dir = os.path.dirname(filtname) 47 g.msgbox(msg="切割完成,请到《%s》目录下查看" % (dir), title=message) 48 elif choice=='删除行': 49 filename = g.fileopenbox(msg='选择需要处理的文件') 50 keyworld= g.enterbox(msg='输入包含的关键词') 51 remove_someline(filename,keyworld) 52 g.msgbox(title='success',msg='成功')