我想使用pythons zipfile模块更新zip文件中的条目.我的问题是,这会生成一个新条目.
请假设我有这个代码:
from zipfile import ZipFile,ZIP_DEFLATED
with ZipFile("myfile.zip","w") as z:
z.writestr("hello.txt", "the content of hello.txt", ZIP_DEFLATED)
### how to update the hello.txt file here ?
z.writestr("hello.txt", "the content of hello.txt", ZIP_DEFLATED)
Run Code Online (Sandbox Code Playgroud)
在此之后,实际的zip文件有两个条目而不是一个:
$ unzip -l myfile.zip
Archive: myfile.zip
Length Date Time Name
--------- ---------- ----- ----
24 2013-02-19 22:48 hello.txt
24 2013-02-19 22:48 hello.txt
--------- -------
48 2 files
$ python --version
Python 3.3.0
$
Run Code Online (Sandbox Code Playgroud)
我知道编写一个完整的新文件的方法,但如果内容很大,这将花费很多时间.
该拉链(1)实用程序可以做到这一点(使用"-u"选项),那么为什么不蟒蛇?有什么方法我仍然可以使用python实现这一点?
谢谢