python 截取某一天的日志,简单操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #!/usr/bin/python #Filename: Segmentation_log.py import re,sys def openfile( * args): try : f = open (args[ 0 ], 'r' ) try : while True : lines = f.readlines( 100 ) if not lines: break for line in lines: if (line.find(args[ 1 ])> = 0 ): writenewfile(line,args[ 2 ]) finally : f.close() print '*' * 21 + "END" + "*" * 21 except IOError: print args[ 0 ] + " not find!" def writenewfile( * args): try : newfile = open (args[ 1 ], 'a' ) try : newfile.write(args[ 0 ]) finally : newfile.close() except IOError: print args[ 1 ] + "not find!!" def chuli( * args): print '*' * 20 + "START" + "*" * 20 logre = re.split( '\.' ,args[ 0 ]) newlogfile = logre[ 0 ] + args[ 1 ] + "." + logre[ 1 ] openfile(args[ 0 ],args[ 1 ],newlogfile) if __name__ = = '__main__' : chuli(sys.argv[ 1 ],sys.argv[ 2 ]) |
有一客户发过来一个日志,谁知道太大了, 打不开,打开了,编辑器操作也很慢,好吧,自己动手.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步