Я создал программу чтения файлов журнала 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
Любые мысли и замечания будут приветствоваться.