#coding=utf-8 import os import hashlib def ergodic(startpath): cu_path=os.getcwd() os.chdir(startpath) file_list=os.listdir(startpath) for i in file_list: fullname=os.path.abspath(i) if os.path.isdir(fullname): try: ergodic(fullname) except WindowsError: print fullname+" doesn‘t exist." elif os.path.isfile(fullname): #f=open(‘E:\\result.txt‘,‘a‘) #f.write(fullname+‘\r\n‘) #f.close() ex_name=fullname[-3:].lower() if ex_name in f_type_list: md5sum(fullname) os.chdir(cu_path) def md5sum(filename): fd = open(filename,"rb") fcont = fd.read() fd.close() fmd5 = hashlib.md5(fcont).hexdigest() r=open(‘E:\\result.txt‘,‘a‘) r.write(filename+‘\t‘+fmd5+‘\r\n‘) r.close() if __name__=="__main__": try: os.remove(‘E:\\result.txt‘) except WindowsError: print "E:\\result.txt doesn‘t exist." search_path="D:\\" f_type_list=(‘rar‘,‘zip‘) ergodic(search_path)
原文地址:http://hakaz.blog.51cto.com/5067476/1657318