×

difflib模块实现文件内容差异对比

96
付炜超
2016.01.14 22:19* 字数 124

[root@www difflib]# cat diff.py

!/usr/bin/python

import difflib
text1 = raw_input("please input text1:")
text1_lines = text1.splitlines()

text2 =raw_input("please input text2:")
text2_lines = text2.splitlines()

d =difflib.HtmlDiff()
print d.make_file(text1_lines,text2_lines)

测试:

[root@www difflib]# python diff.py
please input text1:asdfgh
please input text2:asdf

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

下面还有大量的输出,我们把输出保存到文件里可以看到html效果.

下面是我51cto的链接
http://9399369.blog.51cto.com/9389369/1735151

python自动化运维
Web note ad 1