提取MD5值一样(或不一样)的文件,和后缀名一样的文件,md5后缀,[Python]代码#-
提取MD5值一样(或不一样)的文件,和后缀名一样的文件,md5后缀,[Python]代码#-
[Python]代码
#-*- coding: utf-8 -*-import osimport hashlibimport shutilDstdir="Files_Extract"if not os.path.isdir(Dstdir): os.makedirs(Dstdir)class Files_Extract: def __init__(self,name): self.name=name def File_MD5(self,file_path): f=open(file_path,'rb') md5=hashlib.md5(f.read()).hexdigest() f.close() return md5 def File_Path_MD5(self,src_path): dct={} for x,y,z in os.walk(src_path): for i in z: file_path=os.path.join(x,i) if os.path.isfile(file_path): md5=self.File_MD5(file_path) dct[file_path]=md5 return dct def Copy_File(self,src,dst): try: shutil.copy(src,dst) print "Extract file .... "+src+"\t.... Success" except IOError,e: print e def Different_MD5(self,src_path,dst_path): dct=self.File_Path_MD5(src_path) d={} for path,md5 in dct.items(): d[md5]=path for md5,path in d.items(): ext_name=os.path.splitext(path) bn=os.path.basename(ext_name[0]) new_name=bn+'_'+md5+ext_name[1] new_path=os.path.join(dst_path,new_name) self.Copy_File(path,new_path) def Same_MD5(self,src_path,dst_path): dct=self.File_Path_MD5(src_path) lst_md5=dct.values() cmp_md5=list(set([x for x in lst_md5 if lst_md5.count(x)>1])) #判断列表中出现了2次以上的元素 for file_md5 in cmp_md5: for path,md5 in dct.items(): if file_md5==md5: self.Copy_File(path,dst_path) def File_Ext(self,ext_arge,src_path,dst_path): dct=self.File_Path_MD5(src_path) d={} for path,md5 in dct.items(): d[md5]=path lst_ext=ext_arge.split(';') for ext in lst_ext: for md5,path in d.items(): ext_name=os.path.splitext(path) if ext==ext_name[1]: bn=os.path.basename(ext_name[0]) new_name=bn+'_'+md5+ext_name[1] new_path=os.path.join(dst_path,new_name) self.Copy_File(path, new_path) def run(self,arge): srcdir=arge.replace('\"', '') if self.name=='1': self.Different_MD5(srcdir,Dstdir) elif self.name=='2': self.Same_MD5(srcdir,Dstdir) elif self.name=='3': r=raw_input("Input the filext into here:") self.File_Ext(r, srcdir, Dstdir) else: print "Input Error!"def doc(): '''===============================================================================@Author: xuzhijian17 2011 MD5 File Extract command ToolCopyright (c) 1990 - 2011 Qianyun TechnologiesProgram version 10.0.1388, engine 10.0.1516Version 1516/3730 ",Date 2011-10-12===============================================================================\n\t\t1.Findall Different MD5 files\n\t\t2.Findall Same MD5 files\n\t\t3.Findall Same ext files\n By: X-Jython------------------------------------------------------------------------------- '''if __name__=='__main__': print doc.__doc__ fe=Files_Extract(raw_input("Enter 1,2 or 3:")) fe.run(raw_input("Drag the folder into here:"))
相关内容
- python发送HTTP请求,python发送请求,GET 方法>>> im
- try...finally 异常,try...finally,异常处理import t
- 文件读写,,文件读写#第九章 文件#
- 字典 暗示,字典,[Python]代码#
- 字典,,字典-基础# 字典#
- python Singleton pattern,pythonsingleton,Python singl
- while循环输出(1-10),while循环输出1-10,[Python]代码i
- python读取目录下文件并生成日志,,[Python]代码
- UTF8文件去BOM,UTF8文件BOM,[Python]代码#!
- python使用sha1加密方法验证文件的有效性,,import hashl
评论关闭