Creating new archives
suggest changeTo create a new archive, open the zipfile in write mode.
import zipfile
new_arch=zipfile.ZipFile("filename.zip",mode="w")
To add files to this archive, use the write() method.
new_arch.write('filename.txt','filename_in_archive.txt') # first parameter is the filename, and the second is the name in the archive; by default the filename is used if the second is omitted
new_arch.close()
If you want to write a string or bytes into the archive, you can use the writestr() method. The previous snippet has already closed new_arch, so this call has to happen before close(), or the archive has to be opened again.
str_bytes="string buffer"
new_arch.writestr('filename_string_in_archive.txt',str_bytes)
new_arch.close()
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents