On this page
  1. bz2.open
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

bz2.open

Prev: bz2.decompress | Up: bz2
Rel: bz2.BZ2Compressor, bz2.BZ2Decompressor, bz2.BZ2File, bz2.compress
Ref: docs.python.org — bz2.open
Tags: #public #python3 #stdlib #bz2


Import

import bz2
from bz2 import open

Signature & Overview

bz2.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)

Open a bzip2-compressed file in binary or text mode.

Usage

import bz2
from pathlib import Path
import tempfile

with tempfile.TemporaryDirectory() as tmp:
    p = Path(tmp) / "note.txt.bz2"
    with bz2.open(p, "wt", encoding="utf-8") as f:
        f.write("hello bz2")
    with bz2.open(p, "rt", encoding="utf-8") as f:
        assert f.read() == "hello bz2"

Gotchas

REPL Inspect

from bz2 import open
help(open)

Linked from

Related notes