TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python gzip: Compression Examples

Use the gzip module to compress data. Compare output sizes of compressed data.
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:\Codex.txt", "rb") as file_in: # Open output file. with gzip.open("C:\Codex.gz", "wb") as file_out: # Write output. file_out.writelines(file_in) Output Codex.txt size: 4404 bytes Codex.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:\Codex.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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf