Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Unzip a File in Python

  • 17 Sep, 2025
  • Com 2
How to Unzip a File in Python

To unzip a file in Python, use zipfile.ZipFile to open the archive and extractall() to unzip everything to the current directory or a specified path.

Here is the archive.zip file in the temp directory:

Zip file in a directory

Let’s unzip this file:

import zipfile

with zipfile.ZipFile("temp/Archive.zip", "r") as zip_ref:
    zip_ref.extractall("temp")

print("Extracting all files into a folder")

# Output: Extracting all files into a folder

Unzipping a file in Python

In the above code, we imported the built-in zipfile module and used its ZipFile() method with a context manager to open the Archive.zip file as an object.

Then, we called the .extractall() function on the file object “zip_ref” to unzip the files within it.

The .extractall() method is used for bulk extraction.

The Archive.zip file contains three files:

  1. app.json
  2. empty.txt
  3. output.csv

As you can see from the screenshot above, all the files are extracted into the “temp” folder.

If the ZIP contains a top-level folder, it will create that folder; otherwise, files will be extracted flat, like our example.

Extracting a single file

If you want to just extract a single file instead of a bulk extract, use the zipfile.ZipFile to open in read mode that returns a file object, and then apply the .extract() method on that object.

Here is the temp folder before extracting a single file:

temp folder before a single file extraction

Let’s extract the single file called “app.json” from it. You can extract whatever file you like.

import zipfile

with zipfile.ZipFile("temp/Archive.zip", "r") as zip_ref:
    zip_ref.extract("app.json", "temp")

print("The app.json file has been extracted")

# Output: The "app.json" file has been extracted

Extracting a single file in Python

In this code, the extract() function accepts the specific file you want to extract from the zip and the destination directory path. We passed both arguments (app.json and temp folder), and the screenshot above is the output.

Reading file content without extracting

You don’t need to extract a file to read the content. You can read the zip file object as a normal file using the with open() context manager.

import zipfile

with zipfile.ZipFile("temp/Archive.zip", "r") as zip_ref:
    with zip_ref.open("output.csv") as f:
        content = f.read().decode("utf-8")
        print(content)

# Output:
# name,age,city
# Marsh,30,New York
# Chris,28,San Francisco
# Mike,26,Los Angeles
# Robert,23,Orlando
# Andre,29,Illinois

Alternate approaches

Using shutil.unpack_archive

The shutil is a built-in Python module that provides the unpack_archive() method, which can be used to unzip an archive file. It supports multiple archive formats (.zip, .tar, .gztar, etc.).

Here is the temp folder screenshot before unpack:

The current temp folder

import shutil

shutil.unpack_archive("temp/Archive.zip", "temp")

print("The files has been unzipped")

# Output: The files has been unzipped

Here is the output screenshot:

Unzipping using shutil.unpack_archive() method

This approach is straightforward, but it is less efficient compared to zipfile.

That’s all!

Post Views: 10
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

JavaScript String slice(): Extracting a Portion of a String
How to Check a Data Type of a Variable in Python

2 Comments

  1. Fradrick Munyurwa

    July 20, 2020 at 3:08 am

    I don’t have .zip files, but ones ending with ,el; ,en; ,es; ,kl; ,no; ,ta; ,ko; and ,zh-CN; all downloaded after running PIP on a Coursera course. How do I decompress these ones?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend