typhon.files

This module contains convenience functions for general file handling.

typhon.files.decompress(filename, tmpdir)[source]

Temporarily decompress file for reading.

Returns the full path to the uncompressed temporary file or the original filename if it was not compressed.

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 remove the decompressed file after use.

Parameters:
  • filename (str) – Input file.
  • tmpdir (str) – Path to directory for temporary storage of the uncompressed file. The directory must exist.
Returns:

Generator containing the path to the input filename.

Example

>>> tmpdir = '/tmp'
>>> with typhon.files.decompress('datafile.nc.gz', tmpdir) as file:
>>>     f = netCDF4.Dataset(file)
>>>     #...