How to Unzip File in Python?


In this blog post, I will be teaching you how to unzip a file in python. Python is a versatile scripting language that allows you to automate many tasks, including file compression and decompression. The following steps will show you how to use the built-in zipfile module to create and extract zip files.

Step 1: Import the zipfile module

In order to work with Zip files in python, we will need to import the built-in zipfile module. We can do this by including the following line of code at the beginning of our script:

import zipfile
Code language: JavaScript (javascript)

Step 2: Create a ZipFile object

Once the module is imported, we can create a ZipFile object. This object will allow us to perform various operations on the ZIP file.

We can create a ZipFile object by passing in the path to the ZIP file as an argument to the zipfile.ZipFile constructor.

For example, if we wanted to create a ZipFile object for a file named “example.zip” that is located in our current directory, we would use the following code:

zf = zipfile.ZipFile('example.zip')
Code language: JavaScript (javascript)

You can also pass in an optional mode argument that specifies what mode to open the file in. By default, files are opened in read-only mode, but you can also open them in write mode or append mode.

zf = zipfile.ZipFile('example.zip', 'w') #write mode zf = zipfile.ZipFile('example.zip', 'a') #append mode
Code language: PHP (php)

If you’re working with large Zip files, you may want to specify a timeout value (in seconds) so that your program doesn’t get stuck waiting for data that may never come. This can be done by passing in an additional timeout keyword argument:

zf = zipfile.ZipFile('large_zip_file.zip', 'r', timeout=5) #timeout after 5 seconds
Code language: PHP (php)

Step 3: Extract Files from the ZIP File

Once you have a ZipFile object, you can extract individual files from the ZIP file by calling the extract() method and specifying the name of the file you want to extract:

zf = zipfile.ZipFile('example.zip') zf.extract('filename') #extracts a single file from the ZIP file
Code language: PHP (php)

You can also extract all of the files from the ZIP file at once by calling the extractall() method with no arguments:

zf = zipfile.ZipFile('example.zip') zf = zipfile().extractall() #extracts all files from ZIP file
Code language: PHP (php)

If you want to extract all of the files from the ZIP file into a specific directory, you can pass in that directory’s path as an argument to extractall():

zf = zipfile().extractall(path)

Step 4: Close the ZIP File

Once you’re finished working with a ZIP file, it’s good practice to close it so that you don’t accidentally modify any of its contents:

zf = zipfile().close()

Full Python Script

import zipfile target = input(r"Enter file to be unzipped: ") handle = zipfile.ZipFile(target) handle.extractall("./Unzip file/Unzip files") handle.close()
Code language: JavaScript (javascript)

Run the command: python script.py

That’s all there is to working with Zip files in python! In this blog post, we learned how to import the built-in zipfile module and use it to create and extract Zipfiles .

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts