compress

typhon.files.compress(filename, fmt=None, tmpdir=None)[source]

Compress a file after writing to it.

Supported compression formats are: gzip, bzip2, zip, and lzma (Python 3.3 or newer only).

This function is tailored for use in a with statement. It uses a context manager to automatically create a temporary file to which the data can be written. After writing to the file, it compresses and renames it.

TODO: Preferably, it should be possible to write to the compressed file directly instead of copying it.

Parameters:
  • filename – The path and name of the compressed that should be created. The filename’s extension decides to which format the file will be compressed (look at fmt for a list).

  • fmt – If want to specify the compression format without using the filename’s extension, use this argument. * zip: Uses the standard zip library. * bz2: Uses the bz2 library. * gz: Uses the GNU zip library. * xz: Uses the lzma format.

  • tmpdir (str) – Path to directory for temporary storage of the uncompressed file. The directory must exist. The default is the temporary dir of the system.

Yields:

Generator containing the path to the temporary file.

Examples

>>> with typhon.files.compress('datafile.nc.gz') as file:
>>>     f = netCDF4.Dataset(file, 'w')
>>>     #...