MSI日志文件读取器未将结果写入文本文件

时间:2018-12-14 23:20:13

标签: python logging

我创建了一个MSI日志文件读取器,该读取器搜索某些安装程序在安装过程中留下的特定日志文件名。它将结果分别打印在外壳程序中,但不会将它们写入创建的“ MSI_Return.txt”文件中。

#Import Python Modules
import os
import fnmatch

#Create a find function to search the directories for the file name
def find(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name))
    return result

#Get the full list of LOG files from specified folder
infile = os.path.expanduser('~\\AppData\\Local\\Temp')
listoffiles = find("MSI*.LOG", infile)


#Process each file name in the file name array
key_phrases = ["Error",
              "Failed",
              "Exception", "Return Value 3"]
important = []
for eachfile in listoffiles:
    #Open the file
    with open(eachfile) as i:
        i = i.readlines()
    #Look through the lines for phrases
    #If a phrase is found, append it to an array
    for line in i:
        for phrase in key_phrases:
            if phrase in line:
                important.append(line)
                break

#Return output
MSI_Return = open('MSI_Return.txt','w')
MSI_Return.write('List of MSI files = ' + str(listoffiles))
MSI_Return.write('\n')
MSI_Return.write('Return of key lines from MSI files = ' + str(important))
MSI_Return.close

任何想法或建议都会受到赞赏。

0 个答案:

没有答案