Python计算文件MD5值

 1 import hashlib
 2 def fileMD5(filename):
 3     m = hashlib.md5()    #md5计算
 4     #m = hashlib.sha1()   #sha1计算
 5     #m = hashlib.sha224()   #sha224计算
 6     #m = hashlib.sha256()    #sha256计算
 7     #m = hashlib.sha384()    #sha384计算
 8     #m = hashlib.sha512()    #sha512计算
 9     #
10     with open(filename) as f:
11         while True:
12             data = f.read(4096)
13             if len(data) == 0:
14                 break
15             m.update(data)
16     return m.hexdigest()
17 if __name__ == '__main__':
18     fileMD5('xxxxx.xxx')

 

posted @ 2015-12-17 11:10  dozeoo  阅读(1750)  评论(0编辑  收藏  举报