码迷,mamicode.com
首页 > 编程语言 > 详细

python3 operate ZIP file

时间:2016-11-20 16:32:35      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:python3 zip

>>>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()








本文出自 “希夷” 博客,谢绝转载!

python3 operate ZIP file

标签:python3 zip

原文地址:http://lhjdark.blog.51cto.com/6780582/1874701

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!