-
-
Save dreamingbinary/35049c88b9c9f4494c974f3e7579c19f to your computer and use it in GitHub Desktop.
Python: Compress a UTF-8 file using GZIP compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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