欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)比較文件內(nèi)容異同

 更新時(shí)間:2018年06月22日 14:56:16   作者:Sino_Crazy_Snail  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)比較文件內(nèi)容異同,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)比較文件內(nèi)容異同的具體代碼,供大家參考,具體內(nèi)容如下

import sys
import difflib
import time
import os
"""
創(chuàng)作時(shí)間:2017-10-08 23:30 09
版本: 1.0.0
"""
def main():
  """主函數(shù)"""
  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 == "":#參數(shù)不夠
    print("Usage : python compareFile.py filename1 filename2")
    sys.exit()

  tf1 = readFile(f1)
  tf2 = readFile(f2)

  d = difflib.HtmlDiff()#創(chuàng)建一個(gè)實(shí)例difflib.HtmlDiff
  writeFile(d.make_file(tf1,tf2))#生成一個(gè)比較后的報(bào)告文件,格式為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()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論