
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add Files to a ZIP File Using Python
Managing files and archives is an essential skill. One common archive format is the ZIP file, which simplifies data compression and storage. Python, known for its versatility and power, provides the zip file module, making it easy for developers to interact with ZIP files efficiently.
Key Features of the zipfile Module
Some key features of the zipfile module are as follows-
- Creating ZIP files: You can create new ZIP archives.
- Modifying existing ZIP files: You can add files to existing archives.
- Reading ZIP files: You can extract files from ZIP archives.
Adding a Single File to a ZIP Archive
Let's start with a simple example of how to add a single file to an existing ZIP package. In this instance, we have a file that we wish to add to a ZIP archive that already exists.
Example
In the following example, the 'ZipFile' object is opened in append mode ('a'), allowing us to modify the archive without overwriting it. The 'write()' method adds the specified file to the ZIP archive.
import zipfile def add_single_file_to_zip(zip_file_path, file_to_add): with zipfile.ZipFile(zip_file_path, 'a') as zip_ref: zip_ref.write(file_to_add) zip_file_path = 'existing_archive.zip' file_to_add = 'file_to_include.txt' add_single_file_to_zip(zip_file_path, file_to_add)
Adding Multiple Files to a ZIP Archive
In various situations, we might want to quickly add several files to an already existing ZIP package. A list of files that need to be included can be provided to do this. Let's explore how this is done.
Example
In this example, 'add_multiple_files_to_zip' function appends files to an existing ZIP archive. It opens the archive in append mode ('a') and iterates through a file list, using write() to add each file. The specified files will be included in 'existing_archive.zip'.
import zipfile def add_multiple_files_to_zip(zip_file_path, files_to_add): with zipfile.ZipFile(zip_file_path, 'a') as zip_ref: for file in files_to_add: zip_ref.write(file) zip_file_path = 'existing_archive.zip' files_to_add = ['file1.txt', 'file2.txt', 'file3.txt'] add_multiple_files_to_zip(zip_file_path, files_to_add)
Adding Files with a Custom Filter Function
For more complex scenarios, we need to add files to the ZIP archive? This may be done by utilizing a unique filter function. See how to put it into practice:
Example
In the following example, we defined 'custom_filter_function()' to accept a file and return True/False based on criteria ('.txt' extension and size > 1024 bytes). 'add_files_with_custom_filter()' receives the ZIP location, file list, and this function. It adds files to the ZIP archive only if the filter returns True.
import zipfile import os def custom_filter_function(file): # Your custom condition here return file.endswith('.txt') and os.path.getsize(file) > 1024 def add_files_with_custom_filter(zip_file_path, files_to_add, filter_func): with zipfile.ZipFile(zip_file_path, 'a') as zip_ref: for file in files_to_add: if filter_func(file): zip_ref.write(file, arcname=os.path.basename(file)) zip_file_path = 'existing_archive.zip' files_to_add = ['file1.txt', 'file2.txt', 'file3.jpg', 'file4.txt'] add_files_with_custom_filter(zip_file_path, files_to_add, custom_filter_function)