MCPcopy Index your code
hub / github.com/python/cpython / BZ2File

Class BZ2File

Lib/bz2.py:26–276  ·  view source on GitHub ↗

A file object providing transparent bzip2 (de)compression. A BZ2File can act as a wrapper for an existing file object, or refer directly to a named file on disk. Note that BZ2File provides a *binary* file interface - data read is returned as bytes, and data to be written should be

Source from the content-addressed store, hash-verified

24
25
26class BZ2File(_streams.BaseStream):
27
28 """A file object providing transparent bzip2 (de)compression.
29
30 A BZ2File can act as a wrapper for an existing file object, or refer
31 directly to a named file on disk.
32
33 Note that BZ2File provides a *binary* file interface - data read is
34 returned as bytes, and data to be written should be given as bytes.
35 """
36
37 def __init__(self, filename, mode="r", *, compresslevel=9):
38 """Open a bzip2-compressed file.
39
40 If filename is a str, bytes, or PathLike object, it gives the
41 name of the file to be opened. Otherwise, it should be a file
42 object, which will be used to read or write the compressed data.
43
44 mode can be 'r' for reading (default), 'w' for (over)writing,
45 'x' for creating exclusively, or 'a' for appending. These can
46 equivalently be given as 'rb', 'wb', 'xb', and 'ab'.
47
48 If mode is 'w', 'x' or 'a', compresslevel can be a number between 1
49 and 9 specifying the level of compression: 1 produces the least
50 compression, and 9 (default) produces the most compression.
51
52 If mode is 'r', the input file may be the concatenation of
53 multiple compressed streams.
54 """
55 self._fp = None
56 self._closefp = False
57 self._mode = None
58
59 if not (1 <= compresslevel <= 9):
60 raise ValueError("compresslevel must be between 1 and 9")
61
62 if mode in ("", "r", "rb"):
63 mode = "rb"
64 mode_code = _MODE_READ
65 elif mode in ("w", "wb"):
66 mode = "wb"
67 mode_code = _MODE_WRITE
68 self._compressor = BZ2Compressor(compresslevel)
69 elif mode in ("x", "xb"):
70 mode = "xb"
71 mode_code = _MODE_WRITE
72 self._compressor = BZ2Compressor(compresslevel)
73 elif mode in ("a", "ab"):
74 mode = "ab"
75 mode_code = _MODE_WRITE
76 self._compressor = BZ2Compressor(compresslevel)
77 else:
78 raise ValueError("Invalid mode: %r" % (mode,))
79
80 if isinstance(filename, (str, bytes, os.PathLike)):
81 self._fp = _builtin_open(filename, mode)
82 self._closefp = True
83 self._mode = mode_code

Callers 15

bz2openMethod · 0.90
testReadMethod · 0.90
testReadBadFileMethod · 0.90
testReadMultiStreamMethod · 0.90
testReadTrailingJunkMethod · 0.90
testRead0Method · 0.90
testReadChunk10Method · 0.90
testRead100Method · 0.90
testPeekMethod · 0.90

Calls

no outgoing calls

Tested by 15

testReadMethod · 0.72
testReadBadFileMethod · 0.72
testReadMultiStreamMethod · 0.72
testReadTrailingJunkMethod · 0.72
testRead0Method · 0.72
testReadChunk10Method · 0.72
testRead100Method · 0.72
testPeekMethod · 0.72
testReadIntoMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…