Skip to content

Python zip file

  • by

Use zipfile method to zip files in Python. ZipFile is a class of zipfile modules for reading and writing zip files. The following command will zip the entire directory

shutil.make_archive(output_filename, 'zip', dir_name)

Following the command gives you control over the files you want to archive

ZipFile.write(filename)

Python zip file

Simple example code Use ZIP_DEFLATED, which is the most widely supported.

import zipfile

zip = zipfile.ZipFile("stuff.zip", "w", zipfile.ZIP_DEFLATED)
zip.write("test.txt")
zip.close()

Output:

Python zip file

Do comment if you have any doubts or suggestions on this Python zip topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading