Coding Triumph

Helpful code snippets & tutorials

How to unzip a file archive with python

ZIP files (.zip and .zipx) are the most common types of archive formats that support lossless data compression. A single ZIP file can contain one or more compressed files, making it easy to reduce the size of large files and keep related files together. In this post, you’ll learn how to extract single or multiple files from a ZIP archive with Python.

Prerequisite:

zipfile is a built-in Python module for reading and writing ZIP files. It has many useful functions for handling ZIP archives. For our use case, however, we’ll only be using three functions: ZipFile.printdir(), ZipFile.extract(), and ZipFile.extractall()

Functions definitions:

# Print a table of contents for the archive to sys.stdout.
ZipFile.printdir()

# Extract a sinle file from the archive to the current working directory
ZipFile.extract(member, path=None, pwd=None)
# member: full name of the file to extract
# path (optional): the directory to extract to
# pwd: the password used for encrypted files

# Extract all members from the archive to the current working directory
ZipFile.extractall(path=None, members=None, pwd=None)
# path(optional): the directory to extract to
# members(optional): a list of files to be extracted
# pwd: the password used for encrypted files

Extract all files:

The ZipFile.extractall() method extracts all the content of a zip archive into the current directory. Existing files will be overwritten by the new files.

from zipfile import ZipFile
  
""" 
The "animals.zip" contains three files: 
+ cat.txt
+ dog.txt
+ mouse.txt 
"""
file_name = "animals.zip"
  
# opening the zip file in READ mode
with ZipFile(file_name, 'r') as zip:
    # extract all files in the current directory
    zip.extractall()
    print('Done!')

Extract a single file:

With the help of the ZipFile.extract() method, we can easily extract a single file from the archive.

from zipfile import ZipFile
  
# specifying the zip file name
""" 
The "animals.zip" contains three files: 
+ cat.txt
+ dog.txt
+ mouse.txt 
"""
file_name = "animals.zip"
  
# opening the zip file in READ mode
with ZipFile(file_name, 'r') as zip:
    # list the archeve's content
    zip.printdir()
    """ 
    File Name                Modified                    Size
    animals/                 2022-04-08 22:13:44         0
    animals/cat.txt          2022-04-08 22:13:04         2
    animals/mouse.txt        2022-04-08 22:13:04         2
    animals/dog.txt          2022-04-08 22:13:04         2
    """
    # extract single file
    zip.extract('animals/cat.txt')
    print('Done!')

Conclusion:

In this post, we’ve learned how to use ZipFile.extract() and ZipFile.extractall() methods to extract multiple or single files from any ZIP archive with Python. With the help of these methods, you can cover most use cases you may encounter while handling ZIP archives with Python. I hope you find this post useful. If you do, please share!

If you like this post, please share
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments