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)版权声明:本文标题:快速上手Python:轻松实现SWF文件压缩,提升性能 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1774500321a3571878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论