#-*- coding:utf-8 -*-import shutilimport osfrom Tkinter import *import timeimport redef get_all_file(adr): all_adr=[] all_dir.append(adr) adr+='\\' if os.path.exists(adr): try: all_adr=os.listdir(adr) except Exception,e: print e for i in all_adr: #print adr+i if os.path.isfile(adr+i): file.append(adr+i) dict_file[adr+i]=str(int(get_file_size(adr+i))/1024) if os.path.isdir(adr+i): get_all_file(adr+i) def get_file_size(adr): if os.path.isfile(adr): return os.path.getsize(adr) def search_file(file,key): search_result=[] for f in file: for k in key: if k in f.lower(): print f shutil.copyfile(f,'D:\\result\\'+os.path.basename(f)) search_result.append(f) return search_result if __name__=='__main__': dict_file={} adr='E:\\' all_dir=[] file=[] get_all_file(adr) with open(r'C:\Users\cchen\Desktop\allfile.txt','w+') as f: for i in file: #print i+'\t'+dict_file[i] f.write(i+'\t'+dict_file[i]+'\n') ''' for j in all_dir: print j ''' search_key=['.jpg','.png','jpeg','gif','bmp'] with open(r'C:\Users\cchen\Desktop\searchfile.txt','w+') as sf: for i in search_file(file,search_key): print i+'\t'+dict_file[i] sf.write(i+'\t'+dict_file[i]+'\n')