How to Create and Extract Zip file in Python
What is ZIP File?
- A ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. (or)
- A zip file is a way of grouping or archiving multiple files in one file.
Now, we will see how to create a zip file using python and how to extract that zip file using the same python. It is very simple and easy, Let’s move on
Create ZIP Files
To create a ZIP file, we have to import one built-in module called zipfile with import keyword. we have to compress our files, we will go to write mode.
import zipfile # create open zip file in write mode with zipfile.ZipFile('zip_file.zip', 'w') as zip: #write files to a zip file zip.write('iterathon.txt') zip.write('image.jpg') print('Zip File is created successfully')
In the above program, I created a zip file name called zip_file.zip then, I stored two files in a zip_file( iterathon.txt and image.jpg). Once a zip_file was created successfully, It shows a successful message.
Extract Zip Files
we want to unzip our zip files, we will go to read mode and use extractall().
import zipfile # open zip file in read mode with zipfile.ZipFile('zip_file.zip', 'r') as zip: # Extract files zip.extractall('zip_file') print('Zip File is extracted successfully')
Use the extractall
() method to unzip the files using zipfile module.
My Previous Blog
Random Password Generator in Python: https://iterathon.tech//random-password-generator-in-python/
LEARN MORE ❣