
๐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.
๐1. How to Create a ZIP File Using Python
Python ZIP File Creation with Examples: Python’s shutil and zipfile modules provide flexible methods to create ZIP files.
๐Method 1: Zip Entire Directory Using shutil
To zip an entire directory, use the shutil.make_archive() method.
๐Python ZIP File Creation with Examples Syntax:
python
shutil.make_archive(output_filename, ‘zip’, dir_name)
๐Python ZIP File Creation with Examples Parameters:
- output_filename: The name for your resulting ZIP file.
- ‘zip’: Specifies the ZIP format.
- dir_name: The directory you want to archive.
๐Example Code:
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.
๐2. How to Zip Specific Files Using zipfile
To zip selected files instead of the entire directory, use Pythonโs zipfile module.
๐Example Code for Zipping Specific Files:
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.
๐3. Detailed Explanation
โ
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.
๐4. Python 2 vs Python 3 Code for Creating ZIP Files
Python 2 Example Code:
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 3 Example Code:
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”)
๐5. Summary
โ
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.