Python

How to Unzip Files in Python

Compressed files take up less space, which makes them easier to transfer over the internet or store on your hard drive. However, they need to be unzipped or extracted before they can be utilized. For instance, if you have a large dataset in a zipped file format, you need to extract it to a folder in order to access its contents.

In this article, we will explore different approaches to unzipping files in Python.

How to Unzip Files in Python?

To unzip files in Python, apply the following approaches:

Method 1: Unzip Files in Python Using “zipfile” Module

The “zip” file can be extracted using the “zipfile” module in Python. This module uses the “extractall()” method to extract all the files from a zip file to a specified directory. The “extract()” method, however, extracts a single file or a list of files from a zip file to a specified directory.

Syntax

ZipFile.extractall(path=None, members=None, pwd=None)

 

ZipFile.extract(member, path=None, pwd=None)

 

The original path and the name of the “zip” file that needs to be extracted throughout the blog are shown in the below snippet:

Example 1: Extract All the Files From a Zip File Using the “zipfile extractall()” Method

Let’s overview the following example code:

import zipfile
with zipfile.ZipFile(r"C:\Users\p\Documents\program\sample.zip", "r") as zip:
    zip.extractall(r"C:\Users\p\Documents\program")

 

In the above code, the “zipfile.ZipFile()” function is used to open the zip file, and the “zip.extractall()” method extracts all the files from the specified zip file.

Output

The above outcome implies that all the files from a zip file have been extracted.

Example 2: Extract a Single File From a Zip File Via the “zipfile extract()” Method

The below code is used to extract a single file from the zip file:

import zipfile
with zipfile.ZipFile(r"C:\Users\p\Documents\program\sample.zip", "r") as zip:
    zip.extract("example.py", r"C:\Users\p\Documents\program")

 

In the above code, the “zipfile.ZipFile()” function is used to open the zip file, and the “zip.extract()” method is applied to extract the specific file from the given zip file.

Output

Based on the above output, the single file from the zip file is extracted.

Method 2: Unzip Files in Python Using the “shutil” Module

Another way to unzip files in Python is via the “shutil” module. This module provides a higher-level interface for working with files and directories. The “unpack_archive()” method of the “shutil” module is used to extract an archive file to a specified directory. This method supports various archive formats, such as “zip”, “tar”, “gz”, etc.

Syntax

shutil.unpack_archive(filename, extract_dir[, format])

 

Example

Go through the below-stated code snippet:

import shutil
shutil.unpack_archive(r"C:\Users\p\Documents\program\sample.zip", r"C:\Users\p\Documents\program", 'zip')

 

According to the above lines of code, the “shutil.unpack_archive()” method takes the zip file’s complete path and the target directory as its arguments, respectively, and unzips the files accordingly.

Output

Based on the above output, both files have been extracted to the specified path appropriately.

Conclusion

The “zipfile” module and the “shutil” module are used to unzip single or multiple files from the specified zip file in Python. The “extractall()” method and “extract()” method of the “zipfile” module are used to unzip the single or all the files of the given zip file, respectively. The “unpack_archive()” method of the “shutil” module is used to extract an archive file to a specified directory. This post presented various ways to unzip the specified zip file using numerous examples.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.