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

Function open

Lib/bz2.py:279–318  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

277
278
279def 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
321def compress(data, compresslevel=9):

Callers 15

_maybe_openFunction · 0.70
mainFunction · 0.70
mainFunction · 0.70
_fwalkFunction · 0.70
_mainFunction · 0.70
translationFunction · 0.70
extend_pathFunction · 0.70
__setupMethod · 0.70
_read_outputFunction · 0.70
_get_system_versionFunction · 0.70
compile_fileFunction · 0.70
mainFunction · 0.70

Calls 2

BZ2FileClass · 0.85
replaceMethod · 0.45

Tested by 2

_load_testfileFunction · 0.56
mainFunction · 0.40

Used in the wild real call sites across dependent graphs

searching dependent graphs…