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

Function open

tools/python-3.11.9-amd64/Lib/_pyio.py:77–280  ·  view source on GitHub ↗

r"""Open file and return a stream. Raise OSError upon failure. file is either a text or byte string giving the name (and the path if the file isn't in the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file de

(file, mode="r", buffering=-1, encoding=None, errors=None,
         newline=None, closefd=True, opener=None)

Source from the content-addressed store, hash-verified

75# See init_set_builtins_open() in Python/pylifecycle.c.
76@staticmethod
77def open(file, mode="r", buffering=-1, encoding=None, errors=None,
78 newline=None, closefd=True, opener=None):
79
80 r"""Open file and return a stream. Raise OSError upon failure.
81
82 file is either a text or byte string giving the name (and the path
83 if the file isn't in the current working directory) of the file to
84 be opened or an integer file descriptor of the file to be
85 wrapped. (If a file descriptor is given, it is closed when the
86 returned I/O object is closed, unless closefd is set to False.)
87
88 mode is an optional string that specifies the mode in which the file is
89 opened. It defaults to 'r' which means open for reading in text mode. Other
90 common values are 'w' for writing (truncating the file if it already
91 exists), 'x' for exclusive creation of a new file, and 'a' for appending
92 (which on some Unix systems, means that all writes append to the end of the
93 file regardless of the current seek position). In text mode, if encoding is
94 not specified the encoding used is platform dependent. (For reading and
95 writing raw bytes use binary mode and leave encoding unspecified.) The
96 available modes are:
97
98 ========= ===============================================================
99 Character Meaning
100 --------- ---------------------------------------------------------------
101 'r' open for reading (default)
102 'w' open for writing, truncating the file first
103 'x' create a new file and open it for writing
104 'a' open for writing, appending to the end of the file if it exists
105 'b' binary mode
106 't' text mode (default)
107 '+' open a disk file for updating (reading and writing)
108 ========= ===============================================================
109
110 The default mode is 'rt' (open for reading text). For binary random
111 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
112 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
113 raises an `FileExistsError` if the file already exists.
114
115 Python distinguishes between files opened in binary and text modes,
116 even when the underlying operating system doesn't. Files opened in
117 binary mode (appending 'b' to the mode argument) return contents as
118 bytes objects without any decoding. In text mode (the default, or when
119 't' is appended to the mode argument), the contents of the file are
120 returned as strings, the bytes having been first decoded using a
121 platform-dependent encoding or using the specified encoding if given.
122
123 buffering is an optional integer used to set the buffering policy.
124 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
125 line buffering (only usable in text mode), and an integer > 1 to indicate
126 the size of a fixed-size chunk buffer. When no buffering argument is
127 given, the default buffering policy works as follows:
128
129 * Binary files are buffered in fixed-size chunks; the size of the buffer
130 is chosen using a heuristic trying to determine the underlying device's
131 "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
132 On many systems, the buffer will typically be 4096 or 8192 bytes long.
133
134 * "Interactive" text files (files for which isatty() returns True)

Callers 2

_open_code_with_warningFunction · 0.70

Calls 11

isattyMethod · 0.95
filenoMethod · 0.95
FileIOClass · 0.85
BufferedRandomClass · 0.85
BufferedReaderClass · 0.85
text_encodingFunction · 0.85
TextIOWrapperClass · 0.85
BufferedWriterClass · 0.70
setFunction · 0.50
warnMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected