zoukankan      html  css  js  c++  java
  • python 截取某一天的日志,简单操作

    #!/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])
    

    有一客户发过来一个日志,谁知道太大了, 打不开,打开了,编辑器操作也很慢,好吧,自己动手.

  • 相关阅读:
    printf,sprintf,fprintf的区别与联系
    linux repo init 遇到的问题
    POSTMAN使用说明
    HTTP协议-深入了解
    HTTP协议-详解篇
    HTTP协议-基础篇
    多线程学习
    I/O流
    String类学习&泛型
    集合
  • 原文地址:https://www.cnblogs.com/drgcaosheng/p/3747820.html
Copyright © 2011-2022 走看看