python比对两个文件中的内容是否一致并将结果保存本地

import sys

import difflib

def read_file(filename):

    try:

        with open(filename, 'r',encoding="utf-8") as f:

                return f.readlines()

        except IOError:

                print("ERROR: 没有找到文件:%s或读取文件失败!" % filename)

         sys.exit(1)

def compare_file(file1, file2, out_file):

        file1_content = read_file(file1)

        file2_content = read_file(file2)

        d = difflib.HtmlDiff()

        result = d.make_file(file1_content, file2_content)

        with open(out_file, 'w',encoding='utf-8') as f:

                f.writelines(result)

if __name__ == '__main__':

        compare_file('files/userPre.txt', 'files/userAft.txt', 'E:\\user.html')

        compare_file('files/authorityPre.txt', 'files/authorityAft.txt', 'E:\\authority.html')

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容