Life is short, you need Python

分析log文件中的keyword

#! /usr/bin/env python

import sys,os

FilePath
= 'd:/SVNtest/'
FileName
= FilePath + 'error.log'
ResultFile
= FilePath + 'Fail.log'
KeyWord
= 'error'

try:
fh
= open(FileName,'r')
allLines
= fh.readlines()
fh.close()
except IOError:
print "the file don't exist, Please double check!"

i
= 0
counter
= 0
NewLines
= []

for oneLine in allLines:
i
+= 1
if oneLine.find(KeyWord) >= 0:
counter
+=1
print 'Find the match line at %d,now total %d times' %(i,counter)
NewLines.append(oneLine)


fh
= open(ResultFile,'a')
fh.writelines(NewLines)
fh.close()

 

posted @ 2010-10-25 09:52  runfox545  阅读(363)  评论(0编辑  收藏  举报
白月黑羽 Python教程 白月黑羽Python