Python gzip: Compression Examples

These Python examples use the gzip module to compress data. They compare output sizes.
GZIP. With compression we trade time for space. In compression we apply algorithms that change data to require less physical memory.
This slows down parts of programs. But it makes other parts faster: less data needs transferring. An important compression algorithm is GZIP.
First example. Many web pages are transferred with GZIP compression. This reduces the time required to load pages. In Python, we can use the gzip module.

And: We open the source file and then open an output file. We then apply the gzip open() method to write the compressed file.

Tip: The with statement is helpful here. This statement ensures that system resources are properly freed.

Python program that uses gzip import gzip # Open source file. with open("C:\perls.txt", "rb") as file_in: # Open output file. with gzip.open("C:\perls.gz", "wb") as file_out: # Write output. file_out.writelines(file_in) Output perls.txt size: 4404 bytes perls.gz size: 2110 bytes
Decompress. The gzip module has two main functions. It compresses data and decompresses data. In this next example we decompress the same file that was written in the previous program.

And: In the output, we find the original file length is the same. No data was lost. We also have the file, in string format, in memory.

So: Instead of written the decompressed file to disk and reading that in, we can directly use the string contents.

Python program that decompresses file import gzip # Use open method. with gzip.open("C:\perls.gz", "rb") as f: # Read in string. content = f.read() # Print length. print(len(content)) Output 4404
7-Zip, subprocess. We can invoke 7-Zip with the subprocess module in Python. This can be done on Windows or other platforms (but modifications to the executable name are needed).subprocess
Some notes. The Python site has some useful information on GZIP. Compression collapses space. And it often reduces the time required to process data.gzip: Python.org
Data compression becomes increasingly important. As time passes, the quantity of data increases. And with the gzip module, we have an easy way to use a powerful, compatible algorithm.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls