Documentation
The gzip documentation provides some examples of usage.
I wonder if it would be possible (and desirable) to add an example using gzip with pathlib.
For example, this snippet from the documentation uses gzip.open() together with shutil:
Example of how to GZIP compress an existing file:
import gzip
import shutil
with open('/home/joe/file.txt', 'rb') as f_in:
with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
Something similar can be done using pathlib and gzip.compress(), as follows:
import gzip
import pathlib
path_in = pathlib.Path('/home/joe/file.txt')
path_out = path_in.with_suffix(path_in.suffix + '.gz')
path_out.write_bytes(gzip.compress(path_in.read_bytes()))
Although, I'm not sure if this is better or worse than the shutil example, in terms of memory usage, etc., I do like the readability of the pathlib oneliner.
An additional advantage is that we can now set the mtime argument, which is not explicitly supported by gzip.open(). For example:
path_out.write_bytes(gzip.compress(data=path_in.read_bytes(), mtime=...))
Update:
A notable difference between the gzip.open() approach vs the gzip.compress() approach is that the latter does not include the filename in the gzip header. Also see GzipFile docs and rfc1952.
Documentation
The
gzipdocumentation provides some examples of usage.I wonder if it would be possible (and desirable) to add an example using
gzipwithpathlib.For example, this snippet from the documentation uses
gzip.open()together withshutil:Something similar can be done using
pathlibandgzip.compress(), as follows:Although, I'm not sure if this is better or worse than the
shutilexample, in terms of memory usage, etc., I do like the readability of thepathliboneliner.An additional advantage is that we can now set the
mtimeargument, which is not explicitly supported bygzip.open(). For example:Update:
A notable difference between the
gzip.open()approach vs thegzip.compress()approach is that the latter does not include the filename in the gzip header. Also see GzipFile docs and rfc1952.