python求文件哈希值

import os,sys,hashlib
_FILE_SLIM=(100*1024*1024)
def file_md5(filename):
    calltimes=0
    hmd5=hashlib.md5()
    fp=open(filename,'rb')
    f_size=os.stat(filename).st_size
    if f_size>_FILE_SLIM:
        while (f_size>_FILE_SLIM):
            hmd5.update(fp.read(_FILE_SLIM))
            f_size/=_FILE_SLIM
            calltimes+=1
        if (f_size>0 ) and (f_size<_FILE_SLIM):
            hmd5.update(fp.read())
    hmd5.update(fp.read())

    return (hmd5.hexdigest(),calltimes)
if  __name__ == "__main__":
    if len(sys.argv)==2:
        filepath=os.path.abspath(sys.argv[1])
        (hvalue,ctimes)=file_md5(filepath)
        print(hvalue)

猜你喜欢

转载自blog.csdn.net/shannow_123/article/details/88241523
今日推荐