方法1

#!/usr/bin/python3
# 产生MD5的方法
import os,hashlib
def return_mdf_one(file_path,Bytes=1024):
        #创建一个MD5算法对象
        mdf_cac = hashlib.md5()
        #打开文件,必须'rb'模式打开
        with open(file_path,'rb') as file:
                while 1:
                        #每次只读取固定字节
                        data =file.read(Bytes)
                        #当读取内容不为空时保持更新
                        if data:
                                mdf_cac.update(data)
                        #整个文件读完之后停止更新
                        else:
                                break
                #获取MD5
        result = mdf_cac.hexdigest()
        return result
# 其中01是文件名
print(return_mdf_one('01'))

方法2

#!/usr/bin/python3
import os,hashlib
def return_mdf_two(file):
        if os.path.isfile(file): 
                file_op = open(file,'rb') 
                contents = file_op.read() 
                file_op.close() 
                reuslt = hashlib.md5(contents).hexdigest()
        else: 
                reuslt = '0'
        return reuslt
# 其中01是文件名
print(return_mdf_two('01'))

Last modification:February 28th, 2021 at 03:39 pm
硬币投入口