您的位置:首页 > 编程语言 > Python开发

python实例26[计算MD5]

2015-06-19 14:15 816 查看
代码如下:

<span style="font-size:14px;">from hashlib import md5

def calMD5(str):
m = md5()
m.update(str)
return m.hexdigest()

def calMD5ForFile(file):
m = md5()
a_file = open(file, 'rb')
m.update(a_file.read())
a_file.close()
return m.hexdigest()

def calMD5ForFolder(dir,MD5File):
import os
outfile = open(MD5File,'w')
for root, subdirs, files in os.walk(dir):
for file in files:
filefullpath = os.path.join(root,file)
print filefullpath
filerelpath = os.path.relpath(filefullpath,dir)
md5 = calMD5ForFile(filefullpath)
outfile.write(filerelpath + ' ' + md5 + '\n')
outfile.close()

print calMD5('This is one test string')
print calMD5ForFile('c:\\test\\mytest.txt')
calMD5ForFolder('c:\\test','c:\\mdfile.md5')</span>


hashlib模块帮助:
http://docs.python.org/library/hashlib.html 转载:python实例26[计算MD5]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: