File Decompression in Python
Decompress a gzip file back to plain text.
IntermediateFile Handling ProgramsExample 19 of 20
file-decompression.py
1# Program to decompress a gzip file23import gzip45src = input("Enter source gzip filename: ")6dst = input("Enter destination text filename: ")78try: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.
After this program
If the idea is still fuzzy, read Day 14: File Handling in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 file handling programs, or see every topic on Python exercises.