比較兩個文件的不同之處用處還是比較大的,特別是比較兩個版本的不同之處
[root@localhost python]# cat diftest.py
#!/usr/bin/python
import difflib
import sys
try:
file1=sys.argv[1] #第一個參數文件
file2=sys.argv[2] #第二個參數文件
except Exception,e:
print "Error: %s"%str(e)
sys.exit()
def readfile(filename): #相對來說,可以考慮調用函數,因為需要處理兩個文件
try: #檢測異常
df=open(filename,"rb") #打開文件
text=df.read().splitlines() #讀取文件內容,並根據行進行分割
df.close() #關閉文件
return text #返回文件內容字符串
except IOError,e: #拋出異常
print "ERROR: %s"%str(e)
sys.exit()
if file1=="" or file2=="":
print "please input filename and filename"
sys.exit()
file1_lines=readfile(file1)
file2_lines=readfile(file2)
diff=difflib.HtmlDiff() #創建HtmlDiff類對象
print diff.make_file(file1_lines,file2_lines)
[root@localhost python]# ./diftest.py ip.py ip.py1 >a.html
[root@localhost python]# cat ip.py
#!/usr/bin/python
from IPy import IP
print IP('192.168.0.0/16').version()
ipok=IP('192.0.0.0/8')
i=IP('192.168.1.100')
print ip1.reverseNames()
ip.len()
#for x in ip:
# print(x)
[root@localhost python]# cat ip.py1
#!/usr/bin/python
from IPy import IP
print IP('192.168.0.0/16').version()
ip=IP('192.0.0.0/80')
ip1=IP('192.168.1.100')
print ip1.reverseNames()
print ip.len().linux
#for x in ip:
# print(x)
kkkk
[root@localhost python]#
看一下a.html的效果