The ZipFile module gives the instruments important to process compress records. The ZipFile class is accessible for perusing, composing, and adding documents to a current compress record in this module. How about we begin by taking in a zipfile module with a little example code ...
import zipfile
zip_file = zipfile.ZipFile("zip_filex.zip","r")
for file in zip_file.namelist():
print file
In the code above we imported our module and opened the zip_file.zip file by reading ("r") (we do not have to write the "r" parameter if we open it with read mode).
The namelist () method of the ZipFile class returns a list of the files in the zip file. We are printing this list with the for loop. In our zip file, we assume that the document.odt and document.pdf files are in our subdirectory.
In this example we assume that the zip_file.zip file is a zip file. So what can we do to make sure that a file is a zip file? Of course, thanks to a function in the ZipFile module:
zip_mi = zipfile.is_zipfile("zip_filex.zip")
if zip_mi:
print "This file is a real zip file."
else:
print "This is not a zip file."
We can see from the value that zip_file.zip file that we input as parameter with is_zipfile () function returns true. If true value is returned, you will see if the blocking process. If it is False, else block ...
The ZipFile class provides three methods for extracting files from the zip archive. Let's write a small piece of code that will let us learn the functions of these three methods.
zip_file.extract("thedoc.pdf", path=None)
zip_file.extractall(path=None, members=None)
filex = zip_file.read("thedoc.pdf")
filex_write = open("thedoc.pdf,"wb")
filex_write.write(file)
filex_write.close()
We added way and individuals parameters to appear in our example code; on this page we'll have our strategies completely learned in the capacity ... way parameter is utilized to determine where to get the record (s).
As a matter of course, the concentrate () strategy takes the document indicated in the principal parameter to an indistinguishable registry from the compress file, however we can remove the record in any field that can be composed with the way parameter.
extractall () enables you to remove all records in the zip chronicle. In the event that we give a rundown of a portion of the documents in the compress chronicle to the individuals parameter, this time it will remove the records determined in the rundown.
The most difficult technique is the perused () strategy. Instead of separating a document, it will read that record. We can compose this read document to the record as found in our case above. read () methodu could be utilized to peruse a lot of documents, for example, the record protest, and in addition read advance.
The concentrate () and extractAll () techniques are incorporated into the Python 2.6 discharge. Additionally.
To make a zip document we utilize the "w" parameter as in open ().
import zipfile
zip_filex = zipfile.ZipFile("zip_file.zip","w")
zip_filex.write("thedoc.pdf")
zip_filex.write("thedoc.odt")
zip_file.close()
As should be obvious, it's relatively indistinguishable to the ordinary record composing we did with the open () work. The compose () strategy takes a moment parameter. This parameter will be entered as the name of the compress record of the document you are composing in the string information compress document.
zip_filex.write("thedoc.pdf","thedoc2.pdf")
The file "thedoc.pdf" that we wrote with this code is written as archives "thedoc2.pdf".
In archiving software, you used the part where we set the compression ratio of the zip file. You can specify the compression ratio with a third parameter to the ZipFile class.
zip_filex = zipfile.ZipFile("zip_thedoc.zip","w",8)
Pressure proportion ought to be 0-8. The ZipFile module contains zipfile.ZIP_STORED for pressure of 0 (zero), and zipfile.ZIP_DEFLATED for pressure of 8 which is the best pressure. The default parameter esteem is zipfile.ZIP_STORED. You can likewise indicate a pressure proportion for each record by setting the compress_type parameter to 0-8 while including the document with the compose () technique.
We utilize the "a" parameter rather than the "w" parameter keeping in mind the end goal to open a current compress record and secure the substance. We can do all the rest of the activities as we did with the "w" parameter.