I want to gzip a file in Python. I am trying to use the subprocss.check_call(), but it keeps failing with the error \'OSError: [Errno 2] No such file or directory\'. Is ther
Read the original file in binary (rb) mode and then use gzip.open to create the gzip file that you can write to like a normal file using writelines:
import gzip
with open("path/to/file", 'rb') as orig_file:
with gzip.open("path/to/file.gz", 'wb') as zipped_file:
zipped_file.writelines(orig_file)
Even shorter, you can combine the with statements on one line:
with open('path/to/file', 'rb') as src, gzip.open('path/to/file.gz', 'wb') as dst:
dst.writelines(src)