【学习】大文件MD5校验

将大文件分块读取到内存中,每次读取的内容通过update()更新校验值,最终获得完整文件的md5校验值

 1 #!python3
 2 #coding:utf-8
 3 
 4 '''
 5 This module is coded by Yuning Ju
 6 
 7 Copyright 2018 Yuning Ju
 8 
 9 实现:大文件的MD5校验
10 
11 '''
12 import hashlib,time
13 
14 
15 def md5_check(fh):
16     '''
17     Calculate the md5 of files
18     将文件分块读入内存,防止内存溢出
19     
20     '''
21     
22     fp.seek(0)
23     fr = fp.read(8096)
24     while fr:
25         yield fr
26         fr = fp.read(8096)
27     else:
28         fp.seek(0)
29 
30 
31 if __name__ == '__main__':
32     start = time.clock()
33     filename = input('请输入文件路径:')
34     m = hashlib.md5()
35     with open(filename,'rb') as fp:
36         for fr in md5_check(fp):
37             m.update(fr)    #逐次更新校验值
38     end = time.clock()
39     print(m.hexdigest(),'\n用时: %.3f s' %(end-start))

 

posted @ 2018-07-19 22:25  i_orange  阅读(1900)  评论(0编辑  收藏  举报