Create a ZIP file and add files to it using Python
Hello friend, You must have created a lot of ZIP files using a number of tools. In this tutorial, I will tell you how you can create a ZIP file and add files to it using Python.
Creating a ZIP file using Python
To work with ZIP files, Python has an inbuilt module called zipfile. For creating and uncompressing a ZIP file we use its ZipFile object. I have imported zipfile.ZipFile to my code. To create a ZIP file, all you need to do is to open a new ZIP file in write mode or append mode. If you open your ZIP file in these modes even if doesn’t exist, it creates a new one.
Code :
import os
from zipfile import ZipFile
zip_file_obj = ZipFile("MY ZIP.zip", 'w')
if os.path.exists('MY ZIP.zip'):
print("Created Successfully")
else:
print("Could not create a ZIP file")I have opened a ZIP file with the name MY ZIP.zip in 'w' mode. After creating I can also check if my ZIP file has been created properly or not. I have used os.path function to check if my ZIP file has been created or not.
Output :
Created Successfully
Adding files to ZIP files using Python
Now you can add files to this ZIP file. I am going to tell you about the three ways you can add files to your ZIP file.
1. Add individual files
Using the write() function I can add files one by one to my ZIP file. The write function takes the path of the file to be added as an argument. I have added two files to my ZIP file, 1.png and 3.png.
Code :
with ZipFile("MY ZIP.zip", 'w') as zip_file:
zip_file.write("C:\\Users\\PC\\Desktop\\intern_python\\ZIP files\\1.png")
zip_file.write("C:\\Users\\PC\\Desktop\\intern_python\\ZIP files\\3.png")Output :

2. Add the entire directory
Code :
I have used a for loop to iterate over the directory I want to add to the ZIP file. In this example, I have run a for loop over a folder named ZIP files. It contains image file named 1.png, 2.png, 3.png, 4.png, and 5.png.
with ZipFile("MY ZIP.zip", 'w') as zip_file:
for folder_name, sub_folder_name, file_name in os.walk('C:\\Users\\PC\\Desktop\\intern_python\\ZIP files'):
for files in file_name:
file_path = os.path.join(folder_name, files)
zip_file.write(file_path, os.path.basename(file_path))While iterating I have taken the path of the directory. The for loop takes one file for each iteration. I have used the os.path function to join the base directory path with the new file’s path and stored it into a temporary variable file_path. After creating a path, I can now add this file to the ZIP file, by using the write() function and passing the file’s path as argument.
Output :

3 . Add files from the directory that suffice the condition
For example purposes, I have decided to add those image files whose names are divisible by 2. It is similar to my previous example, but this time I have added an if condition which checks if the name of the image file is divisible by 2. For checking I have used the split function which you can check out from our post on String split and join in Python.
Code :
with ZipFile("MY ZIP.zip", 'w') as zip_file:
for folder_name, sub_folder_name, file_name in os.walk('C:\\Users\\PC\\Desktop\\intern_python\\ZIP files'):
for files in file_name:
if ((int(files.split('.')[0])) % 2 == 0):
file_path = os.path.join(folder_name, files)
zip_file.write(file_path, os.path.basename(file_path))As you know the split() function returns a list, and I need to check the number of the file’s name. I have used the index 0 which will return me the number in the string format which I can change to an integer by using the int() function. After getting their integer value I checked if it was divisible or not. If yes, I have added it to the ZIP file, using the write() function.
Output :

Now you can create a ZIP file using and add files to it.
Leave a Reply