使用python脚本比较两个文件的差异内容并输出到html文档中,可以通过浏览器打开查看。
一、脚本使用对比nginx配置文件的差异
python python_diff_file.py -f1 web26.conf -f2 web103.conf
二、脚本内容#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
1.difflib的HtmlDiff类创建html表格用来展示文件差异,通过make_file方法
2.make_file方法使用
make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
用来生成一个包含表格的html文件,其内容是用来展示差异。
fromlines和tolines,用于比较的内容,格式为字符串组成的列表
fromdesc和todesc,可选参数,对应的fromlines,tolines的差异化文件的标题,默认为空字符串
context 和 numlines,可选参数,context 为True时,只显示差异的上下文,为false,显示全文,numlines默认为5,
当context为True时,控制展示上下文的行数,当context为false时,控制不同差异的高亮之间移动时“next”的开始位置
3.使用argparse传入两个需要对比的文件
"""
import difflib
import argparse
import sys
# 创建打开文件函数,并按换行符分割内容
def readfile(f