admin 管理员组

文章数量: 1184232

import zipfile, os
classZip:
    readme ="""
        input_path,out_path, filelist,zipfilename
        input_path,out_path,zipfilename type is str
    
    """def__init__(self,input_path,out_path, filelist,filename,pathflag ='/'):
        self.input_path = input_path
        self.out_path = out_path
        self.filelist = filelist
        self.filename = filename
        self._pathflag = pathflag
    defcheck(self):for date in[self.input_path, self.out_path, self.filename]:for ci in date:iftype(ci)isstr:passelse:return-1iftype(self.filelist)islist:passelse:return-1defget_zip_file(self,input_path:str, filelist:list,):"""
        对目录进行深度优先遍历
        :param input_path:
        :param result:
        :return:
        """
        result =[]
        tmp_filelist = filelist
        files = os.listdir(input_path)forfilein files:if os.path.isdir(input_path + self._pathflag +file):pass#self.get_zip_file(input_path + '/' + file,filelist, result)else:iffilein filelist andfilenotin result:
                    result.append(input_path +self._pathflag +file)
                    tmp_filelist.remove(file)iflen(tmp_filelist)!=0:for i in tmp_filelist:print('not found file'+str(input_path)+str(i))return-1else:return result
    defzip_file_path(self):"""
        压缩文件
        :param input_path: 压缩的文件夹路径
        :param output_path: 解压(输出)的路径
        :param output_name: 压缩包名称
        :return:
        """
        filelists_path =  self.get_zip_file(self.input_path, self.filelist,)if filelists_path !=-1:
            f = zipfile.ZipFile(self.out_path + self._pathflag + self.filename,'w', zipfile.ZIP_DEFLATED)forfilein filelists_path:
                f.write(file)# 调用了close方法才会保证完成压缩
            f.close()return self.out_path + self.filename
        return'获取压缩文件失败!'defZip(self):if self.check()==-1:return-1print(self.zip_file_path())if __name__ =='__main__':
    Zip('C:\\Users\\11494\\Desktop\\emc\\','C:\\Users\\11494\\Desktop\\',['132.csv','125.csv','180 12.11.csv'],'ziptest.zip',pathflag='\\').Zip()print(Zip.readme)

本文标签: 快速上手 编程 系统