Explore

Python Programs

Convert File To Zip In Python Programming

Convert File To Zip In Python

Want to know how to convert file to zip in python, then you are at the right place today in this tutorial. I will show you how to create a python program to convert any file or folder to zip in python, so follow this tutorial till the end.

So 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 it is going to be very simple program so now let’s see how to do it.

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. Now to run this programs you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer.

To run this python program, follow the below steps:

  • Create a new folder for this python project.
  • Open it in a code editor of your choice.
  • Create a python file with an ending .py extension.
  • Copy the above code and paste it in your file.
  • Open a terminal or CMD at folder location
  • And type this command python filename.py (Name of your file).

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.

Don’t want to create all the files and folders then you can run this program now using this online python compiler it is fast and easy.

Summary

This was the python program to convert file to zip. I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might be interested in this program.

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 🙂