File Decompression in Python

Decompress a gzip file back to plain text.

IntermediateFile Handling ProgramsExample 19 of 20
file-decompression.py
Run in browser
1# Program to decompress a gzip file
2
3import gzip
4
5src = input("Enter source gzip filename: ")
6dst = input("Enter destination text filename: ")
7
8try:
9 with gzip.open(src, "rb") as f_in, open(dst, "wb") as f_out:
10 f_out.writelines(f_in)
11 print("Decompressed", src, "to", dst)
12except FileNotFoundError:
13 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

What's going on

Reverses the compression process by reading from gzip and writing raw bytes to a normal file.