[crazyant@localhost PythonMd5]$ more sample_file www.crazyant.net www.51projob.com [crazyant@localhost PythonMd5]$ md5sum sample_file > sample_file.md5file [crazyant@localhost PythonMd5]$ more sample_file.md5file 311d384505e3622ccf85d88930e2b0a0 sample_file [crazyant@localhost PythonMd5]$ md5sum -c sample_file.md5file sample_file: OK
# -*- encoding:utf-8 -*-
from hashlib import md5
import os
def generate_file_md5value(fpath):
'''以文件路径作为参数,返回对文件md5后的值
'''
m = md5()
# 需要使用二进制格式读取文件内容
a_file = open(fpath, 'rb')
m.update(a_file.read())
a_file.close()
return m.hexdigest()
def generate_file_md5sumFile(fpath):
fname = os.path.basename(fpath)
fpath_md5 = "%s.md5" % fpath
fout = open(fpath_md5, "w")
fout.write("%s %s\n" % (generate_file_md5value(fpath), fname.strip()))
print "generate success, fpath:%s" % fpath_md5
fout.flush()
fout.close()
if __name__ == "__main__":
fpath = "/home/users/workbench/PythonMd5/sample_file"
# 测试一:以文件路径作为参数,获得md5后的字符串
print generate_file_md5value(fpath)
# 测试二:生成和linux命令:md5sum同样结果的.md5文件
generate_file_md5sumFile(fpath)
[crazyant@localhost PythonMd5]$ python generateMd5file.py 311d384505e3622ccf85d88930e2b0a0 generate success, fpath:/home/crazyant/workbench/PythonMd5/sample_file.md5 [crazyant@localhost PythonMd5]$ md5sum -c sample_file.md5 sample_file: OK
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有