Its easy to zip and unizip files with python built in zipfile module
For this we have to get the file location list . which files we wan to zip. we can use python glob to get the path
current example structure
├── file_location_list.py
└── NewFolder
├── image.png
└── text.txt
import glob
files=glob.glob("NewFolder/*")
print(files)
It will print the result like this
['NewFolder/image.png', 'NewFolder/text.txt']
So it print all the file location in the NewFolder
In the previous example we got the file location list. Now we will get the list files and create a zip file
import glob
files=glob.glob("NewFolder/*")
print(files)
from zipfile import ZipFile
# Create a ZipFile object in write mode
with ZipFile('NewZipFile.zip', 'w') as z:
for file in files:
z.write(file)
print('successfully created zip file!')
Now unzip the files
from zipfile import ZipFile
# replace NewZipFile.zip with your zip file which you want to unzip
with ZipFile('NewZipFile.zip', 'r') as z:
z.extractall()
print('Successfully unzipped files')