How to unzip files in python?

This article describes how to unzip different files in Python. We will utilize certain built-in features, simple methodologies, and specific custom codes here. First, let’s discuss the zip file and why it is used.

Zip is a file format that allows us to reconstruct the initial data from the compressed data completely. It is a particular file containing one or many compacted files. It is straightforward to shrink the large files and combine associated files. In Python, ZipFile is a module of Zipfile class for reading and writing different zip files. A zip file is required to reduce the memory mark and recover transmission speeds over some usual connections.

The zip folder consists of multiple files. To utilize the components of the zip folder, we have to access those folders to excerpt the documents contained in the folder. Let’s discuss several methods to unzip a folder or a file that is used in Python and save that file or a folder in identical or different directories.

Python Zipfile Module:

In Python, the module named a ZipFile offers numerous ways for handling folder or file compression processes. It utilizes Situation Manager Construction. The function known as Extractall() function is utilized to excerpt all the files and folders that exist in the zip file. We may utilize functions such as a file.extractall() function to extract the contents of files in a similar directory and different directories.

Extraction of all files to the existing directory:

In this instance, we import the zipfile module. There is a zip file in the existing directory. To unzip this file, first, we open this file in a readable manner to construct a ZipFile object, then we call the file.extractall() function on that item. Extractall() is a function that excerpts all files or folders in the existing directories. If a path of the file argument is specified, the path will be overwritten.

To run Python code, first, download and install the spyder version 5. We now make a new file named by pressing “Ctrl+N” on the keyboard.

By this code, we extract the file in an existing directory.

Extraction of all files to a diverse directory:

In this case, the directories do not exist; thus, first, we have to name the different directories as “Directory” and place all the files that are excerpted from “” Now we pass the target position as a parameter to the extractall() function. Here we pass the target location, the ‘directory.’ This path may be comparative or absolute.

Extract particular files to a different directory:

This technique simply unzips and excerpts a list of specific files from the folder in the document. We may only unzip the folders we want to pass in a list of filenames. In this example, we import two modules. We utilize a data of 40 students i.e. std1, std2, std3, std4, std5,…, std40. We only want to excerpt the information for those students with serial numbers 22, 14, and 8. So we create a list comprising the designations of the required files and pass that list as an argument to the function extractll().

Here is the function file.extractall() excerpts the data of files in different directories. In this case, ‘files_list’ displays a list of files to be extracted. If it is not indicated, then all the files will be extracted. We also use the loop to print all the required files.

Python Shutil Module:

Zipfile delivers a particular function for unzipping files, but it’s a slightly inferior level library segment. As an alternative to Zipfile, there is a shutil component. This is an advanced level of functionality associated with zipfile. As it accomplishes elevated processes on files and folders. Extract the file by using unpack.archive().

Extraction of the file to a different directory by using shutil module:

In this code, first, we import the shutil module. Then we provide the path to the file. The file we want to extract is ‘’ and is placed in a download folder. Next, we give the target directory.

For extracting the file, we utilize unpack_archive. Based on the extension of the file title, it recognizes the determination format automatically. Here we use the .zip extension for the name of the file. We pass two arguments to this, i.e., file and extract_directory. The first parameter, ‘file’ signifies the complete path of a file. The second parameter, ‘extract_directory’ expresses the path of the objective directory from which the file will be extracted. It is optional. If it is not specified, the existing working directory will be utilized as the objective directory.


We learned about zip files and the utilization of zip files. We also learned about different approaches that are used for unzipping a file. The files can be extracted by the use of some built-in methods. Extractall(), Shutil() are those built-in methods. In this article, we extract the folder to the existing directory, and also we do the extraction of the folders to different directories by using the shutil module.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content