close() - This method closes archive after writing important metadata.Please make a note that if the zip archive is opened in reading mode and we try to write to file then it'll fail with ValueError. 'w' - It let us write contents to the file.'r' - It let us read the contents of the file.The mode parameter accepts below-mentioned values. The name parameter accepts the file name to open. It returns a file-like object which we can utilize as per our need. open(name, mode='r') - It let us work with an individual member of the archive.The True value indicates that when compressing file size larger than 4 GB, it'll create zip files with zip64 extension. allowZip64 - This parameter accepts boolean value.The lower values will compress fast but low compression whereas higher values will take time but compresses most. compresslevel - This parameter accepts value between 0-9 for ZIP_DEFLATED and 1-9 for ZIP_BZIP2.'zipfile.ZIP_LZMA' - It requires python module lzma installed to work.'zipfile.ZIP_BZIP2' - It requires python module bz2 installed to work.'zipfile.ZIP_DEFLATED' - It requires python module zlib installed to work.compression - This parameter accepts one of the below values specifying the compression algorithm.'x' - It creates an archive and writes files to it.'a' - For appending new files to an existing zip archive.It'll create a zip archive if it does not exist and overwrite the contents of the existing zip archive. mode - It accepts one of the below-mentioned characters as input.file - It accepts string specifying zip file name.It let us open, read, write, append to zip files. ZipFile(file,mode='r',compression=ZIP_STORED,allowZip64=True,compresslevel=None) - This class lets us work with a zip file given as first parameter. All sections will have information about class and methods listed which can be referred to check for important parameters. We have decoded the contents of the file as it’s returned as bytes.īelow, we have listed class and methods used in example. It then reads the contents of the file, prints it, and closes the archive. Our code for this example starts by creating an instance of ZipFile with zip archive and then opens text file inside of it using open() method of ZipFile instance. We'll be using class ZipFile and its methods for this purpose. Reading the Contents of Existing Zip File ¶Īs a part of our first example, we'll explain how we can open an existing zip archive and read the contents of the file stored in it. We have also created one archive with only this file using our Linux tools which we'll try to read in our first example.ġ. Below we have printed the contents of the file. We have created one simple text file that we'll be using when explaining various examples.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |