Google Colab
unzip folder
file extraction
Python tutorial
data manipulation

How to unzip a folder in google colab?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Google Colab, you do not unzip a "folder" directly. You unzip a ZIP archive into a destination folder. The two most common approaches are the shell unzip command and Python's built-in zipfile module.

Use the Colab shell command for the fastest path

If you already have a ZIP file in the Colab filesystem, the quickest solution is:

bash
!unzip /content/data.zip -d /content/data

This extracts the archive data.zip into the folder /content/data. If the destination folder does not exist yet, unzip creates it as needed.

To see what was extracted:

bash
!ls /content/data

This approach is simple and usually the best choice when you are working interactively in a notebook.

Use Python's zipfile module when you want more control

If you want extraction logic inside Python code, use zipfile:

python
1import zipfile
2
3zip_path = "/content/data.zip"
4extract_to = "/content/data"
5
6with zipfile.ZipFile(zip_path, "r") as zip_ref:
7    zip_ref.extractall(extract_to)
8
9print("Extraction complete")

This is useful when your notebook needs to:

  • inspect archive contents first
  • conditionally extract files
  • integrate extraction into a larger data-preparation function

You can also list the files before extracting:

python
1import zipfile
2
3with zipfile.ZipFile("/content/data.zip", "r") as zip_ref:
4    print(zip_ref.namelist())

Working with Google Drive files

Many Colab users keep ZIP archives in Google Drive. In that case, mount Drive first:

python
from google.colab import drive
drive.mount("/content/drive")

Then unzip from the Drive path:

bash
!unzip "/content/drive/MyDrive/datasets/data.zip" -d /content/data

Or with Python:

python
1import zipfile
2
3zip_path = "/content/drive/MyDrive/datasets/data.zip"
4extract_to = "/content/data"
5
6with zipfile.ZipFile(zip_path, "r") as zip_ref:
7    zip_ref.extractall(extract_to)

This is a common workflow when the dataset is too large to upload manually into each notebook session.

Check storage and extraction location

Colab sessions have temporary local storage under /content. That storage disappears when the runtime resets. So decide which location matches your goal:

  • '/content/... for temporary fast local work'
  • '/content/drive/... for persistence in Google Drive'

If you extract a large dataset into /content, it will be available only for the life of the current runtime session. That is often fine for training runs, but not fine if you expect the files to remain there next week.

Handle overwrite behavior carefully

If you re-run the notebook cell, files may already exist in the destination directory. In that case, you may want to remove the old folder first:

bash
!rm -rf /content/data
!unzip /content/data.zip -d /content/data

Or handle that logic in Python:

python
1import os
2import shutil
3import zipfile
4
5extract_to = "/content/data"
6
7if os.path.exists(extract_to):
8    shutil.rmtree(extract_to)
9
10with zipfile.ZipFile("/content/data.zip", "r") as zip_ref:
11    zip_ref.extractall(extract_to)

That keeps repeated notebook runs deterministic.

Common Pitfalls

The biggest mistake is confusing the ZIP file path with the output folder path. You unzip the archive file into a folder; the folder itself is not what gets "unzipped."

Another common issue is extracting to /content and then losing the files when the Colab runtime resets. Use Google Drive if the files must persist.

People also forget to mount Drive before referencing Drive paths, which makes file-not-found errors look more mysterious than they are.

Finally, large archives can exceed the available Colab disk space. Check your extraction target and storage limits before starting.

Summary

  • Use !unzip archive.zip -d output_folder for the simplest Colab workflow.
  • Use Python's zipfile module when you need programmatic control.
  • Mount Google Drive first if the ZIP file lives in Drive.
  • Extract to /content for temporary local work or to Drive for persistence.
  • Clean or replace the destination folder when you need repeatable notebook runs.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.