How to Unzip File in Python

Here are three ways to unzip files in Python:

  1. Using extractall() Method
  2. Using extract() Method
  3. Using shutil.unpack_archive() Method

Method 1: Using extractall() Method

The most straightforward method is to use the extractall(), which extracts all the contents of the zip file.

If you don’t specify a path, the files will be extracted to the current working directory.

Example

from zipfile import ZipFile

with ZipFile('dir/data.zip', 'r') as zipObj:
  # Extract all the contents of zip file in different directory
  zipObj.extractall('temp')
  print('File is unzipped in temp folder') 
# importing shutil module
import shutil

# Path of the file
filename = "dir/data.zip"

# Target directory
extract_dir = "temp"

# Unzip the file
shutil.unpack_archive(filename, extract_dir)

Output

Using shutil Module

See Also

2 thoughts on “How to Unzip File in Python”

  1. 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
    • The file extensions you’ve mentioned (.el, .en, .es, .kl, .no, .ta, .ko, and .zh-CN) don’t correspond to standard compression formats like .zip, .rar, or .tar.gz. Instead, these extensions seem to represent language codes (for example, .es for Spanish, .ko for Korean, .zh-CN for Simplified Chinese, etc.).

      If these files are indeed not compressed archives, you won’t be able to “decompress” them in the traditional sense.

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.