Open a bzip2-compressed file in binary or text mode. The filename argument can be an actual filename (a str, bytes, or PathLike object), or an existing file object to read from or write to. The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or "ab" for binary mode, o
(filename, mode="rb", compresslevel=9,
encoding=None, errors=None, newline=None)
| 277 | |
| 278 | |
| 279 | def open(filename, mode="rb", compresslevel=9, |
| 280 | encoding=None, errors=None, newline=None): |
| 281 | """Open a bzip2-compressed file in binary or text mode. |
| 282 | |
| 283 | The filename argument can be an actual filename (a str, bytes, or |
| 284 | PathLike object), or an existing file object to read from or write |
| 285 | to. |
| 286 | |
| 287 | The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or |
| 288 | "ab" for binary mode, or "rt", "wt", "xt" or "at" for text mode. |
| 289 | The default mode is "rb", and the default compresslevel is 9. |
| 290 | |
| 291 | For binary mode, this function is equivalent to the BZ2File |
| 292 | constructor: BZ2File(filename, mode, compresslevel). In this case, |
| 293 | the encoding, errors and newline arguments must not be provided. |
| 294 | |
| 295 | For text mode, a BZ2File object is created, and wrapped in an |
| 296 | io.TextIOWrapper instance with the specified encoding, error |
| 297 | handling behavior, and line ending(s). |
| 298 | |
| 299 | """ |
| 300 | if "t" in mode: |
| 301 | if "b" in mode: |
| 302 | raise ValueError("Invalid mode: %r" % (mode,)) |
| 303 | else: |
| 304 | if encoding is not None: |
| 305 | raise ValueError("Argument 'encoding' not supported in binary mode") |
| 306 | if errors is not None: |
| 307 | raise ValueError("Argument 'errors' not supported in binary mode") |
| 308 | if newline is not None: |
| 309 | raise ValueError("Argument 'newline' not supported in binary mode") |
| 310 | |
| 311 | bz_mode = mode.replace("t", "") |
| 312 | binary_file = BZ2File(filename, bz_mode, compresslevel=compresslevel) |
| 313 | |
| 314 | if "t" in mode: |
| 315 | encoding = io.text_encoding(encoding) |
| 316 | return io.TextIOWrapper(binary_file, encoding, errors, newline) |
| 317 | else: |
| 318 | return binary_file |
| 319 | |
| 320 | |
| 321 | def compress(data, compresslevel=9): |
searching dependent graphs…