How to unzip a file in python – Python: How to unzip a file | Extract Single, multiple or all files from a ZIP archive | Syntax for ZipFile

How to unzip a file in python: In this tutorial, we will discuss different ways to unzip or extract single, multiple, or all files from the zip archive to a current or different directory. Also, you can learn what is a zip file and python unzip file along with the syntax. Moreover, let’s check the reasons to use the Zip Files in python. Click on the direct links available below and happily understand what you required regarding How to unzip files in Python?

What is Zip File?

Unzip a file in python: A ZIP file is a single file containing one or more compressed files, it is an easy way to make large files smaller and keep related files together.

Why do we need zip files?

  • To lessen storage requirements.
  • To develop transfer speed over standard connections.

Python Unzip File

Unzip in python: ZipFile class provides a member function to extract all the data from a ZIP archive. ZIP is the archive file format that supports lossless data compression.

In order to unzip a file in Python, make use of this ZipFile.extractall() method. The extractall() method takes a path, members, pwd as an argument, and extracts all the contents.

Also Check:

Syntax for ZipFile

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

It accepts three arguments-

1. path: It shows the location where we want to extract our file

2. members:  It shows the list of files to be extracted

3. pwd: If the zip file is encrypted then pass the password in this argument default it will be None

Example of Python unzip

Python unzip zip file: Let’s assume, in my current working directory, I have a zip file called Mail3.zip, and I wanted to unzip it with the help of the ZipFile.extractall() method. So, check out the following example program code:

# app.py

from zipfile import ZipFile

with ZipFile('Mail3.zip', 'r') as zipObj:
   # Extract all the contents of zip file in current directory
   zipObj.extractall()

Output:

python3 app.py

Lets’ first, import the zipfile module in our program,

from zipfile import ZipFile

Extract all files from a zip file to the current directory

Unzip file python: So if want to zip file ‘program.zip’. in our current directory, let’s see how to extract all files from it.
To unzip it first create a ZipFile object by opening the zip file in read mode and then call extractall() on that object.

# app.py

from zipfile import ZipFile

with ZipFile('program.zip', 'r') as zipObj:
   # Extract all the contents of zip file in current directory
   zipObj.extractall()

In the output, it will extract the files in the same directory as your programming app.py file.

Extract all files from a zip file to Different Directory

Python unzip: To extract all the files from a zip file to another directory we can pass the destination location as an argument in extractall().

# app.py 
from zipfile import ZipFile
# Create a ZipFile Object and load program.zip in it
with ZipFile('program.zip', 'r') as zipObj:
   # Extract all the contents of zip file in different directory
   zipObj.extractall('temp')

It will extract all the files in ‘program.zip’ in the temp folder.

Extract few files from a large zip file based on condition

Unzip files python: If we have a very large zip file and we need a few files from thousand of files in the archive. Unzipping all files from large zip can take minutes. But if we want only a few of the archived files, then instead of unzipping the whole file we can extract a few files too from the zip file.

For this, we are going to use the below syntax-

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

It accepts three arguments-

1. path: It shows the location where we want to extract our file

2. members:  Full name of the file to be extracted. It should one from the list of archived files names returned by ZipFile.namelist()

3. pwd: If the zip file is encrypted then pass the password in this argument default it will be None

# app.py 
from zipfile import ZipFile
# Create a ZipFile Object and load program.zip in it
with ZipFile('programDir.zip', 'r') as zipObj:
   # Get a list of all archived file names from the zip
   listOfFileNames = zipObj.namelist()
   # Iterate over the file names
   for fileName in listOfFileNames:
       # Check filename endswith csv
       if fileName.endswith('.csv'):
           # Extract a single file from zip
           zipObj.extract(fileName, 'temp_csv')

It will extract only csv files from the given zip archive.

Conclusion:

So in this article, you have seen how to unzip a file | Extract Single, multiple, or all files from a ZIP archive.

Thank you!