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

Function open

Lib/_pyio.py:75–271  ·  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 descrip

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

Source from the content-addressed store, hash-verified

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

Callers 1

_open_code_with_warningFunction · 0.70

Calls 10

_isatty_open_onlyMethod · 0.95
setFunction · 0.85
FileIOClass · 0.85
BufferedRandomClass · 0.85
text_encodingFunction · 0.85
TextIOWrapperClass · 0.85
BufferedWriterClass · 0.70
BufferedReaderClass · 0.70
warnMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…