Python Gzip: Explanation and Examples

Hello coders!! In this article, we will learn about python gzip along with certain in-depth examples. At first, we must understand what does the term gzip mean? Gzip is an application that is used to compress and decompress files. It has a simple interface in python GZip Application, making it more efficient and easy to use. Without wasting any more time, let’s learn about the python gzip module in detail.

Functions present in Python gzip Module:

1) open():

It can open any compressed text or binary file. It returns a file as an object.

Syntax:, mode=’rb’, compresslevel=9, encoding=None, errors=None, newline=None)

  • Filename – it is the name of the file that needs to be opened.
  • Mode – it comprises various option like for both binary and text file
    • Options available in binary mode : ‘a’, ‘ab’, ‘w’, ‘wb’, ‘r’, ‘rb’, ‘x’, ‘xb’
    • Options available in text mode : ‘rt’, ‘at’, ‘wt’, or ‘xt’
    • By default, it opens in ‘rb’ (reading binary data) mode.
  • Compress level – it is an integer range from 0-9 as the Gzipfile constructor used to control the level of compression.
  • Encoding, error, newline – it is not required in binary mode. However, in text mode, these values need to be specified.


It is used to compress data. A byte object is returned by this function. By default, the compression level is set at 9.

Syntax: gzip.compress(data, compresslevel=9, *, mtime=None)

  • Data – the data that needs to be compressed
  • compresslevel has the same meaning
  • mtime – It reads the value of time of last modification. Its initial value is None


It is used to decompress the data and it returns the bytes of the decompressed data.

Syntax: gzip.decompress(data)

  • data – The data that needs to be decompressed.

Command-line Interface of Python Gzip:

Sl NoCommandFunction
1fileif the file is not specified, it is read from sys.stdin
2–fastshows the fastest compression method (less compression)
3–bestshows the slowest compression method (best compression)
4-d/–decompressdecompress the given file
5-h/–helpdisplays the help message
Command line interface for python gzip

Examples to understand Python gzip:

Example 1: Create a compressed gzip file:

import gzip
content = b"Hello coders!!"
with'sample.txt.gz', 'wb') as file:

By executing this command a sample.txt file is created in the current directory.

python gzip example
smple.txt file created in the current directory

Example 2: Read a compressed file:

with"sample.txt.gz", "rb") as file:
    data =

By executing this command, we will be able to read the contents of our compressed gzip file sample.txt

Example 3: Compress an existing file

file = open("sample1.txt","rb")
data =
bindata = bytearray(data)
with"sample2.txt.gz", "wb") as f:
  • In this we have first opened an existing file sample1.txt
  • After that we copied its contents into the variable data
  • We then converted it into a byte array
  • Lastly, we compressed the data in sample2.txt
Compressed file sample2.txt created from sample1
Compressed file sample2.txt created from sample1

Example 4: Compress a binary string:

inp = b"Hello coders!!"
out = gzip.compress(inp)

Advantages of Python Gzip:

  • gzip is surprisingly easy to use
  • Saves bandwidth
  • Decreases transfer time
  • Increases the response speed

Disadvantages of Python Gzip:

  • Slightly increases the load on server as compression and decompression both requires CPU cycles

Must Read


In this article, we learned about the python gzip module and how it helps us in various ways. We have also seen various examples to learn how we can implement them in our program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Notify of
Inline Feedbacks
View all comments