Open file handles¶
-
pshell.
open
(file: Union[str, pathlib.Path, int, BinaryIO], mode: str = 'r', *, encoding: str = None, errors: str = None, compression: Union[str, bool] = 'auto', **kwargs) → 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 unlikegzip.open()
,bz2.open()
, andlzma.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 toreplace
instead ofstrict
. - 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