2008-10-31 358 views

回答

49

低级别的方法:

from __future__ import with_statement 
with open(filename1) as f1: 
    with open(filename2) as f2: 
     if f1.read() == f2.read(): 
     ... 

高水平方式:

import filecmp 
if filecmp.cmp(filename1, filename2, shallow=False): 
    ... 
+9

我纠正你的filecmp.cmp电话,因为不存在非真浅论点,它没有做问题所要求的。 – tzot 2008-10-31 23:11:49

+2

你是对的。 http://www.python.org/doc/2.5.2/lib/module-filecmp.html。非常感谢你。 – 2008-11-01 03:21:44

+1

btw,应该以二进制模式打开文件以确保文件可以在行分隔符中有所不同。 – newtover 2013-04-29 10:30:52

3
 

f = open(filename1, "r").read() 
f2 = open(filename2,"r").read() 
print f == f2 

 
+5

“嗯,我有这个8 GiB文件和我想比较的那个32 GiB文件...” – tzot 2008-10-31 23:19:37

22

如果你打算为连基本的效率,您可能需要首先检查文件大小:

if os.path.getsize(filename1) == os.path.getsize(filename2): 
    if open('filename1','r').read() == open('filename2','r').read(): 
    # Files are the same. 

这样可以节省您的阅读每两行文件的大小并不相同,因此不能相同。

(甚至更重要的是,你可以调用出每个文件的快速MD5SUM和比较这些,但是这不是“在Python”,所以我会在这里停下来。)

1

我会使用MD5的文件内容的散列。

import hashlib 

def checksum(f): 
    md5 = hashlib.md5() 
    md5.update(open(f).read()) 
    return md5.hexdigest() 

def is_contents_same(f1, f2): 
    return checksum(f1) == checksum(f2) 

if not is_contents_same('foo.txt', 'bar.txt'): 
    print 'The contents are not the same!' 
5

因为我不能评论别人的答案我会写我自己的。

如果你使用md5,你肯定不能只是md5.update(f.read()),因为你会使用太多的内存。

def get_file_md5(f, chunk_size=8192): 
    h = hashlib.md5() 
    while True: 
     chunk = f.read(chunk_size) 
     if not chunk: 
      break 
     h.update(chunk) 
    return h.hexdigest() 
7

这是一种功能样式的文件比较功能。如果文件具有不同的大小,它立即返回False;否则,它读取4KiB块大小,并立即在第一差返回False:

from __future__ import with_statement 
import os 
import itertools, functools, operator 

def filecmp(filename1, filename2): 
    "Do the two files have exactly the same contents?" 
    with open(filename1, "rb") as fp1, open(filename2, "rb") as fp2: 
     if os.fstat(fp1.fileno()).st_size != os.fstat(fp2.fileno()).st_size: 
      return False # different sizes ∴ not equal 
     fp1_reader= functools.partial(fp1.read, 4096) 
     fp2_reader= functools.partial(fp2.read, 4096) 
     cmp_pairs= itertools.izip(iter(fp1_reader, ''), iter(fp2_reader, '')) 
     inequalities= itertools.starmap(operator.ne, cmp_pairs) 
     return not any(inequalities) 

if __name__ == "__main__": 
    import sys 
    print filecmp(sys.argv[1], sys.argv[2]) 

只是不同的看法:)

0
from __future__ import with_statement 

filename1 = "G:\\test1.TXT" 

filename2 = "G:\\test2.TXT" 


with open(filename1) as f1: 

    with open(filename2) as f2: 

     file1list = f1.read().splitlines() 

     file2list = f2.read().splitlines() 

     list1length = len(file1list) 

     list2length = len(file2list) 

     if list1length == list2length: 

      for index in range(len(file1list)): 

       if file1list[index] == file2list[index]: 

        print file1list[index] + "==" + file2list[index] 

       else:     

        print file1list[index] + "!=" + file2list[index]+" Not-Equel" 

     else: 

      print "difference inthe size of the file and number of lines" 
相关问题