Skip to content

Documentation example using gzip in combination with pathlib #112295

@dennisvang

Description

@dennisvang

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions