How we can compress large Python files?

We can use Python to compress or extract files. We use the zipfile module in Python, to extract or compress individual or multiple files at once. This process is is easy and requires very little code. We begin by importing the zipfile module and then open the ZipFile object in write mode by specifying the second parameter as 'w'. The file to be zipped is in the same folder as this code file or the path of the file to be zipped can be specified instead. Here is the code that you need −

import zipfile
foo_zip = zipfile.ZipFile ( '', 'w' )
foo_zip.write ( 'foo.txt', compress_type=zipfile.ZIP_DEFLATED )
foo_zip.close ()

This creates zipped file from the file foo.txt