python实例26[计算MD5]
2013-03-24 19:31
567 查看
代码如下:
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计算两个矩形框重合百分比的实例
- Python 26进制计算实现方法
- python计算auc指标实例
- Python+matplotlib实现计算两个信号的交叉谱密度实例
- python md5计算
- python计算牛顿迭代多项式实例分析
- python进行中文文本聚类实例(TFIDF计算、词袋构建)
- Python+pandas计算数据相关系数的实例
- 测试python计算MD5和CRC的速度
- 面向对象的程序设计-26-对于商品库存量计算的实例
- Python实例讲解 -- 获取本地时间日期(日期计算)
- python实例26[将log同时写入文件和显示到UI的控件中]
- Python的加密模块md5、sha、crypt使用实例
- 【Python】计算文件MD5 和 SHA1
- python 计算两个日期相差多少个月实例代码
- Python实例讲解 -- 获取本地时间日期(日期计算)