#encoding=utf-8 #author: skybug #date: 2014-04-23 #function: 遍历目录所有文件,计算每个文件的md5 输出到md5.txt import os,sys,getopt,hashlib logfile='md5.txt' limt=8388608 #(8M) def listdir(dir): global logfile logfile = dir + os.path.sep + logfile for files in os.listdir(dir) : path=os.path.join(dir,files) if os.path.isdir(path): continue else: filepath = dir + os.path.sep + files line = getfilemd5(filepath) w_log(logfile,line) def getfilemd5(filepath): global limt size = os.path.getsize(filepath) f=open(filepath,'rb') if size < int(limt)+1: m1=hashlib.md5() m1.update(f.read(limt)) myhash = m1.hexdigest().upper() line = filepath + ';' + myhash f.close() return line #print 'if' + '**'+ line else: m1=hashlib.md5() while True: block=f.read(limt) if not block: break m1.update(block) myhash = m1.hexdigest().upper() line = filepath + ';' + myhash f.close() return line def w_log(log_file_name, line): try: log_file = open(log_file_name, 'a') line=line+'\n' log_file.write(line) except IOError as err: print('File error: ' + str(err)) finally: if 'file' in locals(): log_file.close() dir = raw_input('please input dir:\n') listdir(dir)