Unzip a file in Python: 5 scenarios you should know (2023)

when you are thereHEworld for a while you should know about itunzip or extractfrom a file. More than 70% of the files available on the Internet are in the form of a lossless compressed file (eg zip, jpeg, tar, rar). There are several applications on the market to unzip a file. One of the most popular programs to decompress or extract iswinrar. But in today's article we will learn how to do it.unzip a file in python

In this particular tutorial, we will learn how to deal with 5 different scenarios when we want to extract or unzip a file in Python. For anyone who doesn't know what extraction is, let me explain briefly. Unzip is a term used to describe the process of decompressing and moving one or more files in an archive to another location.

Let's look at some conditions under which we can extract a file using Python.


5 situations in which you can extract a file with Python

  • Extract only one file
  • Extract all/multiple files from a zip file to the current directory
  • Extract all files to another directory
  • Unzip only some specific files based on different conditions
  • Extract the password protected zip file withremove all()

Time to code!

Module to unzip files in Python

To extract a file using Python, we use thezip archiveModulein Python. The zipfile module is used to access functions that would help uscreate, read, write, extract and list a ZIP filea Python.


ZipFile.extractall(path=None, members=None, pwd=None)


  • Far:This path parameter stores a path to the directory where the ZIP files should be unpacked. If not specified, the file is extracted to the current working directory.
  • Members:This parameter is used to add the list of files to extract. If no arguments are given, all files are extracted.
  • Password:This parameter is used to extract a file encrypted with a password.

1. Extract just one file

Sometimes we just need a specific ZIP file to accomplish our task. In such cases, extracting all ZIP files takes time and also consumes your computer's memory. To avoid this, there is an option to extract the specific file/folder from the ZIP file and save it to your computer. The following code demonstrates this:

(Video) How to Unzip Files using Python

Code -

Currently, our ZIP archive contains three files: "a.txt", "p.txt", and "pool.txt".

import zipfilezip_file = "a.zip"file_to_extract = "a.txt"try: com zipfile.ZipFile(zip_file) como z: com open(file_to_extract, 'wb') como f: f.write(z.read(file_to_extract)) print("Extraído", file_to_extract)exceto: print("Arquivo invalido")

Salida -

extrae a.txt

Explanation -

First we import the zipfile module in the first line. After that, we need to declare some constant variables that can be used later in the code. In this particular example, we are extracting "a.txt" from the "a.zip" zip file. With the ZipFile.read() function, you can store the binary value of the file in a variable and copy this variable to the local file to extract it.

2. Extract all/multiple files from a zip file to the current directory in Python

Extracting all files with Python is one of the best features of Python. If you have Python installed on your computer, you don't even need software like Winrar to extract zip files. Also, it can extract not only zip files, but also '.tar.gz', '.apk', '.rar' and other formats. The most interesting thing is that you don't have to declare any type of zipper. The module automatically detects if it is zip, rar or other formats.

Code -

import zipfilezip_file = "a.zip"try: with zipfile.ZipFile(zip_file) as z: z.extractall() print("Extraído tudo")exceto: print("Arquivo inválido")
(Video) Python Tutorial: Zip Files - Creating and Extracting Zip Archives

Salida -

all extracted

Explanation -

By default, we import the zipfile module in the first archive. We then use the context manager to use the ZipFile class. With this method, you don't have to close the ZIP file after opening it. You can use the extractall() method to extract all the files in the current directory of your Python code.

3. Extract all files to another directory in Python

It is very important to learn file management when coding. You should create a better storage space for your files to avoid problems with the same folders. You can use the extractall method with the parameter to extract the zip file to any folder. If the directory does not exist, the module automatically creates a new, empty directory.

Code -

import zipfilezip_file = "a.zip"try: with zipfile.ZipFile(zip_file) as z: z.extractall("temp") print("Extraído tudo")exceto: print("Arquivo inválido")

Salida -

all extracted

Explanation -

We import the zipfile module and open the zip file using the ZipFile class. Using extractall(path) you can extract the entire contents of the zip file to the specifically named path. If the directory already exists, all files will be replaced. If the directory is missing, a new empty folder will be created.

(Video) Decompress and extract .bgz files: Hands-on example from start-to-finish

4. Extract only a few specific files based on different conditions in Python

It is very convenient to extract specific file types from the zip file. Suppose you have a ZIP file that contains images, videos, and other types of files, and you only need to extract images. Then conditional extraction will help you. You can use the namelist() method with conditions to extract them.

Code -

Currently, our ZIP archive contains three files: "a.txt", "p.txt", "pool.txt", "a.jpeg", "b.jpeg", and "a.mp4".

import zipfilezip_file = "a.zip"endswith = ".jpeg"try: with zipfile.ZipFile(zip_file) as z: for file in z.namelist(): if file.endswith(termina con): z.extract(file) print ("Tudo extraído", termina com) excepto: print("Arquivo inválido")

Salida -

All extracted .jpeg files

Explanation -

We'll start by importing the Zipfile module. So we declare some constants and others the zip file as context manager. All of the file names in the ZIP archive can be determined using the namelist() method. This method returns the list of all file objects. Next, we check if the file ends up with a specific file format (in our case, we used the ".jpeg" format).

If the file ends with a specific extension, we can extract the file. All files are extracted to the same folder. If you want to extract them to a specific folder, use the path parameter in the extractall() method.

5. Extracting password-protected zip files using extractall() in Python

One of the features of zip files is that you can assign a password to them. Furthermore, you can also encrypt the file names to make it more difficult. In this section, we will only discuss the basic zip file password manager and how to use it.

(Video) You should put this in all your Python scripts | if __name__ == '__main__': ...

Note: Each software uses a different encryption technique and you may not be able to extract them with Python. Once you know that your zip file is encrypted correctly, use the pyzipper module to create a proper zip object.

Code -

import zipfilezip_file = "a.zip"try: with zipfile.ZipFile(zip_file) as z: z.setpassword(bytes("pythonpool","utf-8")) z.extractall() print("Extraído todo")außer Ausnahme wie e: print("Ungültige Datei", e)

Salida -

all extracted

Explanation -

We import the zipfile module and open the zip file with the context manager. You can use the setpassword method to initialize a password for your ZIP file. The only requirement is that it accept the bytes object as input. We use the bytes() function to convert our string to bytes. Finally, you can use the extractall() method to extract all the files from the ZIP archive.

If the zip file is compressed by software like WinRar, you should use my zip module which has advanced methods to determine the algorithms.

Must read

  • Gaussian Elimination in Python: Illustration and Implementation
  • Bitonic classification: algorithm and implementation in Python
  • An introduction to Python for Android development

last words

Python has proven to be one of the best programming languages ​​for computers with the support of thousands of modules. With easy-to-write syntax and APIs for every module in the system, you can build beautiful applications to make your tasks easier.python unzip fileis one of the features that allows you to automate your extraction tasks.

Please share this post with your friends and let them know about this awesome module!

(Video) Pyspark Scenarios 5 : how read all files from nested folder in pySpark dataframe #pyspark #spark


How to unzip a file in Python? ›

Import the zipfile module Create a zip file object using ZipFile class. Call the extract() method on the zip file object and pass the name of the file to be extracted and the path where the file needed to be extracted and Extracting the specific file present in the zip.

What are the steps to unzip a file? ›

To unzip files
  1. Open File Explorer and find the zipped folder.
  2. To unzip the entire folder, right-click to select Extract All, and then follow the instructions.
  3. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.

How do I open a zip 5 file? ›

Unzip your files
  1. On your Android device, open Files by Google .
  2. On the bottom, tap Browse .
  3. Navigate to the folder that contains a . zip file you want to unzip.
  4. Select the . zip file.
  5. A pop up appears showing the content of that file.
  6. Tap Extract.
  7. You're shown a preview of the extracted files. ...
  8. Tap Done.

How do I unzip a zipped list in Python? ›

UnZipping in Python
  1. listA = [ 1 , 2 , 3 , 4 ] listB = [ 'a' , 'b' , 'c' , 'd' ]
  2. #zip listA and listB and put it in one list zl.
  3. zl = zip (listA, listB)
  4. zl = list (zl)
  5. print (zl)
  6. #unzip zl and put the values back to listA and listB.
  7. listA, listB = zip ( * zl)
  8. print (listA) print (listB)
Aug 13, 2019

What does zip (*) do in Python? ›

Python's zip() function is defined as zip(*iterables) . The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.

What does the zip () function do in Python? ›

Definition and Usage. The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

What is the use of unzip? ›

Unzipping is the act of extracting the files from a zipped single file or similar file archive. If the files in the package were also compressed -- as they usually are -- unzipping decompresses them.

What happens when you unzip a file? ›

Extract/Unzip Zipped Files

When you extract files from a zipped folder, a new folder with the same name is created which contains the files. The compressed (zipped) version also remains.

What is in unzip command? ›

It allows you to combine multiple files and directories into a single archive file. The Unzip command is used to decompress or extract the content from the compressed archive.

How can I open a zip file that Cannot be opened? ›

Navigate to the folder location where the zip file was saved. Right-click on the zip file and choose Open with... Select Windows Explorer. If Windows Explorer is not an option, select Choose default program... and select Windows Explorer, then click OK.

How to open a zip folder? ›

How to Open a ZIP File on Android Devices
  1. Open the Files app. ...
  2. Then click Browse at the bottom of your screen.
  3. Locate the ZIP file you want to extract. ...
  4. Tap the file you want to open and then tap Extract. ...
  5. Finally, tap Done.

Why can't I open my zip file? ›

Zip files may refuse to open if they are not properly downloaded. Also, incomplete downloads occur when files get stuck due to issues like bad internet connection, inconsistency in network connection, all of which can cause transfer errors, affect your Zip files, and make them unable to open.

What is zip and unzip in Python? ›

Zip. Zip is a useful function that allows you to combine two lists easily. After calling zip, an iterator is returned. In order to see the content wrapped inside, we need to first convert it to a list.

How do I unzip a list of lists? ›

To convert a list of lists to two lists:
  1. Use the zip() function with the * operator to unzip the list.
  2. Use a list comprehension to iterate over the zip object.
  3. Use the list() class to convert each tuple to a list.

Is there an unzip in Python? ›

Python Zipfile Module

We can use zipfile. extractall() function to unzip the file contents in the same directory as well as in a different directory.

How do you code a ZIP file in python? ›

How to create a zip file using Python?
  1. import shutil import os. ...
  2. import os from zipfile import ZipFile # Create a ZipFile Object with ZipFile('E:/Zipped file.zip', 'w') as zip_object: # Adding files that need to be zipped zip_object.
Nov 23, 2022

What is zip and example? ›

Standard US ZIP codes

The standard ZIP code notation used by the United States Postal service uses five digits to identify a delivery area. An example of a standard US ZIP code is 90210.

What is a ZIP file and why are they used? ›

A zip file is a file format that can contain multiple files combined and compressed into one file. Files that are zipped have a file extension of . zip. Since it's a type of compressed file, a zip file can be smaller in size than the files it contains. This makes the zip file easier and faster to download.

What is the syntax of zip function? ›

The zip function creates an iterator of tuple by taking elements from each iterable passed as argument and aggregating them together: zip(a, b) = [(a0, b0), (a1, b1), (a2, b2), ...] It stops when the shortest iterable is exhausted, and the elements in longer iterables are left out.

How do I unzip a large file in Python? ›

Unzipping a file with Python is straightforward, We can open the archive using the context manager as we did when creating the zip file, then call the ZipFile. extractall() function and specify a path. That's all there is to it.

How do I unzip a gzip file in Python? ›

The file inside the GZ file is an XML file.
It is very simple.. Here you go !!
  1. read input file using rb mode;
  2. open output file using w mode and utf8 encoding;
  3. gzip. decompress() input bytes;
  4. decode what you get to str .
  5. write str to output file.
Jun 24, 2015

How do I unzip a tar gz file in Python? ›

  1. Import module.
  2. Open .tar.gz file.
  3. Extract file in a specific folder.
  4. Close file.
Dec 17, 2020

How do you code a zip file in python? ›

How to create a zip file using Python?
  1. import shutil import os. ...
  2. import os from zipfile import ZipFile # Create a ZipFile Object with ZipFile('E:/Zipped file.zip', 'w') as zip_object: # Adding files that need to be zipped zip_object.
Nov 23, 2022


1. How I Extract ANY Sequences from a FASTA file using Python | Bioinformatics
(Bioinformatics Coach)
2. How to Execute Python Scripts in UiPath?| UiPath Python Activities| Python Scope| Load Python Script
(AutoBot by Rahul)
3. Read A Specific Line From A File | Python Example
(Portfolio Courses)
4. Python for Beginners with Examples - 018 Real world example Extracting averages from data files
5. Conditional Search Multiple Excel Files - Excel Python Automation - Five Minute Python Scripts
(Derrick Sherrill)
6. Data Extraction Using Python | Python Requests, BeautifulSoup, PyPDF2 | Python Training | Edureka
Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated: 11/09/2022

Views: 6021

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.