File Decompression
Decompress a gzip file back to plain text.
IntermediateTopic: File Handling Programs
Python File Decompression Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to decompress a gzip file
import gzip
src = input("Enter source gzip filename: ")
dst = input("Enter destination text filename: ")
try:
with gzip.open(src, "rb") as f_in, open(dst, "wb") as f_out:
f_out.writelines(f_in)
print("Decompressed", src, "to", dst)
except FileNotFoundError:
print("Source gzip file not found.")Output
Enter source gzip filename: notes.txt.gz Enter destination text filename: notes_copy.txt Decompressed notes.txt.gz to notes_copy.txt
Understanding File Decompression
Reverses the compression process by reading from gzip and writing raw bytes to a normal file.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.