我有这个python脚本,但是它占用了100%的CPU,如何降低它, 是什么导致脚本占用100%的CPU,以及如何使diff更快。是因为difflib的问题,是否还有其他lib可以提供与此
相同的输出import os
import re
import difflib
source = input('Enter Solaris File Dir: ')
source1 = input('Enter Linux File Dir: ')
reportname = input('Enter output reportname: ')
pattern = re.compile("^(.*?)\.(.*?)\.")
for root, dirs, filenames in os.walk(source):
for solarisfile in filenames:
#print solarisfile
#find = re.compile("^(.*?)\.(.*?)\.")
rptname1 = re.match(pattern,solarisfile).group(1)
rptname2 = re.match(pattern,solarisfile).group(2)
solarisdcfname = rptname1+rptname2
#print solarisdcfname
solarisfullpath = os.path.join(source, solarisfile)
for root, dirs, filenames in os.walk(source1):
for linuxfile in filenames:
#print " Ima here "
#print linuxfile
#find1 = re.compile("^(.*?)\.(.*?)\.")
lrptname1 = re.match(pattern,linuxfile).group(1)
lrptname2 = re.match(pattern,linuxfile).group(2)
ldcfname = lrptname1+lrptname2
linuxfullpath = os.path.join(source1, linuxfile)
if(solarisdcfname==ldcfname):
print "----Matched file name ---------------"
print ldcfname
print solarisfile
print linuxfile
print("-----------------------------------")
print("Comparing files ", " > " + solarisfile, " < " +linuxfile)
print("-----------------------------------")
solaris_lines = open(solarisfullpath).readlines()
linux_lines = open(linuxfullpath).readlines()
difference = difflib.HtmlDiff().make_file(solaris_lines,linux_lines,ldcfname+'Solaris',ldcfname+'linux',True,0)
difference_report = open(reportname,'a+')
difference_report.write(difference)
difference_report.close()