Working with zip files in Python


Python provides a built-in module called zipfile for working with zip files. This module allows us to create, read, write, and extract files from zip archives.

Here are some methods to work with zip files in Python:

Creating a zip file

To create a new zip file, we can use the ZipFile class from the zipfile module. We can pass the name of the zip file we want to create as the first argument, and the mode as the second argument. The mode can be 'w' for writing, 'x' for exclusive creation, 'a' for appending, or 'r' for reading.

import zipfile

with zipfile.ZipFile('my_archive.zip', 'w') as zip:
    zip.write('file1.txt')
    zip.write('file2.txt')

This will create a new zip file called my_archive.zip and add two files file1.txt and file2.txt to it.

Extracting files from a zip file

To extract files from a zip file, we can use the extractall() method of the ZipFile class. We can pass the path where we want to extract the files as the first argument.

import zipfile

with zipfile.ZipFile('my_archive.zip', 'r') as zip:
    zip.extractall('extracted_files')

This will extract all the files from my_archive.zip to a new directory called extracted_files.

Reading the contents of a zip file

To read the contents of a zip file, we can use the namelist() method of the ZipFile class. This method returns a list of all the files and directories in the zip file.

import zipfile

with zipfile.ZipFile('my_archive.zip', 'r') as zip:
    print(zip.namelist())

This will print a list of all the files and directories in my_archive.zip.

Adding files to an existing zip file

To add files to an existing zip file, we can use the write() method of the ZipFile class. We can pass the name of the file we want to add as the first argument, and the name we want to give it in the zip file as the second argument.

import zipfile

with zipfile.ZipFile('my_archive.zip', 'a') as zip:
    zip.write('file3.txt', 'new_file.txt')

This will add a new file file3.txt to my_archive.zip with the name new_file.txt.

Deleting files from a zip file

To delete a file from a zip file, we need to create a new zip file and copy all the files except the one we want to delete to the new zip file. We can use the write() method to copy the files.

import zipfile
import os

with zipfile.ZipFile('my_archive.zip', 'r') as zip:
    with zipfile.ZipFile('new_archive.zip', 'w') as new_zip:
        for name in zip.namelist():
            if name != 'file2.txt':
                data = zip.read(name)
                new_zip.writestr(name, data)

os.remove('my_archive.zip')
os.rename('new_archive.zip', 'my_archive.zip')

This will delete the file file2.txt from my_archive.zip. We first read all the files from my_archive.zip and copy them to a new zip file new_archive.zip, except for file2.txt. We then delete the old zip file and rename the new zip file to the old name.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.