Skip to content

Instantly share code, notes, and snippets.

@dreamingbinary
Forked from dmdavis/utf-8_gzip.py
Created November 6, 2019 21:34
Show Gist options
  • Save dreamingbinary/35049c88b9c9f4494c974f3e7579c19f to your computer and use it in GitHub Desktop.
Save dreamingbinary/35049c88b9c9f4494c974f3e7579c19f to your computer and use it in GitHub Desktop.
Python: Compress a UTF-8 file using GZIP compression
def compress_utf8_file(fullpath, delete_original = True):
"""Compress a UTF-8 encoded file using GZIP compression named *.gz. If `delete_original` is `True` [default: True],
the original file specified by `delete_original` is removed after compression."""
with codecs.open(fullpath, 'r', 'utf-8') as fin:
with gzip.open(fullpath + '.gz', 'wb') as fout:
for line in fin:
fout.write(unicode(line).encode('utf-8'))
if delete_original:
os.remove(fullpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment