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)
python求文件哈希值
最新推荐文章于 2024-04-12 10:15:03 发布