Python ZIP file with Example

Posted in /  

Python ZIP file with Example

Vinay Khatri
Last updated on June 15, 2024

    Python comes with many standard libraries, and here, in this Python tutorial, we will learn about a Python zip file in detail. This article explains how to compress (zip) and extract (or un-zip) files.

    What is a Zip File?

    The zip file is an archive file format that uses algorithms to compress files without losing the data. When files are compressed to a zip archive, the size of the file reduces, but the data remain the same. Zip provides an ideal way to compress or reduce the size of files and collect them in a single folder or directory.

    Why Use a Zip File?

    • Zip files reduce the size of files. It reduces the storage space occupied by one or many files without losing data.
    • You can bind together many files in a single zip file.
    • With reduced size, it is very easy to send zip files over networks.

    Creating a Zip File in Python Using the zipfile Module

    Python comes with a standard zipfile module, which can compress files into a zip file.


    Let’s write Python code that will create a zip file.

    Here we are in a folder named dev. Now, you can see that we have three files, namely demo.txt, sublime.png and . The file contains the Python script that will create a zip file by compressing demo.txt and sublime.png files.

    Python Program to Create a Zip File

    import zipfile
    print("Creating Zip File.../")
    with zipfile.ZipFile("", "w", compression=zipfile.ZIP_DEFLATED) as create_zip:
        create_zip.write("demo.txt")   #compress the demo.txt file into
        create_zip.write("sublime.png")  #compress the sublime.png file into
    print("Zip file by name has been created")

    Execute the Python Program

    The above code will create a zip file with the name in the same folder or directory where the python script, i.e., , is located. Now when you open the same directory, you will see the zip file created there.

    Behind the Code

    Similar to the Python file handling method, to create a zip file, the zipfile module provides the ZipFile method. In the ZipFile(“”, “w”, compression=zipfile.ZIP_DEFLATED ) method, we specified three parameters , “w” , and compression=zipfile.ZIP_DEFLATED . This means we want to write compressed files into the file. If this file is not available, this statement will create a file. Else, it will write in the existing file. The create_zip.write(“demo.txt”) and create_zip.write(“sublime.png”) statements copy and compress the files into a file.

    Extracting a Zip File in Python Using the zipfile Module

    Now you know how to create a zip file. Next, let’s discuss how you can extract a zip file using the Python zipfile module. As we have used the write mode, “ w ”, to create a zip file, similarly, we can use the read mode, “ r ”, to extract or read files from a zip file.


    In the dev folder, we have a file by name . Let’s write a Python program to extract the file.

    Python Program to Unzip (Extract) a Zip File

    import zipfile
    print("Extracting File.../")
    with zipfile.ZipFile("", "r") as extract_zip:
        #extract all files and save them in folder Extracted files
        extract_zip.extractall("Extracted Files")
    print("Zip file by name has been created")

    Execute the Program

    After executing the file, you will find the Extracted File folder in the development folder where your is located.

    Behind the Code

    Similar to the zip file creating technique, we used the zip file handling technique to extract the zip files. Using the statement zipfile.ZipFile("" , "r") we specified that we want to read the file, and with the extractall("Extracted Files") statement, we extracted the files into Extracted Files folder. The extractall() method extracts all the files present in the zip file. To extract specific files from the zip file, we can use the extract method. For instance, if we only want to extract the demo.txt file from the, we could have used the following code:



    In this Python tutorial, we learned how to create and extract zip files using Python. Python provides a standard library, zipfile , to create and extract zip files. The zipfile module is very useful when you want to create an automated script. In the zipfile module, the ZipFile() method is used to open the zip file for reading and writing. While creating a zip file, we need to specify the compression=zipfile.ZIP_DEFLATED parameter to compress the files.

    Leave a Comment on this Post