---Advertisement---

Python ZIP File Creation with Examples | Easy Steps to Guide Archive Files 2025

By Bhavani

Updated On:

---Advertisement---
Python ZIP File Creation with Examples

Python ZIP File Creation with Examples: Python provides simple and efficient ways to create ZIP archives using the shutil and zipfile modules. This guide explains how to zip entire directories and selected files in Python with detailed code examples.


Python ZIP File Creation with Examples: Python’s shutil and zipfile modules provide flexible methods to create ZIP files.

To zip an entire directory, use the shutil.make_archive() method.

python

shutil.make_archive(output_filename, ‘zip’, dir_name)

  • output_filename: The name for your resulting ZIP file.
  • ‘zip’: Specifies the ZIP format.
  • dir_name: The directory you want to archive.

python

import os

import shutil

from os import path

def main():

    # Check if the file exists

    if path.exists(“Software Moji Moji.txt”):

        # Get the file path

        src = path.realpath(“Software Moji Moji.txt”)

        # Rename the original file

        os.rename(“career.Software Moji Moji.txt”, “Software Moji Moji.txt”)

        # Zip the entire directory

        root_dir, tail = path.split(src)

        shutil.make_archive(“Software Moji Moji archive”, “zip”, root_dir)

if __name__ == “__main__”:

    main()

Output:
โœ… A new ZIP file named “Software Moji Moji archive.zip” is created in the specified directory.


To zip selected files instead of the entire directory, use Pythonโ€™s zipfile module.

python

import os

from zipfile import ZipFile

# Creating a ZIP file with specific files

with ZipFile(“testSoftware Moji Moji.zip”, “w”) as newzip:

    newzip.write(“Software Moji Moji.txt”)

    newzip.write(“Software Moji Moji.txt.bak”)

Output:
โœ… A ZIP file named “testSoftware Moji Moji.zip” containing only the specified files is created.


โœ… shutil.make_archive() is ideal for zipping entire directories.
โœ… ZipFile.write() offers better control for selecting specific files to include in the archive.
โœ… Using with statements automatically handles file closure, ensuring no manual .close() command is needed.


python

import os

import shutil

from zipfile import ZipFile

from os import path

from shutil import make_archive

def main():

    if path.exists(“Software Moji Moji.txt”):

        src = path.realpath(“Software Moji Moji.txt”)

        os.rename(“career.Software Moji Moji.txt”, “Software Moji Moji.txt”)

        root_dir, tail = path.split(src)

        shutil.make_archive(“Software Moji Moji archive”, “zip”, root_dir)

        with ZipFile(“testSoftware Moji Moji.zip”, “w”) as newzip:

            newzip.write(“Software Moji Moji.txt”)

            newzip.write(“Software Moji Moji.txt.bak”)

if __name__ == “__main__”:

    main()


python

import os

import shutil

from zipfile import ZipFile

from os import path

from shutil import make_archive

if path.exists(“Software Moji Moji.txt”):

    src = path.realpath(“Software Moji Moji.txt”)

    os.rename(“career.Software Moji Moji.txt”, “Software Moji Moji.txt”)

    root_dir, tail = path.split(src)

    shutil.make_archive(“Software Moji Moji archive”, “zip”, root_dir)

    with ZipFile(“testSoftware Moji Moji.zip”, “w”) as newzip:

        newzip.write(“Software Moji Moji.txt”)

        newzip.write(“Software Moji Moji.txt.bak”)


โœ… Use shutil.make_archive() to zip entire directories.
โœ… Use ZipFile.write() for precise control over which files to include.
โœ… Using the with statement ensures proper cleanup and file closure.

Python Exception Handling

Download Python

Leave a Comment

Index