[Python] python分别生成字符串、文件、目录的MD5编码 →→→→→进入此内容的聊天室

来自 , 2020-10-26, 写在 Python, 查看 127 次.
URL http://www.code666.cn/view/ff8c1a3b
  1. from hashlib import md5
  2.  
  3. def calMD5(str):
  4.   m = md5()
  5.   m.update(str)
  6.   return m.hexdigest()
  7.    
  8.  
  9. def calMD5ForFile(file):
  10.   m = md5()
  11.   a_file = open(file, 'rb')
  12.   m.update(a_file.read())
  13.   a_file.close()
  14.   return m.hexdigest()
  15.    
  16. def calMD5ForFolder(dir,MD5File):
  17.   import os
  18.   outfile = open(MD5File,'w')
  19.   for root, subdirs, files in os.walk(dir):
  20.     for file in files:
  21.       filefullpath = os.path.join(root,file)
  22.       print filefullpath
  23.       filerelpath = os.path.relpath(filefullpath,dir)
  24.       md5 = calMD5ForFile(filefullpath)
  25.       outfile.write(filerelpath + ' ' + md5 + '\n')
  26.   outfile.close()
  27.  
  28.  
  29. print calMD5('This is one test string')
  30. print calMD5ForFile('c:\\test\\mytest.txt')
  31. calMD5ForFolder('c:\\test','c:\\mdfile.md5')
  32. #//python/7140

回复 "python分别生成字符串、文件、目录的MD5编码"

这儿你可以回复上面这条便签

captcha