Unzip any File Using Python

Search for a command to run...

No comments yet. Be the first to comment.
The series content will mostly cover Technical Python tutorials and my recent learnings and experience in Python.
Hello world! Have you heard about Notion? Do you use Notion? Notion is a super-clean and deceptively minimalist note-taking app. I am still learning how to use Notion because I have recently started using it, and If you are a regular reader you must...
All you need to know about Hacktoberfest 2021

Hello reader! Well, Hacktoberfest is something, you are gonna hear a lot about in the coming month. You can refer to my YouTube video Tutorial to see a working tutorial for better understanding and a step-by-step guide of the same. https://www.yo...

Hello reader! In this Blog post, I will share some useful code snippets and functions in Python. Give it a read. Let's get started! 1. Memory The method getsizeof can be used to retrieve the size of any object. Here's an example of the same. import...

Hello reader! Whenever a client sends a request to the server, the response always contains a status code. You might not see it always but it's returned at every client-server interaction. Well, even if you are not a programmer, you would still know...

Hello reader! Microsoft subsidiary GitHub launched Copilot to power pair programming with AI. In this blog post, I will share all you need to know about it. You can refer to my YouTube video Tutorial to see a working tutorial for better understandin...

Hello world!
The ZIP file format is a common archive and compression standard. In this Blog article, we will learn how to Unzip a File. We will see the implementation in Python.
Check out the Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!
You can refer to my YouTube video Tutorial to see a working tutorial for better Understanding and a step by step Guide of the same.
1. What is Zip files?
2. How to Unzip a File Using Python
Let's get started!
The Dictionary Definition:
ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed.
Zip files can come handy for a lot different things, we make use of it on a regular basis.
If you wish to know more about it, you can refer to Zip file Wikipedia Page).
ZipFile module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.
If you wish to know more about it, you can refer to ZipFile Module Documentation.
Now that you are familiar with Zip file use cases and have acquired basic knowledge of ZipFile module, we can move forward to the coding section.
You can find all the code at my GitHub Repository. Drop a star if you find it useful.

In order to access the Python library, we need to import the package in our Python script.
import zipfile as z
We will make use of zipfile module to Unzip the file. Now that we have imported the package in our python script, let's start by defining the target zipped file.
target = 'demo.zip'
demo.zip is the zip file we are targeting to Unzip. Let's display a start message.
print('Starting to Unzip the File!')
Let's make use of ZipFile method from zipfile and pass in our target file. I am storing it in root
root = z.ZipFile(target)
Once done, let's define our destination details. We will make use of extractall method for the same.
If the folder with name new already exists, it will create an unzipped file inside it. Otherwise, a new folder will be created with the given name.
What if NO parameter is passed?
Well, in that case, a new folder will be created with name same as the zipped file name i.e. demo.
#CASE 1:
# Destination folder exist with name 'dest'
root.extractall('C:\\Users\imar\Desktop\python\dest')
#CASE 2:
# Create a new Destination folder at the time of Script execution
root.extractall('new')
#CASE 3:
# No parameter is Passed!
root.extractall()
Once done, let's end the process using close method.
root.close()
As soon you end the terminate the process, File will get Unzipped. Let's display a message for user indicating the Successful termination of the process.
print('\nFile is Succesfully Unzipped!')
With these steps, we have successfully Unzipped the File using python. That's it!
Simple, isn't it? Hope this tutorial has helped. I would strongly recommend you to Check out the YouTube video of the same and don't forget to subscribe my Channel.
You can play around with the zipfile library and even explore more features. You can even make use of Python GUI using Tkinter.
You can find all the code at my GitHub Repository. Drop a star if you find it useful.
Thank you for reading, I would love to connect with you at Twitter | LinkedIn.
Do share your valuable suggestions, I appreciate your honest feedback!
You should definitely check out my other Blogs:
https://en.wikipedia.org/wiki/ZIP_(file_format)
https://docs.python.org/3/library/zipfile.html
See you in my next Blog article, Take care!!