python實現比較檔案內容異同

2022-10-04 19:24:20 字數 1375 閱讀 9322

import sys

import difflib

import time

import os

"""創作時間:2017-10-08 23:30 09

版本: 1.0.0

"""def main():

"""主函式"""

try:

f1 = sys.ar**[1]#獲取檔名

f2 = sys.ar**[2程式設計客棧]

except exception as e:

print("error: "+ str(e))

print("usage : python comparefile.py filename1 filename2")

sys.exit()

if f1 == "" or f2 == "":#引數不夠

print(www.cppcns.com"usage : python comparefile.py filename1 filename2")

sys.exit()

tf1 = readfile(f1)

tf2 = readfile(f2)

udfwea d = difflib.htmldiff()#建立乙個例項difflib.htmldiff

writefile(d.make_file(tf1,tf2))#生成乙個比較後的報告檔案,格式為html

def readfile(filename):

"""讀取檔案,並處理"""

try:

filehandle = open(filename, "r")

te程式設計客棧xt = 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."%y_%m_%d_%h_%m_%s",time.localtime())), "w")

difffile.write("")

difffile.write(file)

print("the file on {}".format(os.path.abspath(str(difffile.name))))#提示檔案生成在什麼地方

difffile.close()

if __name__ == "__main__":

main()

本文標題: python實現比較檔案內容異同

本文位址:

檔案內容比較

這是乙個c 程式設計思想 2卷 上的乙個示例,試寫了一下,算是對模板的乙個練習 include include include include include include include include using namespace std typedef vectorvecstr typed...

異方差 Python實現

借鑑一位博主的文章,結合兩篇,用於實現python 本人親測,可以跑通。這位博主的 字母有連一起的,以下是我親測的 import pandas as pd import numpy as np from patsy import dmatrices from statsmodels.stats.ou...

ThinkPHP JQuery實現檔案的非同步上傳

效果圖 上傳檔案介面 public function uploadapi else else return json 1.上傳一張 2.上傳成功 4.檢視 5.檢視上傳目錄 1.php的上傳限制 解決方法 開啟php的配置檔案php.ini 查詢max execution time,修改其值為60或...