python实现比较文件内容异同
本文实例为大家分享了python实现比较文件内容异同的具体代码,供大家参考,具体内容如下
import sys import difflib import time import os """ 创作时间:2017-10-08 23:30 09 版本: 1.0.0 """ def main(): """主函数""" try: f1 = sys.argv[1]#获取文件名 f2 = sys.argv[2] except Exception as e: print("Error: "+ str(e)) print("Usage : python compareFile.py filename1 filename2") sys.exit() if f1 == "" or f2 == "":#参数不够 print("Usage : python compareFile.py filename1 filename2") sys.exit() tf1 = readFile(f1) tf2 = readFile(f2) d = difflib.HtmlDiff()#创建一个实例difflib.HtmlDiff writeFile(d.make_file(tf1,tf2))#生成一个比较后的报告文件,格式为html def readFile(filename): """读取文件,并处理""" try: fileHandle = open(filename, "r") text = fileHandle.read().splitlines() fileHandle.close() return text except IOError as e: print("Read file error: "+ str(e)) sys.exit() def writeFile(file): """写入文件""" diffFile = open('diff_{}_.html'.format(time.strftime("%Y_%m_%d_%H_%M_%S",time.localtime())), "w") diffFile.write("<meta charset='UTF-8'>") diffFile.write(file) print("The file on {}".format(os.path.abspath(str(diffFile.name))))#提示文件生成在什么地方 diffFile.close() if __name__ == "__main__": main()
相关推荐
夜斗不是神 2020-11-17
huavhuahua 2020-11-20
Yasin 2020-11-16
xiaoseyihe 2020-11-16
千锋 2020-11-15
diyanpython 2020-11-12
chunjiekid 2020-11-10
wordmhg 2020-11-06
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16