在Python中,有没有简洁的方法比较两个文本文件的内容是否相同?

77 投票
10 回答
43316 浏览
提问于 2025-04-11 09:34

我不在乎它们之间有什么区别。我只想知道内容是否不同。

10 个回答

15

这是一个功能性风格的文件比较函数。如果两个文件的大小不一样,它会立刻返回“假”(False);如果大小相同,它会以4KiB(千字节)为块大小来读取文件内容,并在第一次发现不同的时候立刻返回“假”(False):

from __future__ import with_statement
import os
import itertools, functools, operator
try:
    izip= itertools.izip  # Python 2
except AttributeError:
    izip= zip  # Python 3

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

        # set up one 4k-reader for each file
        fp1_reader= functools.partial(fp1.read, 4096)
        fp2_reader= functools.partial(fp2.read, 4096)

        # pair each 4k-chunk from the two readers while they do not return '' (EOF)
        cmp_pairs= izip(iter(fp1_reader, b''), iter(fp2_reader, b''))

        # return True for all pairs that are not equal
        inequalities= itertools.starmap(operator.ne, cmp_pairs)

        # voilà; any() stops at first True value
        return not any(inequalities)

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

这只是另一种写法 :)

36

如果你想提高一些基本的效率,首先你可能想检查一下文件的大小:

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

这样可以避免你去逐行读取两个文件,而这两个文件的大小都不一样,自然也不可能是相同的。

(更进一步,你还可以用一个快速的MD5校验和来比较每个文件,但那就不是“用Python”了,所以我就不多说了。)

95

低级的方法:

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):
   ...

撰写回答