python 比较2个文件内容

 

1. 通过使用md5字符串比较2个文件

import hashlib
def get_file_md5(filename):
    '''可以比较两个文件的md5值,来比较文件内容。未使用'''
    md5 = hashlib.md5()
    f = open(filename, 'rb')
    while True:
        b = f.read(8096)
        if not b:
            break
        md5.update(b)
    f.close()
    return md5.hexdigest()

 

python3

import filecmp
# true if 2 files are the same
result = filecmp.cmp(file1, file2)

 

posted @ 2019-10-23 16:16  道以万计  阅读(5529)  评论(0编辑  收藏  举报