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.
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:
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:
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:
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:
Working with Google Drive files
Many Colab users keep ZIP archives in Google Drive. In that case, mount Drive first:
Then unzip from the Drive path:
Or with Python:
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:
Or handle that logic in Python:
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_folderfor the simplest Colab workflow. - Use Python's
zipfilemodule when you need programmatic control. - Mount Google Drive first if the ZIP file lives in Drive.
- Extract to
/contentfor temporary local work or to Drive for persistence. - Clean or replace the destination folder when you need repeatable notebook runs.
Related reading
- How to unzip a list of tuples into individual lists?
- How to update a plot in matplotlib
- How to update an existing Conda environment with a .yml file
- How to update metadata of an existing object in AWS S3 using python boto3?
- How to update Python?
- How to update SQLAlchemy row entry?
- How to update values using pymongo?
- How to update/upgrade a package using pip?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.