种心收默

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])

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

 

posted on   samcao  阅读(856)  评论(0编辑  收藏  举报

努力加载评论中...
< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示