【发布时间】:2022-12-07 20:54:05
【问题描述】:
使用 difflib 库,我试图生成 html 格式的 diff 文件。它在大部分时间都有效,但有几次,生成的 html 格式不正确。有时它还观察到形成的 html 不包含所有内容,有时形成的内容在适当的位置没有行。 下面是我正在使用的代码:
import difflib
try:
print("Reading file from first file")
firstfile = open(firstFilePath, "r")
contentsFirst = firstfile.readlines()
print("Reading file from second file")
secondfile = open(secondFilePath, "r")
contentsSecond = secondfile.readlines()
print("Creating diff file:")
config_diff = difflib.HtmlDiff(wrapcolumn=70).make_file(contentsSecond, contentsFirst)
if not os.path.exists(diff_file_path):
os.makedirs(diff_file_path)
final_path = diff_file_path + "/" + diff_file_name + '.html'
diff_file = open(final_path, 'w')
diff_file.write(config_diff)
print("Diff file is genrated :")
except Exception as error:
print("Exception occurred in create_diff_file " + str(error))
raise Exception(str(error))
这段代码在线程程序中被调用。尽管通过重试,我得到了想要的结果,但不知道导致差异文件格式错误和不一致的原因。如果有人可以帮助我找到背后的真正原因并提出解决方案,那将对我有所帮助。
【问题讨论】: