以下是我用来执行任务的代码。该代码可以正常运行,但是需要2分钟以上的时间才能执行。而且只有一个规则(Rule_1),该规则是使用.exe(用CPP编写)检查的。实际上,要通过这些规则需要很多CSV文件。
我的问题是程序将始终使用大量时间,因为我必须对文件实施更多的50条规则,否则还有其他方法吗?
import os
import fnmatch
import subprocess
import xml.etree.ElementTree as ElementTree
from xml.parsers.expat import ExpatError
import sys
from shutil import copyfileobj
def locate(pattern, root="Z:/Automation/"):
'''Locate all files matching supplied filename pattern in and below
supplied root directory.'''
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
csv_path_unrefined = []
for xml in locate("*.csv"):
try:
ElementTree.parse(xml)
except (SyntaxError, ExpatError):
csv_path_unrefined.append(xml)
csv_path = []
for paths in csv_path_unrefined:
if "results" in str(paths):
csv_path.append(paths)
def check_rule1(path):
# path = "PWLLOGGER_DEMO.csv"
file = 'ConsoleApplication9.exe "' + path + '"'
# print(file)
details = os.popen(file).read()
log_file = open("logs/Rule_1.txt")
state = log_file.read()
with open('results/Rule_1_log.log', 'a+') as files:
files.write("\n========" + path + "========\n")
files.close
with open('results/Rule_1_log.log', 'a+') as output, open('logs/Rule_1.txt', 'r') as input:
copyfileobj(input, output)
if "failed" in state:
return False
else:
return True
rule_1_passed = []
rule_1_failed = []
for paths in csv_path:
result_r1 = check_rule1(paths)
# print(result_r1)
if result_r1 == False:
rule_1_failed.append(paths)
#print("Rule 1 has failed for " + paths)
elif result_r1 == True:
rule_1_passed.append(paths)
#print("Rule 1 has passed for " + paths)
open('logs/Rule_1.txt', 'w').close()
print(rule_1_failed)
print(rule_1_passed)