Open file handles

pshell.open(file: str | Path | int | BinaryIO, mode: str = 'r', *, encoding: str | None = None, errors: str | None = None, compression: Literal[False, 'gzip', 'bzip2', 'lzma', 'auto'] = 'auto', **kwargs: Any) IO

Open a file handle to target file name or file descriptor.

Unlike the builtin function, this wrapper:

  • performs automatic environment variable resolution in the file name

  • logs the file access

  • supports transparent compression

Parameters:
  • file – Path to the file to be opened or file descriptor to be wrapped. If compression is set to ‘gzip’, ‘bzip2’ or ‘lzma’, file can also be a binary file handle.

  • mode (str) – As in the builtin open() function. It always defaults to text mode unless ‘b’ is explicitly specified; this is unlike gzip.open(), bz2.open(), and lzma.open() which instead default to binary mode.

  • encoding (str) – Character encoding when in text mode. Unlike the builtin open() function, it always defaults to utf-8 instead of being platform-specific.

  • errors (str) – As in the builtin open() function, but it defaults to replace instead of strict.

  • compression

    One of:

    False

    No compression (use builtin open())

    ’gzip’

    gzip compression (use gzip.open())

    ’bzip2’:

    bzip2 compression (use bz2.open())

    ’lzma’:

    lzma compression (use lzma.open())

    ’auto’:

    Automatically set compression if the file extension is .gz, .bz2, or .xz (case insensitive)

  • kwargs – Passed verbatim to the underlying open function