zipfile module doesn't allow append

Roger Miller roger.miller at
Thu Jun 29 23:51:49 CEST 2006

Ritesh Raj Sarraf wrote:

> The line
> filename = zipfile.ZipFile(zip_file_name, "a")
> throws an exception if the given filename is not present already.
> Shouldn't it create a file (in case one is not there) since it is
> "append" mode ??

Perhaps it would be nicer that way, but it is working as documented.
Catch the exception and open in 'w' mode.

To anticipate your next possible problem, note that in append mode if
you write a file that already exists in the archive it will not replace
the existing file, but will add another one with the same name. As far
as I can tell, there is no way to read the newer version because always finds the first version. So if you are trying
to update a zipfile you will probably have to read the old archive and
write a new one, copying the files you want to keep and replacing the
ones you want to update. (At this point you might want to consider
invoking an external zip utility instead.)

More information about the Python-list mailing list