# 大文件md5 注意:同一个文件的时候一定要用同一个md5的实例化对象 25cd4f5e603e0da5ff5b12e445503052
def md5_file(file_name,each_size=1024):
import os
md5_obj = hashlib.md5()
file_size = os.path.getsize(file_name)
with open(file_name,'rb') as f:
count = 0
while count < file_size:
if count + each_size < file_size:
line = f.read(each_size)
count += each_size
else:
line = f.read(file_size-count)
count = file_size
md5_obj.update(line)
m = md5_obj.hexdigest()
return m
def md5_file_compare(file1,file2,each_size=1024):
file1_md5 = md5_file(file1,each_size)
file2_md5 = md5_file(file2,each_size)
return file1_md5,file2_md5,file1_md5==file2_md5
file1 = '测试用的文件.mp4'
file2 = 'os模块.ipynb'
print(md5_file_compare(file1,file2,each_size=1024))