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', 'zstd', 'zstandard', '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 automatic compression/decompression
- Parameters:
file – Path to the file to be opened or file descriptor to be wrapped. If compression is set to ‘auto’, ‘gzip’, ‘bzip2’, ‘lzma’, ‘zstd’, or ‘zstandard’, 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(),lzma.open(), orcompression.zstd.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 toreplaceinstead ofstrict.compression –
One of:
- False
No compression (use builtin
open())gzipgzip compression (use
gzip.open())bzip2bzip2 compression (use
bz2.open())lzmalzma compression (use
lzma.open())zstd,zstandardzstd compression (use
compression.zstd.open()). Requires either Python 3.14+ or thebackports.zstdpackage.auto(default):Automatically set compression if the file extension is
.gz,.bz2,.xz,.zst, or.zstd(case insensitive)
kwargs – Passed verbatim to the underlying open function