MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / open

Function open

tools/python-3.11.9-amd64/Lib/gzip.py:25–68  ·  view source on GitHub ↗

Open a gzip-compressed file in binary or text mode. The filename argument can be an actual filename (a str or bytes 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, or "rt", "w

(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST,
         encoding=None, errors=None, newline=None)

Source from the content-addressed store, hash-verified

23
24
25def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST,
26 encoding=None, errors=None, newline=None):
27 """Open a gzip-compressed file in binary or text mode.
28
29 The filename argument can be an actual filename (a str or bytes object), or
30 an existing file object to read from or write to.
31
32 The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or "ab" for
33 binary mode, or "rt", "wt", "xt" or "at" for text mode. The default mode is
34 "rb", and the default compresslevel is 9.
35
36 For binary mode, this function is equivalent to the GzipFile constructor:
37 GzipFile(filename, mode, compresslevel). In this case, the encoding, errors
38 and newline arguments must not be provided.
39
40 For text mode, a GzipFile object is created, and wrapped in an
41 io.TextIOWrapper instance with the specified encoding, error handling
42 behavior, and line ending(s).
43
44 """
45 if "t" in mode:
46 if "b" in mode:
47 raise ValueError("Invalid mode: %r" % (mode,))
48 else:
49 if encoding is not None:
50 raise ValueError("Argument 'encoding' not supported in binary mode")
51 if errors is not None:
52 raise ValueError("Argument 'errors' not supported in binary mode")
53 if newline is not None:
54 raise ValueError("Argument 'newline' not supported in binary mode")
55
56 gz_mode = mode.replace("t", "")
57 if isinstance(filename, (str, bytes, os.PathLike)):
58 binary_file = GzipFile(filename, gz_mode, compresslevel)
59 elif hasattr(filename, "read") or hasattr(filename, "write"):
60 binary_file = GzipFile(None, gz_mode, compresslevel, filename)
61 else:
62 raise TypeError("filename must be a str or bytes object, or a file")
63
64 if "t" in mode:
65 encoding = io.text_encoding(encoding)
66 return io.TextIOWrapper(binary_file, encoding, errors, newline)
67 else:
68 return binary_file
69
70def write32u(output, value):
71 # The L format writes the bit pattern correctly whether signed

Callers 1

mainFunction · 0.70

Calls 2

GzipFileClass · 0.85
replaceMethod · 0.45

Tested by

no test coverage detected