我不在乎有什么不同。我只是想知道内容是否不同。
Corey Trager
问问题
35306 次
9 回答
88
低级方式:
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):
...
于 2008-10-31T17:50:04.310 回答
30
如果您想要达到基本的效率,您可能需要先检查文件大小:
if os.path.getsize(filename1) == os.path.getsize(filename2):
if open('filename1','r').read() == open('filename2','r').read():
# Files are the same.
这样可以节省您阅读两个文件的每一行,这些文件的大小甚至不同,因此不能相同。
(更进一步,您可以调用每个文件的快速 MD5sum 并比较它们,但这不是“在 Python 中”,所以我会停在这里。)
于 2008-10-31T17:56:15.187 回答
13
这是一个函数式文件比较函数。如果文件大小不同,它会立即返回 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])
只是一个不同的看法:)
于 2008-10-31T23:03:01.760 回答
6
由于我无法评论其他人的答案,所以我会自己写。
如果你使用 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()
于 2008-10-31T19:06:03.000 回答
2
f = open(filename1, "r").read()
f2 = open(filename2,"r").read()
print f == f2
于 2008-10-31T17:52:16.910 回答
2
我会使用 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!'
于 2008-10-31T18:53:52.870 回答
1
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"
于 2016-12-15T17:10:53.367 回答
0
简单高效的解决方案:
import os
def is_file_content_equal(
file_path_1: str, file_path_2: str, buffer_size: int = 1024 * 8
) -> bool:
"""Checks if two files content is equal
Arguments:
file_path_1 (str): Path to the first file
file_path_2 (str): Path to the second file
buffer_size (int): Size of the buffer to read the file
Returns:
bool that indicates if the file contents are equal
Example:
>>> is_file_content_equal("filecomp.py", "filecomp copy.py")
True
>>> is_file_content_equal("filecomp.py", "diagram.dio")
False
"""
# First check sizes
s1, s2 = os.path.getsize(file_path_1), os.path.getsize(file_path_2)
if s1 != s2:
return False
# If the sizes are the same check the content
with open(file_path_1, "rb") as fp1, open(file_path_2, "rb") as fp2:
while True:
b1 = fp1.read(buffer_size)
b2 = fp2.read(buffer_size)
if b1 != b2:
return False
# if the content is the same and they are both empty bytes
# the file is the same
if not b1:
return True
于 2021-07-31T11:10:31.307 回答