SoFunction
Updated on 2024-10-29

python implement batch compression of folders in a specified directory

Language: python 3

Usage: Select a directory, compress the folders under the directory separately, generate a zip file with the same name, and save it to the directory.

import os
import shutil
import zipfile
from tkinter import Tk
from tkinter import filedialog
root = Tk()
()
directory = (title="Select Catalog")
def zip_folders(directory):
    for root, dirs, files in (directory):
        for dir_name in dirs:
            folder_path = (root, dir_name)
            zip_path = f"{folder_path}"
            shutil.make_archive(zip_path, 'zip', folder_path)
            print(f"compressed folder: {zip_path}")
zip_folders(directory)
zip_folders(directory)

This article on python to achieve batch compression of specified directory folder is introduced to this article, more related python batch compression of specified folder content, please search for my previous articles or continue to browse the following related articles I hope you will have more support for me in the future!