python zipfile模块,python zipfile模块似乎没有压缩我的文件

I made a little helper function:

import zipfile

def main(archive_list=[],zfilename='default.zip'):

print zfilename

zout = zipfile.ZipFile(zfilename, "w")

for fname in archive_list:

print "writing: ", fname

zout.write(fname)

zout.close()

if __name__ == '__main__':

main()

The problem is that all my files are NOT being COMPRESSED! The files are the same size and, effectively, just the extension is being change to ".zip" (from ".xls" in this case).

I'm running python 2.5 on winXP sp2.

解决方案

This is because ZipFile requires you to specify the compression method. If you don't specify it, it assumes the compression method to be zipfile.ZIP_STORED, which only stores the files without compressing them. You need to specify the method to be zipfile.ZIP_DEFLATED. You will need to have the zlib module installed for this (it is usually installed by default).

import zipfile

def main(archive_list=[],zfilename='default.zip'):

print zfilename

zout = zipfile.ZipFile(zfilename, "w", zipfile.ZIP_DEFLATED) #

for fname in archive_list:

print "writing: ", fname

zout.write(fname)

zout.close()

if __name__ == '__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值