-
ZipFile.write(filename[,arcname[, compress_type]])
-
Write the file named filename to the archive, giving it the archive namearcname (by default, this will be the same as filename, but without a drive letter and with leading path separators removed). If given,compress_type overrides the value given for the compression parameter to the constructor for the new entry. The archive must be open with mode'w' or 'a' – calling write() on a ZipFile created with mode'r' will raise a RuntimeError. Callingwrite() on a closed ZipFile will raise aRuntimeError.
Note
There is no official file name encoding for ZIP files. If you have unicode file names, you must convert them to byte strings in your desired encoding before passing them towrite(). WinZip interprets all file names as encoded in CP437, also known as DOS Latin.
Note
Archive names should be relative to the archive root, that is, they should not start with a path separator.
Note
If arcname (orfilename, if arcname is not given) contains a null byte, the name of the file in the archive will be truncated at the null byte.
-
ZipFile.writestr(zinfo_or_arcname,bytes[, compress_type])
-
Write the string bytes to the archive; zinfo_or_arcname is either the file name it will be given in the archive, or aZipInfo instance. If it’s an instance, at least the filename, date, and time must be given. If it’s a name, the date and time is set to the current date and time. The archive must be opened with mode'w' or 'a' – calling writestr() on a ZipFile created with mode'r' will raise a RuntimeError. Callingwritestr() on a closed ZipFile will raise aRuntimeError.
If given, compress_type overrides the value given for thecompression parameter to the constructor for the new entry, or in the zinfo_or_arcname (if that is a ZipInfo instance).
Note
When passing a ZipInfo instance as thezinfo_or_arcname parameter, the compression method used will be that specified in thecompress_type member of the given ZipInfo instance. By default, theZipInfo constructor sets this member toZIP_STORED.
Changed in version 2.7:The compression_type argument.
The following data attributes are also available:
-
ZipFile.debug
- The level of debug output to use. This may be set from0 (the default, no output) to3 (the most output). Debugging information is written tosys.stdout.
-
ZipFile.comment
- The comment text associated with the ZIP file. If assigning a comment to aZipFile instance created with mode ‘a’ or ‘w’, this should be a string no longer than 65535 bytes. Comments longer than this will be truncated in the written archive when ZipFile.close() is called.