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
- Import the required modules:
from shutil import make_archive from zipfile import ZipFile from os import path import os
- Ensure the source file exists and obtain its absolute path.
if path.exists("guru99.txt"): src = path.realpath("guru99.txt") - Optionally rename an existing file to match the target name.
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:







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
- To zip an entire directory, call
shutil.make_archive('name', 'zip', root_dir). - To add specific files, use
ZipFile.write(filename)inside awithblock.
Python
- Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide
- C# Serialization & Deserialization: A Practical Example
- Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
- Python Counter in collections – Efficient Counting, Updating, and Arithmetic Operations
- Mastering Python’s readline() – Efficient Line‑by‑Line File Reading
- Python List index() – How to Find Element Positions with Practical Examples
- Master Python Regular Expressions: re.match(), re.search(), re.findall() – Practical Examples
- Python Calendar Module: Expert Guide with Code Examples
- Master Python Multithreading: GIL Explained with Practical Examples
- Master Python Attrs: Build Advanced Data Classes with Practical Examples