以下 python 2.5 脚本有效,但由于我是初学者,我想知道是否有任何明显的错误,或者是否有更好的方法来实现我想要实现的目标?
目的是打开当天的日志 - '/Library/Application Support/Perceptive Automation/Indigo 5/Logs/' 并提取包含错误一词的行并仅通过电子邮件发送新错误。提取错误行后,将计算 ( prenumLines
) 中的行数tmp.txt
。然后将提取的行写入tmp.txt
文件并再次计算行数 ( postnumLines
)。大于 的行号prenumLines
将打印到“theBody”并通过电子邮件发送。
from datetime import date
import linecache
fileDate = str(date.today())
theBody = []
tmpFile = open('/Library/Application Support/Perceptive Automation/Indigo 5/Logs/tmp.txt')
prenumLines = sum(1 for line in tmpFile)
log= open( '/Library/Application Support/Perceptive Automation/Indigo 5/Logs/' + fileDate + ' Events.txt', 'r' )
tmpFile = open('/Library/Application Support/Perceptive Automation/Indigo 5/Logs/tmp.txt', 'w')
for line in log:
if 'Error' in line:
tmpFile.write(line )
log.close()
tmpFile.close()
postnumLines = sum(1 for line in open('/Library/Application Support/Perceptive Automation/Indigo 5/Logs/tmp.txt'))
lineNum = prenumLines
while lineNum < postnumLines:
theBody.append(linecache.getline( '/Library/Application Support/Perceptive Automation/Indigo 5/Logs/tmp.txt', lineNum + 1) )
lineNum = lineNum + 1
tmpFile.close()
theBody = "".join(theBody)
#theBody is the body of an email which is sent next
#print theBody