在Python中,是否有一个简洁的方法来比较两个文本文件的内容是否相同?
问题描述:
我不在乎差异是什么。我只想知道内容是否不同。
I don't care what the differences are. I just want to know whether the contents are different.
答
低级方法:
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):
...