2010-08-14 23 views
3

我想比較兩個文件中的數據,並檢索差異所在的偏移量列表。 我在一些文本文件上試過它,它工作得很好.. 但是在非文本文件(仍然包含ascii文本)時,我將它們稱爲二進制數據文件。 (可執行文件,等等..)我應該使用struct來比較字節嗎?

它似乎認爲一些字節是相同的,即使當我在十六進制編輯器看它時,他們顯然不是。我試着打印出它認爲是相同的這個二進制數據,並且在它應該打印的地方得到空行。 因此,我認爲這是問題的根源。

那麼什麼是最好的方式來比較可能是二進制和包含ascii文本的數據字節?我想到了用結構模塊通過是一個起點......

正如你可以看到下面,我比較==操作符

這裏字節的代碼:

import os 
import math 


#file1 = 'file1.txt' 
#file2 = 'file2.txt' 
file1 = 'file1.exe' 
file2 = 'file2.exe' 
file1size = os.path.getsize(file1) 
file2size = os.path.getsize(file2) 
a = file1size - file2size 
end = file1size #if they are both same size 
if a > 0: 
    #file 2 is smallest 
    end = file2size 
    big = file1size 

elif a < 0: 
    #file 1 is smallest 
    end = file1size 
    big = file2size 


f1 = open(file1, 'rb') 
f2 = open(file2, 'rb') 



readSize = 500 
r = readSize 
off = 0 
data = [] 
looking = False 
d = open('data.txt', 'w') 


while off < end: 
    f1.seek(off) 
    f2.seek(off) 
    b1, b2 = f1.read(r), f2.read(r) 
    same = b1 == b2 
    print '' 
    if same: 
     print 'Same at: '+str(off) 
     print 'readSize: '+str(r) 
     print b1 
     print b2 
     print '' 
     #save offsets of the section of "different" bytes 
     #data.append([diffOff, diffOff+off-1]) #[begin diff off, end diff off] 
     if looking: 
      d.write(str(diffOff)+" => "+str(diffOff+off-2)+"\n") 
      looking = False 
      r = readSize 
      off = off + 1 
     else: 
      off = off + r 

    else: 
     if r == 1: 
      looking = True 
      diffOff = off 
      off = off + 1 #continue reading 1 at a time, until u find a same reading 
     r = 1 #it will shoot back to the last off, since we didn't increment it here 



d.close() 
f1.close() 
f2.close()   

#add the diff ending portion to diff data offs, if 1 file is longer than the other 
a = int(math.fabs(a)) #get abs val of diff 
if a: 
    data.append([big-a, big-1]) 


print data 

回答

4

你有沒有請嘗試difflibfilecmp模塊?

該模塊提供了用於比較序列的類和 函數。它可以用於比如 比如 文件,並且可以產生不同格式的 信息,包括HTML和上下文的 和統一的 差異。要比較目錄和 文件,另請參閱filecmp模塊。

的filecmp模塊定義功能 比較文件和目錄,與 各種可選的時間/正確性 權衡。有關比較文件,請參閱 也difflib模塊

+0

你嘗試了嗎? – 2010-08-17 10:06:22

0

您可能遇到編碼/解碼問題。有人可能會提出一個更好的解決方案,但你可以嘗試把文件讀入一個bytearray所以你讀取原始字節而不是解碼字符:

這裏有一個最原始的例子:

$ od -Ax -tx1 /tmp/aa 
000000 e0 b2 aa 0a 
$ od -Ax -tx1 /tmp/bb 
000000 e0 b2 bb 0a 

$ cat /tmp/diff.py 
a = bytearray(open('/tmp/aa', 'rb').read()) 
b = bytearray(open('/tmp/bb', 'rb').read()) 
print "%02x, %02x" % (a[2], a[3]) 
print "%02x, %02x" % (b[2], b[3]) 

$ python /tmp/diff.py 
aa, 0a 
bb, 0a