How to Compress Files Using gzip in Python Easily
To compress a file using
gzip in Python, open the original file in binary read mode and a new file in gzip.open write mode, then copy the contents. The gzip module handles the compression automatically.Syntax
Use gzip.open(filename, mode) to open a file for compression. The mode should be 'wb' for writing compressed data. Read the original file in binary mode and write to the gzip file.
- filename: The name of the gzip file to create.
- mode: Use
'wb'to write compressed data. - Read the source file in
'rb'mode to get bytes.
python
import gzip with open('input.txt', 'rb') as f_in: with gzip.open('output.txt.gz', 'wb') as f_out: f_out.writelines(f_in)
Example
This example compresses a text file named example.txt into example.txt.gz using the gzip module. It reads the original file in binary mode and writes the compressed data to the new file.
python
import gzip input_file = 'example.txt' output_file = 'example.txt.gz' with open(input_file, 'rb') as f_in: with gzip.open(output_file, 'wb') as f_out: f_out.writelines(f_in) print(f'File "{input_file}" compressed to "{output_file}" successfully.')
Output
File "example.txt" compressed to "example.txt.gz" successfully.
Common Pitfalls
Common mistakes when compressing files with gzip in Python include:
- Opening the source file in text mode instead of binary mode, which can cause errors or corrupted output.
- Using
'w'mode instead of'wb'for gzip output, which will fail because gzip expects binary mode. - Not closing files properly, which can be avoided by using
withstatements.
python
import gzip # Wrong way: opening source file in text mode # with open('input.txt', 'r') as f_in: # with gzip.open('output.gz', 'wb') as f_out: # f_out.writelines(f_in) # This will cause an error # Right way: with open('input.txt', 'rb') as f_in: with gzip.open('output.gz', 'wb') as f_out: f_out.writelines(f_in)
Quick Reference
Remember these tips when compressing files with gzip in Python:
- Always open files in binary mode (
'rb'for reading,'wb'for writing). - Use
gzip.open()to write compressed files. - Use
withblocks to handle files safely and automatically close them. - Compressed files usually have a
.gzextension.
Key Takeaways
Use the gzip module's open function with 'wb' mode to write compressed files.
Always open the original file in binary mode ('rb') to avoid errors.
Use with statements to ensure files are properly closed after compression.
Compressed files typically have a .gz extension to indicate gzip format.