The zip file is a format that is composed of files. This composition is lossless compression and requires less storage space. It offers good data transfer and eases the process of sharing files by comprising them into a single zip file. after transferring them, we need to extract these files for further use. In this article, we will examine how we can unzip a file in Python. Let’s get started.
How to Unzip Files in Python?
We can unzip/extract a single or multiple files from a zip file by using extract() and extractall() methods respectively. We will be discussing the method to extract all the files from the zip file first.
Extracting all files from a zip file in Python
We can extract all the files from a zip file in Python using the extractall() method. These files are by default extracted to the current directory you are working in. In case you need to extract these files to some other directory you will need to specify the directory path. The syntax for this method is:
ZipFile.extractall(file_path , members=None, pwd=None)
In the above syntax:
- The extractall() method contains 3 parameters.
- The first parameter “file_path” is the path to which you want to extract all the archived files. If this path is not specified, the files will be extracted to the current working directory by default.
- The “members” parameter specifies the list of files that need to be extracted. If the “members” parameter is missing all the files will be extracted.
- The third parameter, “pwd” is the password in case the files are encrypted. By default this parameter equals none.
Let’s get into some examples to make it see how it works.
Example: Extracting all the files from a zip file
To extract all the files from the zip file, we will first need to import the zipfile module. After this, by using ZipFile class we will create a zipfile object. We will then call an “extractall()” method on the zip file object. This method will require some parameters like; the path or file_pathh where we need to extract the files and the ”members” the specific files we need to extract from the zip file.
Let’s consider the following code for extracting a file from a zip file.
# import the zipfile module
from zipfile import ZipFile
# loading the temp.zip and creating a zip object
with ZipFile("C:\\Users\\sai mohan pulamolu\\\
Desktop\\geeks_dir\\temp\\temp.zip", 'r') as zObject:
# Extracting all the members of the zip
# into a specific location.
zObject.extractall(
path="C:\\Users\\sai mohan pulamolu\\Desktop\\geeks_dir\\temp")
In the above code, first import a zipfile module. Then we loaded the path of zipfile to this module. The next step is to apply the extractall() method and specify the path where the files will be extracted. her e we have not specified the “members” parameter which means that all the files will be extracted. In the output image, we can see the results of the code:
Extracting a specific file from a zip file in Python
To extract a file from the zip file, we will first need to import the zipfile module. After this, by using ZipFile class we will create a zipfile object. We will then call an “extract()” method on the zip file object. This method will require some parameters like; the path or file_pathh where we need to extract the file and the ”members” the specific file name that we want to extract from the zip file.
Let’s see how this extract() method works.
# importing the zipfile module
from zipfile import ZipFile
# loading the temp.zip and creating a zip object
with ZipFile("C:\\Users\\sai mohan pulamolu\\Desktop\
\\geeks_dir\\temp\\temp.zip", 'r') as zObject:
# Extracting specific file in the zip
# into a specific location.
zObject.extract(
"text1.txt", path="C:\\Users\\sai mohan pulamolu\\D\
esktop\\geeks_dir\\temp")
zObject.close()
In the above code, we have just specified the name of the file that we want to extract from the zip file. The output will look like this.
Conclusion
Zip files are sometimes of great use, especially when we want to transfer the files. Afterwards, to use these files, we need to unzip them. In this post, we have seen the methods to unzip a file in Python and to extract single or multiple files from the zip file.