版權聲明:本文系做者原創。未經許可,不得轉載。code
大文件若是一次性讀入計算,會佔用大量內存,能夠分塊讀取、分塊計算,效果與一次讀取計算相同。代碼以下: import hashlib import base64 ''' sha1 file with filename (SHA1) ''' def SHA1FileWithName(fineName, block_size=64 * 1024): with open(fineName, 'rb') as f: sha1 = hashlib.sha1() while True: data = f.read(block_size) if not data: break sha1.update(data) retsha1 = base64.b64encode(sha1.digest()) return retsha1 ''' md5 file with filename (MD5) ''' def MD5FileWithName(fineName, block_size=64 * 1024): with open(fineName, 'rb') as f: md5 = hashlib.md5() while True: data = f.read(block_size) if not data: break md5.update(data) retmd5 = base64.b64encode(md5.digest()) return retmd5