11. pypsi.utils - Utility functions

Utility functions and classes.

pypsi.utils.safe_open(file, mode='r', chunk_size=4096, ascii_is_utf8=True, **kwargs)[source]

Detect a file’s encoding, skip any Byte Order Marks that the are located at the beginning of the file, and returns the opened file stream. The file argument can be either a string containing a path to the file or an already open binary file-like object.

Parameters:
  • file (str) – path to the file or a binary file-like object
  • mode (str) – the mode to open the file (see open())
  • chunk_size (int) – number of bytes to read to determine encoding
  • ascii_is_utf8 (bool) – whether to force UTF-8 encoding if the file is dected as ASCII
  • errors (str) – determines how errors are handled and is passed to the call to open().
Returns file:

the opened file stream