2016-10-18 8 views
1

2つのファイルを比較するpythonの方法を探しています。file1とfile2は、パッチファイルの形式の違いを取得し、その違いをfile2にマージします。私は違いを見つけるためにGoogleのPythonのAPIのdif_match_patchで次のポストImplementing Google's DiffMatchPatch API for Python 2/3を見てきましたが、私は作成して、パッチを適用するソリューションを探していますpython diff_match_patchを使ってパッチを作成して適用する方法

diff file1 file2 > diff.patch 
apply the patch diff.patch to file2 // this must be doing something like git apply. 

:コードは、このような何かを行う必要があります。

答えて

2

まず、diff_match_patchをインストールする必要があります。ここで

は私のコードです:

import sys 
 
import time 
 
import diff_match_patch as dmp_module 
 

 

 
def readFileToText(filePath): 
 
\t file = open(filePath,"r") 
 
\t s = '' 
 
\t for line in file: 
 
    \t \t s = s + line 
 
\t return s 
 
    
 

 
dmp = dmp_module.diff_match_patch() 
 
origin = sys.argv[1]; 
 
lastest = sys.argv[2]; 
 

 
originText = readFileToText(origin) 
 
lastestText = readFileToText(lastest) 
 

 
patch = dmp.patch_make(originText, lastestText) 
 
patchText = dmp.patch_toText(patch) 
 

 
# floder = sys.argv[1] 
 
floder = '/Users/test/Documents/patch' 
 
print(floder) 
 
patchFilePath = floder 
 
patchFile = open(patchFilePath,"w") 
 
patchFile.write(patchText) 
 

 
print(patchText)

関連する問題