提取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:"))

评论关闭