Skip to Content
Vasu D.

3 mins read


How to Create a zip file in Python (with example)

Learn how to work with zip files in Python using shutil and zipfile libraries. Easiest examples for creating one liner zip archives in Python.


Introduction to Zip File

A zip file is a compressed archive of collection of files or folders. In this article we will try creating a zip file with Python. For examples, we will be creating an archive of folders, and files using Python.

Note: This tutorial uses Python3.6+

How to create a Zip archive/file in Python ?

There are multiple ways to create an archive with Python. There are built-in python libraries shutil and zipfile. Both of these libraries provide you functions for creating, reading zipfiles.

In this tutorial, we will be demonstrate both of these libraries with an example. Let's go!

Create ZIP archive with zipfile

zipfile is a built-in python library, that provides you with different tools to CRUD and list a zip file. For the complete API-reference you may visit official documentation.

Let's archive a directory/folder with zipfile. Copy-paste the below given code to archiver.py file.

from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
 
from os import PathLike
from typing import Union
 
def zip_dir(zipfile_name: str, source_dir: Union[str, PathLike]):
    src_path = Path(source_dir).expanduser().resolve(strict=True)
 
    with ZipFile(zip_name, 'w', ZIP_DEFLATED) as zf:
        for file in src_path.rglob('*'):
            # Here, we iterate recursively through the entire dir tree
            # and write each file/folder individually to the zip
            zf.write(file, file.relative_to(src_path.parent))
 
zip_dir("awesome_archive.zip", "/path/to/archive")

Run the python file, with command :

python archiver.py

This will create a zip file named awesome_archive.zip of /path/to/archive in your current working directory.

The issue with creating a zip file with zipfile module is, that you have to manually iterate through the entire directory and its subdirectories. Which is an unnecessary overhead, if you wants to ship things real quick.

Create ZIP archive with Shutil

Another way of creating zip files with Python is using shutil library. Shutil is a built-in python library, and has different functions for file operations. For complete API reference, visit official documentation for shutil.

Unlike zipping a folder with zipfile, with shutil you don't have to manually iterate through the dir-tree. Shutil abstracts that logic out for you.

Let's archive a directory/folder with shutil. Copy-paste the below given code to archive_with_shutil.py file.

import shutil
 
shutil.make_archive("awesome_archive.zip", 'zip', "/path/to/archive")

This will create a zip file named awesome_archive.zip of /path/to/archive in your current working directory.

Best lib for zipping a folder in python ?

We recommend using shutil for creating, reading a zip file because of following reasons :

  • part of built-in python library.
  • You don't have to manually iterate through the subdirectories and files
  • Does the job in one single line.

Why zipfile may not be a good option ?

  • you have to manually write the logic of iterating through files and subdirectories

, and that's it. Now you know how to create a zip archive of a folder with Python. Happy coding !


Looking for a Python Expert? Let's Collaborate!
Get in Touch