前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3 operate ZIP

python3 operate ZIP

作者头像
py3study
发布2020-01-03 16:32:41
3200
发布2020-01-03 16:32:41
举报
文章被收录于专栏:python3python3

>>>import zipfile , os

//infomation for zip file

>>>zipFileHandle = zipfile.ZipFile('example.zip')   //create zip file handle

>>>zipFileHandle.namelist()      //return all files and directorys in the zip file

>>>spamInfo = zipFileHandle.getinfo('filename') //return a object of file in the zip file

>>>spamInfo.filename     //return filename

>>>spamInfo.file_size    //return file original size

>>>spamInfo.compress_size   // file compressed size

>>>round(spamInfo.compress_size / spamInfo/file_size , 2)    //compress rate

//unzip

>>>zipFileHandle.extractall()      //unzip all to current directory

>>>zipFileHandle.extract('filename')   //unzip 'filename' file to current directory

//close handle

>>>zipFileHandle.close()

//create a new zip file

>>>newZip = zipfile.ZipFile('new.zip','w')

>>>newZip.write('filename' , compress_type=zipfile.ZIP_DEFLATED)

>>>newZip.close()

//add files into a zip file

>>>zipFileHandle = zipfile.ZipFile('oldfile.zip' , 'a')

>>>zipFileHandle.write('filename' , compress_type=zipfile.ZIP_DEFLATED)

>>>>>>zipFileHandle.close()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档