python如何附加到zip存档中的文件 [英] python how to append to file in zip archive

查看:40
本文介绍了python如何附加到zip存档中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我做这样的事情:

from zipfile import ZipFile 

zip = ZipFile(archive, "a")  

for x in range(5):
    zip.writestr("file1.txt", "blabla")

它将创建一个包含5个文件的存档,所有文件均名为"file1.txt".我要实现的是有一个压缩文件,每个循环迭代都会在其中添加一些内容.没有某种辅助缓冲区是否可以解决?

It will create an archive with 5 files all named "file1.txt". What I want to achieve is having one compressed file to which every loop iteration appends some content. Is it possible without having some kind of auxiliary buffer and how to do this?

推荐答案

使用zipfile包是不可能的,但是

It's impossible with zipfile package but writing to compressed files is supported in gzip:

import gzip
content = "Lots of content here"
f = gzip.open('/home/joe/file.txt.gz', 'wb')
f.write(content)
f.close()

这篇关于python如何附加到zip存档中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆