python分别生成字符串、文件、目录的MD5编码,pythonmd5,from hashlib
文章由Byrx.net分享于2019-03-23 07:03:09
python分别生成字符串、文件、目录的MD5编码,pythonmd5,from hashlib
from hashlib import md5def 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')
评论关闭