Python Language Creating new archives


Example

To create new archive open zipfile with write mode.

import zipfile
new_arch=zipfile.ZipFile("filename.zip",mode="w")

To add files to this archive use write() method.

new_arch.write('filename.txt','filename_in_archive.txt') #first parameter is filename and second parameter is filename in archive by default filename will taken if not provided
new_arch.close()

If you want to write string of bytes into the archive you can use writestr() method.

str_bytes="string buffer"
new_arch.writestr('filename_string_in_archive.txt',str_bytes)
new_arch.close()