How to Read a .gz File in Python

In this tutorial, you will learn how to open and read a .gz (gzip) file in Python. Gzip is a popular data compression program and gzip files are often used to save disk space or to reduce the amount of time needed for transferring files over the network.

Setup: Install the Necessary Module

The gzip module in Python is used for reading and writing .gz files. However, it’s part of Python’s standard library, so you don’t need to manually install it.

Step 1: Open the .gz File

First, you need to open the file. This can be done using the gzip.open() method. As parameters, you pass the filename and the mode (which should be ‘rt’ for reading text).

Step 2: Read the File

Next, read the file contents. This is done with the Read() method. We have already done this in the above code snippet.

Step 3: Print the File Contents

To check whether you’ve correctly read the information, you can print the data using Python’s built-in print() function.

Full Python Code

Output

The output will be the contents of the gzip file you’ve read from.

Example Gz File Content

Here is an example content of gzip file:

'Lorem ipsum dolor sit amet....'

Conclusion

Reading .gz files in Python is not complicated. If you remember to use the gzip open() function to open the file and the read() method to extract the contents, you should have no problem. Hope you found this tutorial helpful.