flib.io.raw module

File I/O for binary files.

load(file: Union[IO[Any], os.PathLike, flib.io.abc.Connector], encoding: Optional[str] = None, compressed: bool = False) → bytes

Load binary data from file.

Parameters:
  • file – String or path-like object that points to a readable file in the directory structure of the system, or a file object in reading mode.
  • encoding – Encodings specified in RFC 3548. Allowed values are: ‘base16’, ‘base32’, ‘base64’ and ‘base85’ or None for no encoding. By default no encoding is used.
  • compressed – Boolean value which determines, if the returned binary data shall be decompressed by using :func:zlib.decompress.
Returns:

Content of the given file as bytes object.

openx(file: Union[IO[Any], os.PathLike, flib.io.abc.Connector], mode: str = 'rb') → Iterator[io.BufferedIOBase]

Context manager to provide a unified interface to binary files.

This context manager extends the standard implementation of :py:func`open` by allowing the passed file argument to be a str or path-like object, which points to a valid filename in the directory structure of the system, or a file object. If the file argument is a str or a path-like object, the given path may contain application variables, like %home% or %user_data_dir%, which are extended before returning a file handler to a binary file. Afterwards, when exiting the with-statement, the file is closed. If the file argument, however, is a file object, the file is not closed, when exiting the with-statement.

Parameters:
  • file – String or path-like object that points to a valid filename in the directory structure of the system, or a file object.
  • mode – String, which characters specify the mode in which the file stream is opened or wrapped. The default mode is reading mode. Suported characters are: ‘r’: Reading mode (default) ‘w’: Writing mode
Yields:

binary file in reading or writing mode.

save(data: Union[bytes, bytearray, memoryview, str], file: Union[IO[Any], os.PathLike, flib.io.abc.Connector], encoding: Optional[str] = None, compression: Optional[int] = None) → None

Save binary data to file.

Parameters:
  • data – Binary data given as bytes-like object or string
  • file – String or path-like object that points to a writable file in the directory structure of the system, or a file object in writing mode.
  • encoding – Encodings specified in RFC 3548. Allowed values are: ‘base16’, ‘base32’, ‘base64’ and ‘base85’ or None for no encoding. By default no encoding is used.
  • compression – Determines the compression level for zlib.compress(). By default no zlib compression is used. For an integer ranging from -1 to 9, a zlib compression with the respective compression level is used. Thereby -1 is the default zlib compromise between speed and compression, 0 deflates the given binary data without attempted compression, 1 is the fastest compression with minimum compression capability and 9 is the slowest compression with maximum compression capability.