1、解释大神之作,致敬“种心收默”(http://www.cnblogs.com/drgcaosheng/p/3747820.html)
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
|
#!/usr/bin/python #-*- coding:utf-8 -*- import re,sys
def openfile( * args):
print args
try :
f = open (args[ 0 ], 'r' ) #args[0]表示要打开的文件,第一个参数下标的值
try :
while True :
lines = f.readlines( 100 )
if not lines: #假如没有行了,则跳出整个循环
break
for line in lines: #循环每一行
if (line.find(args[ 1 ])> = 0 ): #args[1]表示要搜索的文字,第二个参数下标的值
writenewfile(line,args[ 2 ]) #假如存在搜索的关键字,然后传到writenewfile函数,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) #调用openfile函数,args[0]和args[1]是chuli传过来sys.argv[1],sys.argv[2]。
if __name__ = = '__main__' : #意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行。
chuli(sys.argv[ 1 ],sys.argv[ 2 ]) #获取python cao.py 104.250.149.146-test.log "Dec 18"的实际参数传给chuli函数的args[0]和args[1]
|
2、运行方式和结果。
1
2
3
4
|
[root@localhost ~] # python cao.py 104.250.3.77-test.log "Dec 18"
* * * * * * * * * * * * * * * * * * * * START * * * * * * * * * * * * * * * * * * * *
( '104.250.3.77-test.log' , 'Dec 18' , '104Dec 18.250' )
* * * * * * * * * * * * * * * * * * * * * END * * * * * * * * * * * * * * * * * * * * *
|
执行顺序如下:
(1)、先调用chuli函数,将python cao.py命令后实际的2个参数传递给chuli函数。
(2)、执行chuli函数,打印开始的*号,生成新文件名,并调用openfile函数,并且将三个参数传给了openfile函数。
(3)、执行openfile函数,读取文件,搜索关键字,搜索完成后调用writenewfile函数,写入新文件中。
(4)、执行writenewfile函数,以追加方式打开新文件,然后把匹配的行一行一行写入到新文件中。
3、小弟python知识、逻辑有限,*args传多个值把我搞晕了,所以我改下:
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
45
|
#!/usr/bin/python #-*- coding:utf-8 -*- import re,sys
def openfile(srcfile,search,decfile):
try :
f = open (srcfile, 'r' )
try :
while True :
lines = f.readlines( 100 )
if not lines:
break
for line in lines:
if (line.find(search)> = 0 ):
writenewfile(line,decfile)
finally :
f.close()
print '*' * 21 + "END" + "*" * 21
except IOError:
print srcfile + " not find!"
def writenewfile(line,decfile):
try :
newfile = open (decfile, 'a' )
try :
newfile.write(line)
finally :
newfile.close()
except IOError:
print decfile + "not find!!"
def makefile(srcfile,search):
print '*' * 20 + "START" + "*" * 20
namesplit = re.split( '\.' ,srcfile)
decfile = namesplit[ 0 ] + search + "." + namesplit[ 1 ]
openfile(srcfile,search,decfile)
if __name__ = = '__main__' :
makefile(sys.argv[ 1 ],sys.argv[ 2 ])
|
本文转自 sailikung 51CTO博客,原文链接:http://blog.51cto.com/net881004/2052575,如需转载请自行联系原作者