Open a Zstandard compressed file in binary or text mode. file can be either a file name (given as a str, bytes, or PathLike object), in which case the named file is opened, or it can be an existing file object to read from or write to. The mode parameter can be 'r', 'rb' (default),
(file, /, mode='rb', *, level=None, options=None, zstd_dict=None,
encoding=None, errors=None, newline=None)
| 293 | |
| 294 | |
| 295 | def open(file, /, mode='rb', *, level=None, options=None, zstd_dict=None, |
| 296 | encoding=None, errors=None, newline=None): |
| 297 | """Open a Zstandard compressed file in binary or text mode. |
| 298 | |
| 299 | file can be either a file name (given as a str, bytes, or PathLike object), |
| 300 | in which case the named file is opened, or it can be an existing file object |
| 301 | to read from or write to. |
| 302 | |
| 303 | The mode parameter can be 'r', 'rb' (default), 'w', 'wb', 'x', 'xb', 'a', |
| 304 | 'ab' for binary mode, or 'rt', 'wt', 'xt', 'at' for text mode. |
| 305 | |
| 306 | The level, options, and zstd_dict parameters specify the settings the same |
| 307 | as ZstdFile. |
| 308 | |
| 309 | When using read mode (decompression), the options parameter is a dict |
| 310 | representing advanced decompression options. The level parameter is not |
| 311 | supported in this case. When using write mode (compression), only one of |
| 312 | level, an int representing the compression level, or options, a dict |
| 313 | representing advanced compression options, may be passed. In both modes, |
| 314 | zstd_dict is a ZstdDict instance containing a trained Zstandard dictionary. |
| 315 | |
| 316 | For binary mode, this function is equivalent to the ZstdFile constructor: |
| 317 | ZstdFile(filename, mode, ...). In this case, the encoding, errors and |
| 318 | newline parameters must not be provided. |
| 319 | |
| 320 | For text mode, an ZstdFile object is created, and wrapped in an |
| 321 | io.TextIOWrapper instance with the specified encoding, error handling |
| 322 | behavior, and line ending(s). |
| 323 | """ |
| 324 | |
| 325 | text_mode = 't' in mode |
| 326 | mode = mode.replace('t', '') |
| 327 | |
| 328 | if text_mode: |
| 329 | if 'b' in mode: |
| 330 | raise ValueError(f'Invalid mode: {mode!r}') |
| 331 | else: |
| 332 | if encoding is not None: |
| 333 | raise ValueError('Argument "encoding" not supported in binary mode') |
| 334 | if errors is not None: |
| 335 | raise ValueError('Argument "errors" not supported in binary mode') |
| 336 | if newline is not None: |
| 337 | raise ValueError('Argument "newline" not supported in binary mode') |
| 338 | |
| 339 | binary_file = ZstdFile(file, mode, level=level, options=options, |
| 340 | zstd_dict=zstd_dict) |
| 341 | |
| 342 | if text_mode: |
| 343 | return io.TextIOWrapper(binary_file, encoding, errors, newline) |
| 344 | else: |
| 345 | return binary_file |
searching dependent graphs…