Python comes with many standard libraries, and here, in this Python tutorial, we will learn about a Python zip file in detail. This article explains how to compress (zip) and extract (or un-zip) files.
What is a Zip File?
The zip file is an archive file format that uses algorithms to compress files without losing the data. When files are compressed to a zip archive, the size of the file reduces, but the data remain the same. Zip provides an ideal way to compress or reduce the size of files and collect them in a single folder or directory.
Why Use a Zip File?
- Zip files reduce the size of files. It reduces the storage space occupied by one or many files without losing data.
- You can bind together many files in a single zip file.
- With reduced size, it is very easy to send zip files over networks.
Creating a Zip File in Python Using the zipfile Module
Python comes with a standard
module, which can compress files into a zip file.
Let’s write Python code that will create a zip file.
Here we are in a folder named dev. Now, you can see that we have three files, namely
file contains the Python script that will create a zip file by compressing
Python Program to Create a Zip File
import zipfile print("Creating Zip File.../") with zipfile.ZipFile("file.zip", "w", compression=zipfile.ZIP_DEFLATED) as create_zip: create_zip.write("demo.txt") #compress the demo.txt file into file.zip create_zip.write("sublime.png") #compress the sublime.png file into file.zip print("Zip file by name file.zip has been created")
Execute the Python Program
The above code will create a zip file with the name
in the same folder or directory where the python script, i.e.,
is located. Now when you open the same directory, you will see the zip file created there.
Behind the Code
Similar to the Python file handling method, to create a zip file, the
module provides the ZipFile method. In the
method, we specified three parameters
. This means we want to write compressed files into the
file. If this file is not available, this statement will create a file.zip file. Else, it will write in the existing
file. The create_zip.write(“demo.txt”) and create_zip.write(“sublime.png”) statements copy and compress the files into a
Extracting a Zip File in Python Using the zipfile Module
Now you know how to create a zip file. Next, let’s discuss how you can extract a zip file using the Python
module. As we have used the write mode, “
”, to create a zip file, similarly, we can use the read mode, “
”, to extract or read files from a zip file.
In the dev folder, we have a file by name file.zip . Let’s write a Python program to extract the file.zip file.
Python Program to Unzip (Extract) a Zip File
import zipfile print("Extracting file.zip File.../") with zipfile.ZipFile("file.zip", "r") as extract_zip: #extract all files and save them in folder Extracted files extract_zip.extractall("Extracted Files") print("Zip file by name file.zip has been created")
Execute the Program
After executing the file, you will find the
folder in the development folder where your
Behind the Code
Similar to the zip file creating technique, we used the zip file handling technique to extract the zip files. Using the statement
we specified that we want to read the file.zip file, and with the
statement, we extracted the
method extracts all the files present in the zip file. To extract specific files from the zip file, we can use the extract method. For instance, if we only want to extract the
file from the file.zip, we could have used the following code:
In this Python tutorial, we learned how to create and extract zip files using Python. Python provides a standard library,
, to create and extract zip files. The
module is very useful when you want to create an automated script. In the
method is used to open the zip file for reading and writing. While creating a zip file, we need to specify the
parameter to compress the files.