玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > Python源码实例 > 文本与文件处理源码 > 正文内容

Python os.path计算文件md5值源码

#-*- encoding=utf-8 -*-
import io   
import sys   
import hashlib   
import string   
import os

def calMd5(afile):   
   m = hashlib.md5()   
   file = io.FileIO(afile,'r')   
   bytes = file.read(1024)   
   while(bytes != b''):   
      m.update(bytes)   
      bytes = file.read(1024)    
   file.close()    
   md5value = m.hexdigest()   
   return  md5value
   
def visitor(args, directoryName,filesInDirectory):     # called for each dir 
   print "\t"*(args-1),directoryName
   for fname in filesInDirectory:                   
      fpath = os.path.join(directoryName, fname)    
      if not os.path.isdir(fpath):                   
         print "\t"*args,fname,"\t",calMd5(fpath)

def calDirMd5(startdir, level):
   os.path.walk(startdir, visitor, level+1)
    
if __name__ == '__main__':
   root=raw_input("type root directory:")
   calDirMd5(root,0)

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/code/text-file/tf2526.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2016年03月29日 - 14时21分56秒 发布自玩蛇网

我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules