how to make zip file in python code example

Example 1: python zip folder

import os
import zipfile

def zip_directory(folder_path, zip_path):
    with zipfile.ZipFile(zip_path, mode='w') as zipf:
        len_dir_path = len(folder_path)
        for root, _, files in os.walk(folder_path):
            for file in files:
                file_path = os.path.join(root, file)
                zipf.write(file_path, file_path[len_dir_path:])
                
zip_directory('C:/FolderToZip', 'C:/Folder.zip')

Example 2: zip a directory in python

shutil.make_archive(output_filename, 'zip', dir_name)