python实例26[计算MD5]
2011-03-17 13:20
453 查看
代码如下:
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')
hashlib模块帮助:
http://docs.python.org/library/hashlib.html
完!
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')
hashlib模块帮助:
http://docs.python.org/library/hashlib.html
完!
相关文章推荐
- python实例26[计算MD5]
- python实例26[计算MD5]
- python实例26[计算MD5]
- python练习实例之二 利润分红计算
- python实例26[查询修改文件的属性]
- python实例26[sendemail]
- Python利用pandas计算多个CSV文件数据值的实例
- Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
- python 计算md5
- python计算牛顿迭代多项式实例分析
- 用python计算md5,sha1,crc32
- Python如何计算类的实例个数
- Python 练习实例26
- Python高级应用实例对比:高效计算大文件中的最长行的长度
- Python学习26:类属性vs实例属性
- Python高级应用实例对比:高效计算大文件中的最长行的长度
- python实现对以‘&’连接的参数串排序后计算md5
- python 26 连接 mysql 实例
- python26实例[文件copy和自动rename]
- python实例26[验证用户是否存在于LDAP Server]