Pythondex
Python Conversion Programs

Convert File To Zip In Python

Last updated January 31, 2024 by Jarvis Silva

In this tutorial I will show you how to create a python program to convert any file or folder to zip in python, to create this program we will use ZipFile and Shutil Library they are both pre-installed with python so you don’t need to install anything so let’s start

Python Code To Convert File To Zip In Python


from zipfile import ZipFile

zip_file = ZipFile('sample.zip', 'w')
zip_file.write('text.txt')
zip_file.close()

Python Code To Convert A Folder To Zip In Python


from shutil import make_archive

make_archive('OutputZipFileName', 'zip', 'enter full path of the folder here')

Above are the python programs to convert file and folder to zip in python, after running this programs you will see a zip file is created in your folder which if you open you will see your files or folders.

Here are some more python drawing tutorials for you:

I hope you found what you were looking for from this tutorial, and if you want more python programs and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂