代码如下:

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

 

 

完!

 

相关文章:

  • 2021-07-06
  • 2021-10-08
  • 2022-12-23
  • 2021-11-01
  • 2021-12-10
  • 2022-12-23
  • 2021-11-27
  • 2021-11-28
猜你喜欢
  • 2021-12-30
  • 2021-06-10
  • 2021-12-22
  • 2022-12-23
  • 2021-08-01
  • 2021-08-19
相关资源
相似解决方案