bz2.BZ2File

class bz2.BZ2File

Open a bz2 file. The mode can be ‘r’ or ‘w’, for reading (default) or writing. When opened for writing, the file will be created if it doesn’t exist, and truncated otherwise. If the buffering argument is given, 0 means unbuffered, and larger numbers specify the buffer size. If compresslevel is given, must be a number between 1 and 9.

Add a ‘U’ to mode to open the file for input with universal newline support. Any line ending in the input file will be seen as a ‘n’ in Python. Also, a file so opened gains the attribute ‘newlines’; the value for this attribute is one of None (no newline read yet), ‘r’, ‘n’, ‘rn’ or a tuple containing all the newline types seen. Universal newlines are available only when reading.

Methods

__enter__(() -> self.)
__exit__((*excinfo) -> None.  Closes the file.)
__format__ default object formatter
__new__((S, ...)
__reduce__ helper for pickle
__reduce_ex__ helper for pickle
__sizeof__(() -> int) size of object in memory, in bytes
__subclasshook__ Abstract classes can override this to customize issubclass().
close(() -> None or (perhaps) an integer) Close the file.
read(([size]) -> string) Read at most size uncompressed bytes, returned as a string.
readline(([size]) -> string) Return the next line from the file, as a string, retaining newline.
readlines(([size]) -> list) Call readline() repeatedly and return a list of lines read.
seek((offset [, whence]) -> None) Move to new file position.
tell(() -> int) Return the current file position, an integer (may be a long integer).
write((data) -> None) Write the ‘data’ string to file.
writelines((sequence_of_strings) -> None) Write the sequence of strings to the file.
xreadlines(() -> self) For backward compatibility.

Attributes

closed True if the file is closed
mode file mode (‘r’, ‘w’, or ‘U’)
name file name
newlines end-of-line convention used in this file
next
softspace flag indicating that a space needs to be printed; used by print