Python
# Program to compress a text file using gzip
import gzip
src = input("Enter source text filename: ")
dst = input("Enter destination gzip filename: ")
try:
with open(src, "rb") as f_in, gzip.open(dst, "wb") as f_out:
f_out.writelines(f_in)
print("Compressed", src, "to", dst)
except FileNotFoundError:
print("Source file not found.")Output
Enter source text filename: notes.txt Enter destination gzip filename: notes.txt.gz Compressed notes.txt to notes.txt.gz
gzip.open with 'wb' creates a gzip-compressed file; we stream bytes from the source file into it.