On this page
bz2.BZ2File
Prev: bz2.BZ2Decompressor | Up: bz2 | Next: bz2.compress
Rel: bz2.BZ2Compressor, bz2.BZ2Decompressor, bz2.compress, bz2.decompress
Ref: docs.python.org — bz2.BZ2File
Tags: #public #python3 #stdlib #bz2
Import
import bz2
from bz2 import BZ2File
Signature & Overview
bz2.BZ2File(filename, mode='r', *, compresslevel=9)
A file object providing transparent bzip2 (de)compression.
Methods & Attributes
BZ2File.close(self)— Flush and close the file.BZ2File.closed(...)— True if this file is closed.BZ2File.detach(self, /)— Disconnect this buffer from its underlying raw stream and return it.BZ2File.fileno(self)— Return the file descriptor for the underlying file.BZ2File.flush(self, /)— Flush write buffers, if applicable.BZ2File.isatty(self, /)— Return whether this is an 'interactive' stream.BZ2File.mode(...)—BZ2File.modemethod/property.BZ2File.name(...)—BZ2File.namemethod/property.BZ2File.peek(self, n=0)— Return buffered data without advancing the file position.BZ2File.read(self, size=-1)— Read up to size uncompressed bytes from the file.BZ2File.read1(self, size=-1)— Read up to size uncompressed bytes, while trying to avoid making multiple reads from the underlying stream. Reads up to a buffer's worth of data if size is negative.BZ2File.readable(self)— Return whether the file was opened for reading.BZ2File.readinto(self, b)— Read bytes into b.BZ2File.readinto1(self, buffer, /)—BZ2File.readinto1method/property.BZ2File.readline(self, size=-1)— Read a line of uncompressed bytes from the file.BZ2File.readlines(self, size=-1)— Read a list of lines of uncompressed bytes from the file.BZ2File.seek(self, offset, whence=0)— Change the file position.BZ2File.seekable(self)— Return whether the file supports seeking.BZ2File.tell(self)— Return the current file position.BZ2File.truncate(self, size=None, /)— Truncate file to size bytes.BZ2File.writable(self)— Return whether the file was opened for writing.BZ2File.write(self, data)— Write a byte string to the file.BZ2File.writelines(self, seq)— Write a sequence of byte strings to the file.
Usage
import bz2
import io
buf = io.BytesIO()
with bz2.BZ2File(buf, "wb") as f:
f.write(b"compressed")
buf.seek(0)
with bz2.BZ2File(buf, "rb") as f:
assert f.read() == b"compressed"
Gotchas
- Binary mode only on
BZ2File: Usebz2.open(..., "rt", encoding="utf-8")when reading or writing text.
REPL Inspect
from bz2 import BZ2File
help(BZ2File)
Linked from
Related notes
- tuts/python3/stdlib_module/bz2 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bz2/BZ2Compressor Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bz2/BZ2Decompressor Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bz2/compress Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bz2/decompress Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bz2/open links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib