Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Creating ZIP Archives in Python: From Full Directory to Custom File Selection

Python makes it straightforward to create ZIP and TAR archives, whether you need to compress an entire directory or select specific files.

Use shutil.make_archive for a quick, directory‑wide zip:

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

For granular control, the zipfile.ZipFile class lets you specify individual files:

ZipFile.write(filename)

Step 1: Prepare the environment

Step 2: Zip the entire directory

When you want to archive everything in a folder, use make_archive:

root_dir, _ = path.split(src)
make_archive("guru99_archive", "zip", root_dir)

This creates guru99_archive.zip in the current working directory.

Step 3: Add specific files to a new ZIP

If you need finer control, the ZipFile class is ideal:

with ZipFile("testguru99.zip", "w") as newzip:
    newzip.write("guru99.txt")
    newzip.write("guru99.txt.bak")

The with statement automatically closes the archive when the block ends.

Step 4: Verify the result

Open the generated ZIP file in your operating system’s file explorer to see the contents. On Windows, a double‑click will display a list of the included files.

Below are illustrative screenshots from a typical workflow:

Creating ZIP Archives in Python: From Full Directory to Custom File Selection

Creating ZIP Archives in Python: From Full Directory to Custom File Selection

Creating ZIP Archives in Python: From Full Directory to Custom File Selection

Creating ZIP Archives in Python: From Full Directory to Custom File Selection

Creating ZIP Archives in Python: From Full Directory to Custom File Selection

Creating ZIP Archives in Python: From Full Directory to Custom File Selection

Creating ZIP Archives in Python: From Full Directory to Custom File Selection

Complete code (Python 3):

import os
from shutil import make_archive
from zipfile import ZipFile
from os import path


def main():
    if path.exists("guru99.txt"):
        # Get absolute path to the source file
        src = path.realpath("guru99.txt")
        # Optional: rename an existing file
        os.rename("career.guru99.txt", "guru99.txt")

        # Archive the entire directory containing the file
        root_dir, _ = path.split(src)
        make_archive("guru99_archive", "zip", root_dir)

        # Create a second ZIP with only selected files
        with ZipFile("testguru99.zip", "w") as newzip:
            newzip.write("guru99.txt")
            newzip.write("guru99.txt.bak")


if __name__ == "__main__":
    main()

Summary

Python

  1. Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide
  2. C# Serialization & Deserialization: A Practical Example
  3. Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
  4. Python Counter in collections – Efficient Counting, Updating, and Arithmetic Operations
  5. Mastering Python’s readline() – Efficient Line‑by‑Line File Reading
  6. Python List index() – How to Find Element Positions with Practical Examples
  7. Master Python Regular Expressions: re.match(), re.search(), re.findall() – Practical Examples
  8. Python Calendar Module: Expert Guide with Code Examples
  9. Master Python Multithreading: GIL Explained with Practical Examples
  10. Master Python Attrs: Build Advanced Data Classes with Practical Examples