blob: de88c572f3662e3f921019daa7f021387f556814 [file] [log] [blame]
Georg Brandl014197c2008-04-09 18:40:51 +00001:mod:`io` --- Core tools for working with streams
2=================================================
3
4.. module:: io
5 :synopsis: Core tools for working with streams.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl014197c2008-04-09 18:40:51 +00007.. moduleauthor:: Guido van Rossum <guido@python.org>
8.. moduleauthor:: Mike Verdone <mike.verdone@gmail.com>
9.. moduleauthor:: Mark Russell <mark.russell@zen.co.uk>
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000010.. moduleauthor:: Antoine Pitrou <solipsis@pitrou.net>
11.. moduleauthor:: Amaury Forgeot d'Arc <amauryfa@gmail.com>
Benjamin Petersonef9f2bd2009-05-01 20:45:43 +000012.. moduleauthor:: Benjamin Peterson <benjamin@python.org>
Benjamin Peterson058e31e2009-01-16 03:54:08 +000013.. sectionauthor:: Benjamin Peterson <benjamin@python.org>
Georg Brandl014197c2008-04-09 18:40:51 +000014
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040015**Source code:** :source:`Lib/io.py`
16
17--------------
18
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000019.. _io-overview:
20
Antoine Pitroub530e142010-08-30 12:41:00 +000021Overview
22--------
Georg Brandl014197c2008-04-09 18:40:51 +000023
R David Murray9f0c9402012-08-17 20:33:54 -040024.. index::
25 single: file object; io module
26
27The :mod:`io` module provides Python's main facilities for dealing with various
28types of I/O. There are three main types of I/O: *text I/O*, *binary I/O*
29and *raw I/O*. These are generic categories, and various backing stores can
30be used for each of them. A concrete object belonging to any of these
31categories is called a :term:`file object`. Other common terms are *stream*
32and *file-like object*.
Georg Brandl014197c2008-04-09 18:40:51 +000033
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)cd449802018-11-12 09:36:18 +053034Independent of its category, each concrete stream object will also have
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000035various capabilities: it can be read-only, write-only, or read-write. It can
36also allow arbitrary random access (seeking forwards or backwards to any
37location), or only sequential access (for example in the case of a socket or
38pipe).
Georg Brandl014197c2008-04-09 18:40:51 +000039
Antoine Pitroub530e142010-08-30 12:41:00 +000040All streams are careful about the type of data you give to them. For example
41giving a :class:`str` object to the ``write()`` method of a binary stream
Stéphane Wirtele483f022018-10-26 12:52:11 +020042will raise a :exc:`TypeError`. So will giving a :class:`bytes` object to the
Antoine Pitroub530e142010-08-30 12:41:00 +000043``write()`` method of a text stream.
Georg Brandl014197c2008-04-09 18:40:51 +000044
Antoine Pitroua787b652011-10-12 19:02:52 +020045.. versionchanged:: 3.3
Eli Benderskyf877a7c2012-07-14 21:22:25 +030046 Operations that used to raise :exc:`IOError` now raise :exc:`OSError`, since
47 :exc:`IOError` is now an alias of :exc:`OSError`.
Antoine Pitroua787b652011-10-12 19:02:52 +020048
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000049
Antoine Pitroub530e142010-08-30 12:41:00 +000050Text I/O
51^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +000052
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000053Text I/O expects and produces :class:`str` objects. This means that whenever
54the backing store is natively made of bytes (such as in the case of a file),
55encoding and decoding of data is made transparently as well as optional
56translation of platform-specific newline characters.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000057
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000058The easiest way to create a text stream is with :meth:`open()`, optionally
59specifying an encoding::
Antoine Pitroub530e142010-08-30 12:41:00 +000060
61 f = open("myfile.txt", "r", encoding="utf-8")
62
63In-memory text streams are also available as :class:`StringIO` objects::
64
65 f = io.StringIO("some initial text data")
66
Eli Benderskyf877a7c2012-07-14 21:22:25 +030067The text stream API is described in detail in the documentation of
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000068:class:`TextIOBase`.
Antoine Pitroub530e142010-08-30 12:41:00 +000069
Antoine Pitroub530e142010-08-30 12:41:00 +000070
71Binary I/O
72^^^^^^^^^^
73
Martin Panter6bb91f32016-05-28 00:41:57 +000074Binary I/O (also called *buffered I/O*) expects
75:term:`bytes-like objects <bytes-like object>` and produces :class:`bytes`
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000076objects. No encoding, decoding, or newline translation is performed. This
77category of streams can be used for all kinds of non-text data, and also when
78manual control over the handling of text data is desired.
Antoine Pitroub530e142010-08-30 12:41:00 +000079
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000080The easiest way to create a binary stream is with :meth:`open()` with ``'b'`` in
81the mode string::
Antoine Pitroub530e142010-08-30 12:41:00 +000082
83 f = open("myfile.jpg", "rb")
84
85In-memory binary streams are also available as :class:`BytesIO` objects::
86
87 f = io.BytesIO(b"some initial binary data: \x00\x01")
88
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000089The binary stream API is described in detail in the docs of
90:class:`BufferedIOBase`.
Antoine Pitroub530e142010-08-30 12:41:00 +000091
92Other library modules may provide additional ways to create text or binary
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000093streams. See :meth:`socket.socket.makefile` for example.
94
Antoine Pitroub530e142010-08-30 12:41:00 +000095
96Raw I/O
97^^^^^^^
98
99Raw I/O (also called *unbuffered I/O*) is generally used as a low-level
100building-block for binary and text streams; it is rarely useful to directly
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000101manipulate a raw stream from user code. Nevertheless, you can create a raw
102stream by opening a file in binary mode with buffering disabled::
Antoine Pitroub530e142010-08-30 12:41:00 +0000103
104 f = open("myfile.jpg", "rb", buffering=0)
105
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000106The raw stream API is described in detail in the docs of :class:`RawIOBase`.
Benjamin Petersoncc12e1b2010-02-19 00:58:13 +0000107
Georg Brandl014197c2008-04-09 18:40:51 +0000108
Inada Naoki48274832021-03-29 12:28:14 +0900109.. _io-text-encoding:
110
111Text Encoding
112-------------
113
114The default encoding of :class:`TextIOWrapper` and :func:`open` is
115locale-specific (:func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`).
116
117However, many developers forget to specify the encoding when opening text files
118encoded in UTF-8 (e.g. JSON, TOML, Markdown, etc...) since most Unix
119platforms use UTF-8 locale by default. This causes bugs because the locale
120encoding is not UTF-8 for most Windows users. For example::
121
122 # May not work on Windows when non-ASCII characters in the file.
123 with open("README.md") as f:
124 long_description = f.read()
125
126Additionally, while there is no concrete plan as of yet, Python may change
127the default text file encoding to UTF-8 in the future.
128
129Accordingly, it is highly recommended that you specify the encoding
130explicitly when opening text files. If you want to use UTF-8, pass
131``encoding="utf-8"``. To use the current locale encoding,
132``encoding="locale"`` is supported in Python 3.10.
133
Miss Islington (bot)a5508202021-12-26 03:52:46 -0800134When you need to run existing code on Windows that attempts to open
Inada Naoki48274832021-03-29 12:28:14 +0900135UTF-8 files using the default locale encoding, you can enable the UTF-8
136mode. See :ref:`UTF-8 mode on Windows <win-utf8-mode>`.
137
138.. _io-encoding-warning:
139
140Opt-in EncodingWarning
141^^^^^^^^^^^^^^^^^^^^^^
142
143.. versionadded:: 3.10
144 See :pep:`597` for more details.
145
146To find where the default locale encoding is used, you can enable
147the ``-X warn_default_encoding`` command line option or set the
148:envvar:`PYTHONWARNDEFAULTENCODING` environment variable, which will
149emit an :exc:`EncodingWarning` when the default encoding is used.
150
151If you are providing an API that uses :func:`open` or
152:class:`TextIOWrapper` and passes ``encoding=None`` as a parameter, you
153can use :func:`text_encoding` so that callers of the API will emit an
154:exc:`EncodingWarning` if they don't pass an ``encoding``. However,
155please consider using UTF-8 by default (i.e. ``encoding="utf-8"``) for
156new APIs.
157
158
Antoine Pitroub530e142010-08-30 12:41:00 +0000159High-level Module Interface
160---------------------------
Georg Brandl014197c2008-04-09 18:40:51 +0000161
162.. data:: DEFAULT_BUFFER_SIZE
163
164 An int containing the default buffer size used by the module's buffered I/O
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000165 classes. :func:`open` uses the file's blksize (as obtained by
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000166 :func:`os.stat`) if possible.
Georg Brandl014197c2008-04-09 18:40:51 +0000167
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000168
Andrew Svetlova60de4f2013-02-17 16:55:58 +0200169.. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Georg Brandl014197c2008-04-09 18:40:51 +0000170
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000171 This is an alias for the builtin :func:`open` function.
Georg Brandl014197c2008-04-09 18:40:51 +0000172
Steve Dower44f91c32019-06-27 10:47:59 -0700173 .. audit-event:: open path,mode,flags io.open
Steve Dowerb82e17e2019-05-23 08:45:22 -0700174
Steve Dower60419a72019-06-24 08:42:54 -0700175 This function raises an :ref:`auditing event <auditing>` ``open`` with
Steve Dowerb82e17e2019-05-23 08:45:22 -0700176 arguments ``path``, ``mode`` and ``flags``. The ``mode`` and ``flags``
177 arguments may have been modified or inferred from the original call.
178
179
180.. function:: open_code(path)
181
182 Opens the provided file with mode ``'rb'``. This function should be used
183 when the intent is to treat the contents as executable code.
184
Shantanu831d58d2020-05-01 10:52:10 -0700185 ``path`` should be a :class:`str` and an absolute path.
Steve Dowerb82e17e2019-05-23 08:45:22 -0700186
187 The behavior of this function may be overridden by an earlier call to the
Shantanu831d58d2020-05-01 10:52:10 -0700188 :c:func:`PyFile_SetOpenCodeHook`. However, assuming that ``path`` is a
189 :class:`str` and an absolute path, ``open_code(path)`` should always behave
190 the same as ``open(path, 'rb')``. Overriding the behavior is intended for
191 additional validation or preprocessing of the file.
Steve Dowerb82e17e2019-05-23 08:45:22 -0700192
193 .. versionadded:: 3.8
194
Georg Brandl014197c2008-04-09 18:40:51 +0000195
Inada Naoki48274832021-03-29 12:28:14 +0900196.. function:: text_encoding(encoding, stacklevel=2)
197
198 This is a helper function for callables that use :func:`open` or
199 :class:`TextIOWrapper` and have an ``encoding=None`` parameter.
200
201 This function returns *encoding* if it is not ``None`` and ``"locale"`` if
202 *encoding* is ``None``.
203
204 This function emits an :class:`EncodingWarning` if
205 :data:`sys.flags.warn_default_encoding <sys.flags>` is true and *encoding*
206 is None. *stacklevel* specifies where the warning is emitted.
207 For example::
208
209 def read_text(path, encoding=None):
210 encoding = io.text_encoding(encoding) # stacklevel=2
211 with open(path, encoding) as f:
212 return f.read()
213
214 In this example, an :class:`EncodingWarning` is emitted for the caller of
215 ``read_text()``.
216
217 See :ref:`io-text-encoding` for more information.
218
219 .. versionadded:: 3.10
220
221
Georg Brandl014197c2008-04-09 18:40:51 +0000222.. exception:: BlockingIOError
223
Antoine Pitrouf55011f2011-10-12 18:57:23 +0200224 This is a compatibility alias for the builtin :exc:`BlockingIOError`
225 exception.
Georg Brandl014197c2008-04-09 18:40:51 +0000226
227
228.. exception:: UnsupportedOperation
229
Antoine Pitroua787b652011-10-12 19:02:52 +0200230 An exception inheriting :exc:`OSError` and :exc:`ValueError` that is raised
Georg Brandl014197c2008-04-09 18:40:51 +0000231 when an unsupported operation is called on a stream.
232
233
Antoine Pitroub530e142010-08-30 12:41:00 +0000234.. seealso::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000235
Antoine Pitroub530e142010-08-30 12:41:00 +0000236 :mod:`sys`
237 contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`,
238 and :data:`sys.stderr`.
239
240
241Class hierarchy
242---------------
243
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000244The implementation of I/O streams is organized as a hierarchy of classes. First
245:term:`abstract base classes <abstract base class>` (ABCs), which are used to
246specify the various categories of streams, then concrete classes providing the
247standard stream implementations.
Antoine Pitroub530e142010-08-30 12:41:00 +0000248
249 .. note::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000250
251 The abstract base classes also provide default implementations of some
252 methods in order to help implementation of concrete stream classes. For
253 example, :class:`BufferedIOBase` provides unoptimized implementations of
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300254 :meth:`~IOBase.readinto` and :meth:`~IOBase.readline`.
Antoine Pitroub530e142010-08-30 12:41:00 +0000255
256At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
257defines the basic interface to a stream. Note, however, that there is no
258separation between reading and writing to streams; implementations are allowed
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000259to raise :exc:`UnsupportedOperation` if they do not support a given operation.
Antoine Pitroub530e142010-08-30 12:41:00 +0000260
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000261The :class:`RawIOBase` ABC extends :class:`IOBase`. It deals with the reading
262and writing of bytes to a stream. :class:`FileIO` subclasses :class:`RawIOBase`
263to provide an interface to files in the machine's file system.
Antoine Pitroub530e142010-08-30 12:41:00 +0000264
Géry Ogam3b58a702019-09-11 16:55:13 +0200265The :class:`BufferedIOBase` ABC extends :class:`IOBase`. It deals with
266buffering on a raw binary stream (:class:`RawIOBase`). Its subclasses,
267:class:`BufferedWriter`, :class:`BufferedReader`, and :class:`BufferedRWPair`
268buffer raw binary streams that are readable, writable, and both readable and writable,
269respectively. :class:`BufferedRandom` provides a buffered interface to seekable streams.
270Another :class:`BufferedIOBase` subclass, :class:`BytesIO`, is a stream of
271in-memory bytes.
Antoine Pitroub530e142010-08-30 12:41:00 +0000272
Géry Ogam3b58a702019-09-11 16:55:13 +0200273The :class:`TextIOBase` ABC extends :class:`IOBase`. It deals with
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000274streams whose bytes represent text, and handles encoding and decoding to and
Géry Ogam3b58a702019-09-11 16:55:13 +0200275from strings. :class:`TextIOWrapper`, which extends :class:`TextIOBase`, is a buffered text
276interface to a buffered raw stream (:class:`BufferedIOBase`). Finally,
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000277:class:`StringIO` is an in-memory stream for text.
Antoine Pitroub530e142010-08-30 12:41:00 +0000278
279Argument names are not part of the specification, and only the arguments of
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000280:func:`open` are intended to be used as keyword arguments.
Antoine Pitroub530e142010-08-30 12:41:00 +0000281
Andrew Svetloved636a82012-12-06 12:20:56 +0200282The following table summarizes the ABCs provided by the :mod:`io` module:
283
Georg Brandl44ea77b2013-03-28 13:28:44 +0100284.. tabularcolumns:: |l|l|L|L|
285
Andrew Svetloved636a82012-12-06 12:20:56 +0200286========================= ================== ======================== ==================================================
287ABC Inherits Stub Methods Mixin Methods and Properties
288========================= ================== ======================== ==================================================
289:class:`IOBase` ``fileno``, ``seek``, ``close``, ``closed``, ``__enter__``,
290 and ``truncate`` ``__exit__``, ``flush``, ``isatty``, ``__iter__``,
291 ``__next__``, ``readable``, ``readline``,
292 ``readlines``, ``seekable``, ``tell``,
293 ``writable``, and ``writelines``
294:class:`RawIOBase` :class:`IOBase` ``readinto`` and Inherited :class:`IOBase` methods, ``read``,
295 ``write`` and ``readall``
Sanyam Khurana1b74f9b2017-12-11 19:12:09 +0530296:class:`BufferedIOBase` :class:`IOBase` ``detach``, ``read``, Inherited :class:`IOBase` methods, ``readinto``,
297 ``read1``, and ``write`` and ``readinto1``
Andrew Svetloved636a82012-12-06 12:20:56 +0200298:class:`TextIOBase` :class:`IOBase` ``detach``, ``read``, Inherited :class:`IOBase` methods, ``encoding``,
299 ``readline``, and ``errors``, and ``newlines``
300 ``write``
301========================= ================== ======================== ==================================================
302
Antoine Pitroub530e142010-08-30 12:41:00 +0000303
Georg Brandl014197c2008-04-09 18:40:51 +0000304I/O Base Classes
Antoine Pitroub530e142010-08-30 12:41:00 +0000305^^^^^^^^^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000306
307.. class:: IOBase
308
309 The abstract base class for all I/O classes, acting on streams of bytes.
310 There is no public constructor.
311
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000312 This class provides empty abstract implementations for many methods
313 that derived classes can override selectively; the default
314 implementations represent a file that cannot be read, written or
315 seeked.
Georg Brandl014197c2008-04-09 18:40:51 +0000316
Steve Palmer7b97ab32019-04-09 05:35:27 +0100317 Even though :class:`IOBase` does not declare :meth:`read`
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000318 or :meth:`write` because their signatures will vary, implementations and
319 clients should consider those methods part of the interface. Also,
Antoine Pitroua787b652011-10-12 19:02:52 +0200320 implementations may raise a :exc:`ValueError` (or :exc:`UnsupportedOperation`)
321 when operations they do not support are called.
Georg Brandl014197c2008-04-09 18:40:51 +0000322
323 The basic type used for binary data read from or written to a file is
Martin Panter6bb91f32016-05-28 00:41:57 +0000324 :class:`bytes`. Other :term:`bytes-like objects <bytes-like object>` are
Steve Palmer7b97ab32019-04-09 05:35:27 +0100325 accepted as method arguments too. Text I/O classes work with :class:`str` data.
Georg Brandl014197c2008-04-09 18:40:51 +0000326
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000327 Note that calling any method (even inquiries) on a closed stream is
Antoine Pitroua787b652011-10-12 19:02:52 +0200328 undefined. Implementations may raise :exc:`ValueError` in this case.
Georg Brandl014197c2008-04-09 18:40:51 +0000329
Éric Araujo3f7c0e42012-12-08 22:53:43 -0500330 :class:`IOBase` (and its subclasses) supports the iterator protocol, meaning
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300331 that an :class:`IOBase` object can be iterated over yielding the lines in a
332 stream. Lines are defined slightly differently depending on whether the
333 stream is a binary stream (yielding bytes), or a text stream (yielding
334 character strings). See :meth:`~IOBase.readline` below.
Georg Brandl014197c2008-04-09 18:40:51 +0000335
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300336 :class:`IOBase` is also a context manager and therefore supports the
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000337 :keyword:`with` statement. In this example, *file* is closed after the
Serhiy Storchaka2b57c432018-12-19 08:09:46 +0200338 :keyword:`!with` statement's suite is finished---even if an exception occurs::
Georg Brandl014197c2008-04-09 18:40:51 +0000339
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000340 with open('spam.txt', 'w') as file:
341 file.write('Spam and eggs!')
Georg Brandl014197c2008-04-09 18:40:51 +0000342
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000343 :class:`IOBase` provides these data attributes and methods:
Georg Brandl014197c2008-04-09 18:40:51 +0000344
345 .. method:: close()
346
Christian Heimesecc42a22008-11-05 19:30:32 +0000347 Flush and close this stream. This method has no effect if the file is
Georg Brandl48310cd2009-01-03 21:18:54 +0000348 already closed. Once the file is closed, any operation on the file
Georg Brandl8569e582010-05-19 20:57:08 +0000349 (e.g. reading or writing) will raise a :exc:`ValueError`.
Antoine Pitrouf9fc08f2010-04-28 19:59:32 +0000350
351 As a convenience, it is allowed to call this method more than once;
352 only the first call, however, will have an effect.
Georg Brandl014197c2008-04-09 18:40:51 +0000353
354 .. attribute:: closed
355
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300356 ``True`` if the stream is closed.
Georg Brandl014197c2008-04-09 18:40:51 +0000357
358 .. method:: fileno()
359
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000360 Return the underlying file descriptor (an integer) of the stream if it
Antoine Pitroua787b652011-10-12 19:02:52 +0200361 exists. An :exc:`OSError` is raised if the IO object does not use a file
Georg Brandl014197c2008-04-09 18:40:51 +0000362 descriptor.
363
364 .. method:: flush()
365
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000366 Flush the write buffers of the stream if applicable. This does nothing
367 for read-only and non-blocking streams.
Georg Brandl014197c2008-04-09 18:40:51 +0000368
369 .. method:: isatty()
370
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000371 Return ``True`` if the stream is interactive (i.e., connected to
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000372 a terminal/tty device).
Georg Brandl014197c2008-04-09 18:40:51 +0000373
374 .. method:: readable()
375
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200376 Return ``True`` if the stream can be read from. If ``False``, :meth:`read`
Antoine Pitroua787b652011-10-12 19:02:52 +0200377 will raise :exc:`OSError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000378
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300379 .. method:: readline(size=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000380
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300381 Read and return one line from the stream. If *size* is specified, at
382 most *size* bytes will be read.
Georg Brandl014197c2008-04-09 18:40:51 +0000383
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000384 The line terminator is always ``b'\n'`` for binary files; for text files,
Zachary Ware0069eac2014-07-18 09:11:48 -0500385 the *newline* argument to :func:`open` can be used to select the line
Georg Brandl014197c2008-04-09 18:40:51 +0000386 terminator(s) recognized.
387
Georg Brandl3dd33882009-06-01 17:35:27 +0000388 .. method:: readlines(hint=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000389
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000390 Read and return a list of lines from the stream. *hint* can be specified
391 to control the number of lines read: no more lines will be read if the
392 total size (in bytes/characters) of all lines so far exceeds *hint*.
Georg Brandl014197c2008-04-09 18:40:51 +0000393
Miss Islington (bot)9b369c92021-07-13 07:41:12 -0700394 *hint* values of ``0`` or less, as well as ``None``, are treated as no
395 hint.
396
Ezio Melottied3cd7e2013-04-15 19:08:31 +0300397 Note that it's already possible to iterate on file objects using ``for
398 line in file: ...`` without calling ``file.readlines()``.
399
Benjamin Peterson2a3d4d92019-07-10 19:43:04 -0700400 .. method:: seek(offset, whence=SEEK_SET)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000401
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000402 Change the stream position to the given byte *offset*. *offset* is
Martin Panterdb4220e2015-09-11 03:58:30 +0000403 interpreted relative to the position indicated by *whence*. The default
404 value for *whence* is :data:`SEEK_SET`. Values for *whence* are:
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000405
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000406 * :data:`SEEK_SET` or ``0`` -- start of the stream (the default);
407 *offset* should be zero or positive
408 * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may
409 be negative
410 * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually
411 negative
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000412
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000413 Return the new absolute position.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000414
Raymond Hettinger35a88362009-04-09 00:08:24 +0000415 .. versionadded:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000416 The ``SEEK_*`` constants.
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000417
Jesus Cea94363612012-06-22 18:32:07 +0200418 .. versionadded:: 3.3
419 Some operating systems could support additional values, like
420 :data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`. The valid values
421 for a file could depend on it being open in text or binary mode.
422
Georg Brandl014197c2008-04-09 18:40:51 +0000423 .. method:: seekable()
424
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000425 Return ``True`` if the stream supports random access. If ``False``,
Antoine Pitroua787b652011-10-12 19:02:52 +0200426 :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`OSError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000427
428 .. method:: tell()
429
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000430 Return the current stream position.
Georg Brandl014197c2008-04-09 18:40:51 +0000431
Georg Brandl3dd33882009-06-01 17:35:27 +0000432 .. method:: truncate(size=None)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000433
Antoine Pitrou2016dc92010-05-29 12:08:25 +0000434 Resize the stream to the given *size* in bytes (or the current position
435 if *size* is not specified). The current stream position isn't changed.
436 This resizing can extend or reduce the current file size. In case of
437 extension, the contents of the new file area depend on the platform
Steve Dowerfe0a41a2015-03-20 19:50:46 -0700438 (on most systems, additional bytes are zero-filled). The new file size
439 is returned.
440
Emmanuel Arias522630a2019-02-15 16:02:38 -0300441 .. versionchanged:: 3.5
442 Windows will now zero-fill files when extending.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000443
Georg Brandl014197c2008-04-09 18:40:51 +0000444 .. method:: writable()
445
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000446 Return ``True`` if the stream supports writing. If ``False``,
Antoine Pitroua787b652011-10-12 19:02:52 +0200447 :meth:`write` and :meth:`truncate` will raise :exc:`OSError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000448
449 .. method:: writelines(lines)
450
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000451 Write a list of lines to the stream. Line separators are not added, so it
452 is usual for each of the lines provided to have a line separator at the
453 end.
Georg Brandl014197c2008-04-09 18:40:51 +0000454
Benjamin Petersonef8abfc2014-06-14 18:51:34 -0700455 .. method:: __del__()
456
457 Prepare for object destruction. :class:`IOBase` provides a default
458 implementation of this method that calls the instance's
459 :meth:`~IOBase.close` method.
460
Georg Brandl014197c2008-04-09 18:40:51 +0000461
462.. class:: RawIOBase
463
Géry Ogam3b58a702019-09-11 16:55:13 +0200464 Base class for raw binary streams. It inherits :class:`IOBase`. There is no
Georg Brandl014197c2008-04-09 18:40:51 +0000465 public constructor.
466
Géry Ogam3b58a702019-09-11 16:55:13 +0200467 Raw binary streams typically provide low-level access to an underlying OS
468 device or API, and do not try to encapsulate it in high-level primitives
469 (this functionality is done at a higher-level in buffered binary streams and text streams, described later
470 in this page).
Antoine Pitrou497a7672009-09-17 17:18:01 +0000471
Géry Ogam3b58a702019-09-11 16:55:13 +0200472 :class:`RawIOBase` provides these methods in addition to those from
473 :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000474
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300475 .. method:: read(size=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000476
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300477 Read up to *size* bytes from the object and return them. As a convenience,
Sanyam Khurana1b74f9b2017-12-11 19:12:09 +0530478 if *size* is unspecified or -1, all bytes until EOF are returned.
479 Otherwise, only one system call is ever made. Fewer than *size* bytes may
480 be returned if the operating system call returns fewer than *size* bytes.
Antoine Pitrou78ddbe62009-10-01 16:24:45 +0000481
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300482 If 0 bytes are returned, and *size* was not 0, this indicates end of file.
Antoine Pitrou78ddbe62009-10-01 16:24:45 +0000483 If the object is in non-blocking mode and no bytes are available,
484 ``None`` is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000485
Sanyam Khurana1b74f9b2017-12-11 19:12:09 +0530486 The default implementation defers to :meth:`readall` and
487 :meth:`readinto`.
488
Benjamin Petersonb47aace2008-04-09 21:38:38 +0000489 .. method:: readall()
Georg Brandl014197c2008-04-09 18:40:51 +0000490
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000491 Read and return all the bytes from the stream until EOF, using multiple
492 calls to the stream if necessary.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000493
494 .. method:: readinto(b)
495
Martin Panter6bb91f32016-05-28 00:41:57 +0000496 Read bytes into a pre-allocated, writable
497 :term:`bytes-like object` *b*, and return the
Steve Palmer7b97ab32019-04-09 05:35:27 +0100498 number of bytes read. For example, *b* might be a :class:`bytearray`.
499 If the object is in non-blocking mode and no bytes
Benjamin Peterson2a1a4902014-06-22 14:19:07 -0700500 are available, ``None`` is returned.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000501
502 .. method:: write(b)
503
Martin Panter6bb91f32016-05-28 00:41:57 +0000504 Write the given :term:`bytes-like object`, *b*, to the
505 underlying raw stream, and return the number of
506 bytes written. This can be less than the length of *b* in
507 bytes, depending on specifics of the underlying raw
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300508 stream, and especially if it is in non-blocking mode. ``None`` is
509 returned if the raw stream is set not to block and no single byte could
Martin Panter6bb91f32016-05-28 00:41:57 +0000510 be readily written to it. The caller may release or mutate *b* after
511 this method returns, so the implementation should only access *b*
512 during the method call.
Georg Brandl014197c2008-04-09 18:40:51 +0000513
514
Georg Brandl014197c2008-04-09 18:40:51 +0000515.. class:: BufferedIOBase
516
Antoine Pitrou497a7672009-09-17 17:18:01 +0000517 Base class for binary streams that support some kind of buffering.
518 It inherits :class:`IOBase`. There is no public constructor.
Georg Brandl014197c2008-04-09 18:40:51 +0000519
Antoine Pitrou497a7672009-09-17 17:18:01 +0000520 The main difference with :class:`RawIOBase` is that methods :meth:`read`,
521 :meth:`readinto` and :meth:`write` will try (respectively) to read as much
522 input as requested or to consume all given output, at the expense of
523 making perhaps more than one system call.
524
525 In addition, those methods can raise :exc:`BlockingIOError` if the
526 underlying raw stream is in non-blocking mode and cannot take or give
527 enough data; unlike their :class:`RawIOBase` counterparts, they will
528 never return ``None``.
529
530 Besides, the :meth:`read` method does not have a default
Georg Brandl014197c2008-04-09 18:40:51 +0000531 implementation that defers to :meth:`readinto`.
532
Antoine Pitrou497a7672009-09-17 17:18:01 +0000533 A typical :class:`BufferedIOBase` implementation should not inherit from a
534 :class:`RawIOBase` implementation, but wrap one, like
535 :class:`BufferedWriter` and :class:`BufferedReader` do.
Georg Brandl014197c2008-04-09 18:40:51 +0000536
Géry Ogam3b58a702019-09-11 16:55:13 +0200537 :class:`BufferedIOBase` provides or overrides these data attributes and
538 methods in addition to those from :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000539
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000540 .. attribute:: raw
541
542 The underlying raw stream (a :class:`RawIOBase` instance) that
543 :class:`BufferedIOBase` deals with. This is not part of the
544 :class:`BufferedIOBase` API and may not exist on some implementations.
545
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000546 .. method:: detach()
547
548 Separate the underlying raw stream from the buffer and return it.
549
550 After the raw stream has been detached, the buffer is in an unusable
551 state.
552
553 Some buffers, like :class:`BytesIO`, do not have the concept of a single
554 raw stream to return from this method. They raise
555 :exc:`UnsupportedOperation`.
556
Benjamin Petersonedc36472009-05-01 20:48:14 +0000557 .. versionadded:: 3.1
558
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300559 .. method:: read(size=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000560
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300561 Read and return up to *size* bytes. If the argument is omitted, ``None``,
562 or negative, data is read and returned until EOF is reached. An empty
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300563 :class:`bytes` object is returned if the stream is already at EOF.
Georg Brandl014197c2008-04-09 18:40:51 +0000564
565 If the argument is positive, and the underlying raw stream is not
566 interactive, multiple raw reads may be issued to satisfy the byte count
567 (unless EOF is reached first). But for interactive raw streams, at most
568 one raw read will be issued, and a short result does not imply that EOF is
569 imminent.
570
Antoine Pitrou497a7672009-09-17 17:18:01 +0000571 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
572 non blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000573
Martin Panterccb2c0e2016-10-20 23:48:14 +0000574 .. method:: read1([size])
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000575
Benjamin Petersona96fea02014-06-22 14:17:44 -0700576 Read and return up to *size* bytes, with at most one call to the
577 underlying raw stream's :meth:`~RawIOBase.read` (or
Benjamin Peterson2a1a4902014-06-22 14:19:07 -0700578 :meth:`~RawIOBase.readinto`) method. This can be useful if you are
579 implementing your own buffering on top of a :class:`BufferedIOBase`
580 object.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000581
Martin Panter4e946792016-10-21 23:00:10 +0000582 If *size* is ``-1`` (the default), an arbitrary number of bytes are
Martin Panterccb2c0e2016-10-20 23:48:14 +0000583 returned (more than zero unless EOF is reached).
584
Georg Brandl014197c2008-04-09 18:40:51 +0000585 .. method:: readinto(b)
586
Martin Panter6bb91f32016-05-28 00:41:57 +0000587 Read bytes into a pre-allocated, writable
588 :term:`bytes-like object` *b* and return the number of bytes read.
Steve Palmer7b97ab32019-04-09 05:35:27 +0100589 For example, *b* might be a :class:`bytearray`.
Georg Brandl014197c2008-04-09 18:40:51 +0000590
591 Like :meth:`read`, multiple reads may be issued to the underlying raw
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300592 stream, unless the latter is interactive.
Georg Brandl014197c2008-04-09 18:40:51 +0000593
Benjamin Peterson2a1a4902014-06-22 14:19:07 -0700594 A :exc:`BlockingIOError` is raised if the underlying raw stream is in non
595 blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000596
Benjamin Petersona96fea02014-06-22 14:17:44 -0700597 .. method:: readinto1(b)
598
Martin Panter6bb91f32016-05-28 00:41:57 +0000599 Read bytes into a pre-allocated, writable
600 :term:`bytes-like object` *b*, using at most one call to
Benjamin Peterson2a1a4902014-06-22 14:19:07 -0700601 the underlying raw stream's :meth:`~RawIOBase.read` (or
602 :meth:`~RawIOBase.readinto`) method. Return the number of bytes read.
Benjamin Petersona96fea02014-06-22 14:17:44 -0700603
Benjamin Peterson2a1a4902014-06-22 14:19:07 -0700604 A :exc:`BlockingIOError` is raised if the underlying raw stream is in non
605 blocking-mode, and has no data available at the moment.
Benjamin Petersona96fea02014-06-22 14:17:44 -0700606
607 .. versionadded:: 3.5
608
Georg Brandl014197c2008-04-09 18:40:51 +0000609 .. method:: write(b)
610
Martin Panter6bb91f32016-05-28 00:41:57 +0000611 Write the given :term:`bytes-like object`, *b*, and return the number
612 of bytes written (always equal to the length of *b* in bytes, since if
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300613 the write fails an :exc:`OSError` will be raised). Depending on the
614 actual implementation, these bytes may be readily written to the
615 underlying stream, or held in a buffer for performance and latency
616 reasons.
Georg Brandl014197c2008-04-09 18:40:51 +0000617
Antoine Pitrou497a7672009-09-17 17:18:01 +0000618 When in non-blocking mode, a :exc:`BlockingIOError` is raised if the
619 data needed to be written to the raw stream but it couldn't accept
620 all the data without blocking.
Georg Brandl014197c2008-04-09 18:40:51 +0000621
Martin Panter6bb91f32016-05-28 00:41:57 +0000622 The caller may release or mutate *b* after this method returns,
623 so the implementation should only access *b* during the method call.
624
Georg Brandl014197c2008-04-09 18:40:51 +0000625
Benjamin Petersonaa069002009-01-23 03:26:36 +0000626Raw File I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000627^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000628
Ross Lagerwall59142db2011-10-31 20:34:46 +0200629.. class:: FileIO(name, mode='r', closefd=True, opener=None)
Benjamin Petersonaa069002009-01-23 03:26:36 +0000630
Géry Ogam3b58a702019-09-11 16:55:13 +0200631 A raw binary stream representing an OS-level file containing bytes data. It
632 inherits :class:`RawIOBase`.
Antoine Pitrou497a7672009-09-17 17:18:01 +0000633
634 The *name* can be one of two things:
635
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300636 * a character string or :class:`bytes` object representing the path to the
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300637 file which will be opened. In this case closefd must be ``True`` (the default)
Robert Collins933430a2014-10-18 13:32:43 +1300638 otherwise an error will be raised.
Antoine Pitrou497a7672009-09-17 17:18:01 +0000639 * an integer representing the number of an existing OS-level file descriptor
Robert Collins933430a2014-10-18 13:32:43 +1300640 to which the resulting :class:`FileIO` object will give access. When the
641 FileIO object is closed this fd will be closed as well, unless *closefd*
642 is set to ``False``.
Benjamin Petersonaa069002009-01-23 03:26:36 +0000643
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100644 The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading
Charles-François Natalid612de12012-01-14 11:51:00 +0100645 (default), writing, exclusive creation or appending. The file will be
646 created if it doesn't exist when opened for writing or appending; it will be
647 truncated when opened for writing. :exc:`FileExistsError` will be raised if
648 it already exists when opened for creating. Opening a file for creating
649 implies writing, so this mode behaves in a similar way to ``'w'``. Add a
650 ``'+'`` to the mode to allow simultaneous reading and writing.
Benjamin Petersonaa069002009-01-23 03:26:36 +0000651
Antoine Pitrou497a7672009-09-17 17:18:01 +0000652 The :meth:`read` (when called with a positive argument), :meth:`readinto`
653 and :meth:`write` methods on this class will only make one system call.
654
Ross Lagerwall59142db2011-10-31 20:34:46 +0200655 A custom opener can be used by passing a callable as *opener*. The underlying
656 file descriptor for the file object is then obtained by calling *opener* with
657 (*name*, *flags*). *opener* must return an open file descriptor (passing
658 :mod:`os.open` as *opener* results in functionality similar to passing
659 ``None``).
660
Victor Stinnerdaf45552013-08-28 00:53:59 +0200661 The newly created file is :ref:`non-inheritable <fd_inheritance>`.
662
Éric Araujo8f423c92012-11-03 17:06:52 -0400663 See the :func:`open` built-in function for examples on using the *opener*
664 parameter.
665
Ross Lagerwall59142db2011-10-31 20:34:46 +0200666 .. versionchanged:: 3.3
667 The *opener* parameter was added.
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100668 The ``'x'`` mode was added.
Ross Lagerwall59142db2011-10-31 20:34:46 +0200669
Victor Stinnerdaf45552013-08-28 00:53:59 +0200670 .. versionchanged:: 3.4
671 The file is now non-inheritable.
672
Géry Ogam3b58a702019-09-11 16:55:13 +0200673 :class:`FileIO` provides these data attributes in addition to those from
674 :class:`RawIOBase` and :class:`IOBase`:
Benjamin Petersonaa069002009-01-23 03:26:36 +0000675
676 .. attribute:: mode
677
678 The mode as given in the constructor.
679
680 .. attribute:: name
681
682 The file name. This is the file descriptor of the file when no name is
683 given in the constructor.
684
Benjamin Petersonaa069002009-01-23 03:26:36 +0000685
686Buffered Streams
Antoine Pitroub530e142010-08-30 12:41:00 +0000687^^^^^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000688
Antoine Pitroubed81c82010-12-03 19:14:17 +0000689Buffered I/O streams provide a higher-level interface to an I/O device
690than raw I/O does.
Antoine Pitrou497a7672009-09-17 17:18:01 +0000691
Georg Brandl014197c2008-04-09 18:40:51 +0000692.. class:: BytesIO([initial_bytes])
693
Géry Ogam3b58a702019-09-11 16:55:13 +0200694 A binary stream using an in-memory bytes buffer. It inherits
Serhiy Storchakac057c382015-02-03 02:00:18 +0200695 :class:`BufferedIOBase`. The buffer is discarded when the
696 :meth:`~IOBase.close` method is called.
Georg Brandl014197c2008-04-09 18:40:51 +0000697
Martin Panter6bb91f32016-05-28 00:41:57 +0000698 The optional argument *initial_bytes* is a :term:`bytes-like object` that
699 contains initial data.
Georg Brandl014197c2008-04-09 18:40:51 +0000700
701 :class:`BytesIO` provides or overrides these methods in addition to those
702 from :class:`BufferedIOBase` and :class:`IOBase`:
703
Antoine Pitrou972ee132010-09-06 18:48:21 +0000704 .. method:: getbuffer()
705
706 Return a readable and writable view over the contents of the buffer
707 without copying them. Also, mutating the view will transparently
708 update the contents of the buffer::
709
710 >>> b = io.BytesIO(b"abcdef")
711 >>> view = b.getbuffer()
712 >>> view[2:4] = b"56"
713 >>> b.getvalue()
714 b'ab56ef'
715
716 .. note::
717 As long as the view exists, the :class:`BytesIO` object cannot be
Serhiy Storchakac057c382015-02-03 02:00:18 +0200718 resized or closed.
Antoine Pitrou972ee132010-09-06 18:48:21 +0000719
720 .. versionadded:: 3.2
721
Georg Brandl014197c2008-04-09 18:40:51 +0000722 .. method:: getvalue()
723
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300724 Return :class:`bytes` containing the entire contents of the buffer.
Georg Brandl014197c2008-04-09 18:40:51 +0000725
Serhiy Storchakac057c382015-02-03 02:00:18 +0200726
Martin Panterccb2c0e2016-10-20 23:48:14 +0000727 .. method:: read1([size])
Georg Brandl014197c2008-04-09 18:40:51 +0000728
Martin Panterccb2c0e2016-10-20 23:48:14 +0000729 In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.read`.
Georg Brandl014197c2008-04-09 18:40:51 +0000730
Martin Panterccb2c0e2016-10-20 23:48:14 +0000731 .. versionchanged:: 3.7
732 The *size* argument is now optional.
Benjamin Petersona96fea02014-06-22 14:17:44 -0700733
Martin Panterccb2c0e2016-10-20 23:48:14 +0000734 .. method:: readinto1(b)
735
736 In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.readinto`.
Benjamin Petersona96fea02014-06-22 14:17:44 -0700737
738 .. versionadded:: 3.5
Georg Brandl014197c2008-04-09 18:40:51 +0000739
Georg Brandl3dd33882009-06-01 17:35:27 +0000740.. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000741
Géry Ogam3b58a702019-09-11 16:55:13 +0200742 A buffered binary stream providing higher-level access to a readable, non
743 seekable :class:`RawIOBase` raw binary stream. It inherits
744 :class:`BufferedIOBase`.
745
Antoine Pitrou497a7672009-09-17 17:18:01 +0000746 When reading data from this object, a larger amount of data may be
747 requested from the underlying raw stream, and kept in an internal buffer.
748 The buffered data can then be returned directly on subsequent reads.
Georg Brandl014197c2008-04-09 18:40:51 +0000749
750 The constructor creates a :class:`BufferedReader` for the given readable
751 *raw* stream and *buffer_size*. If *buffer_size* is omitted,
752 :data:`DEFAULT_BUFFER_SIZE` is used.
753
754 :class:`BufferedReader` provides or overrides these methods in addition to
755 those from :class:`BufferedIOBase` and :class:`IOBase`:
756
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300757 .. method:: peek([size])
Georg Brandl014197c2008-04-09 18:40:51 +0000758
Benjamin Petersonc43a26d2009-06-16 23:09:24 +0000759 Return bytes from the stream without advancing the position. At most one
Benjamin Peterson2a8b54d2009-06-14 14:37:23 +0000760 single read on the raw stream is done to satisfy the call. The number of
761 bytes returned may be less or more than requested.
Georg Brandl014197c2008-04-09 18:40:51 +0000762
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300763 .. method:: read([size])
Georg Brandl014197c2008-04-09 18:40:51 +0000764
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300765 Read and return *size* bytes, or if *size* is not given or negative, until
766 EOF or if the read call would block in non-blocking mode.
Georg Brandl014197c2008-04-09 18:40:51 +0000767
Martin Panterccb2c0e2016-10-20 23:48:14 +0000768 .. method:: read1([size])
Georg Brandl014197c2008-04-09 18:40:51 +0000769
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300770 Read and return up to *size* bytes with only one call on the raw stream.
771 If at least one byte is buffered, only buffered bytes are returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000772 Otherwise, one raw stream read call is made.
773
Martin Panterccb2c0e2016-10-20 23:48:14 +0000774 .. versionchanged:: 3.7
775 The *size* argument is now optional.
776
Georg Brandl014197c2008-04-09 18:40:51 +0000777
Georg Brandl3dd33882009-06-01 17:35:27 +0000778.. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000779
Géry Ogam3b58a702019-09-11 16:55:13 +0200780 A buffered binary stream providing higher-level access to a writeable, non
781 seekable :class:`RawIOBase` raw binary stream. It inherits
782 :class:`BufferedIOBase`.
783
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300784 When writing to this object, data is normally placed into an internal
Antoine Pitrou497a7672009-09-17 17:18:01 +0000785 buffer. The buffer will be written out to the underlying :class:`RawIOBase`
786 object under various conditions, including:
787
788 * when the buffer gets too small for all pending data;
789 * when :meth:`flush()` is called;
790 * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects);
791 * when the :class:`BufferedWriter` object is closed or destroyed.
Georg Brandl014197c2008-04-09 18:40:51 +0000792
793 The constructor creates a :class:`BufferedWriter` for the given writeable
794 *raw* stream. If the *buffer_size* is not given, it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000795 :data:`DEFAULT_BUFFER_SIZE`.
796
Georg Brandl014197c2008-04-09 18:40:51 +0000797 :class:`BufferedWriter` provides or overrides these methods in addition to
798 those from :class:`BufferedIOBase` and :class:`IOBase`:
799
800 .. method:: flush()
801
802 Force bytes held in the buffer into the raw stream. A
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000803 :exc:`BlockingIOError` should be raised if the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000804
805 .. method:: write(b)
806
Martin Panter6bb91f32016-05-28 00:41:57 +0000807 Write the :term:`bytes-like object`, *b*, and return the
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300808 number of bytes written. When in non-blocking mode, a
809 :exc:`BlockingIOError` is raised if the buffer needs to be written out but
810 the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000811
812
Georg Brandl3dd33882009-06-01 17:35:27 +0000813.. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000814
Géry Ogam3b58a702019-09-11 16:55:13 +0200815 A buffered binary stream providing higher-level access to a seekable
816 :class:`RawIOBase` raw binary stream. It inherits :class:`BufferedReader`
817 and :class:`BufferedWriter`.
Georg Brandl014197c2008-04-09 18:40:51 +0000818
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000819 The constructor creates a reader and writer for a seekable raw stream, given
Georg Brandl014197c2008-04-09 18:40:51 +0000820 in the first argument. If the *buffer_size* is omitted it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000821 :data:`DEFAULT_BUFFER_SIZE`.
822
Georg Brandl014197c2008-04-09 18:40:51 +0000823 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
Christopher Headb13552c2019-04-12 08:50:41 -0700824 :class:`BufferedWriter` can do. In addition, :meth:`seek` and :meth:`tell`
825 are guaranteed to be implemented.
Georg Brandl014197c2008-04-09 18:40:51 +0000826
827
Antoine Pitrou13d28952011-08-20 19:48:43 +0200828.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE)
829
Géry Ogam3b58a702019-09-11 16:55:13 +0200830 A buffered binary stream providing higher-level access to two non seekable
831 :class:`RawIOBase` raw binary streams---one readable, the other writeable.
832 It inherits :class:`BufferedIOBase`.
Antoine Pitrou13d28952011-08-20 19:48:43 +0200833
834 *reader* and *writer* are :class:`RawIOBase` objects that are readable and
835 writeable respectively. If the *buffer_size* is omitted it defaults to
836 :data:`DEFAULT_BUFFER_SIZE`.
837
Antoine Pitrou13d28952011-08-20 19:48:43 +0200838 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods
839 except for :meth:`~BufferedIOBase.detach`, which raises
840 :exc:`UnsupportedOperation`.
841
842 .. warning::
Larry Hastings3732ed22014-03-15 21:13:56 -0700843
Antoine Pitrou13d28952011-08-20 19:48:43 +0200844 :class:`BufferedRWPair` does not attempt to synchronize accesses to
845 its underlying raw streams. You should not pass it the same object
846 as reader and writer; use :class:`BufferedRandom` instead.
847
848
Georg Brandl014197c2008-04-09 18:40:51 +0000849Text I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000850^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000851
852.. class:: TextIOBase
853
854 Base class for text streams. This class provides a character and line based
Géry Ogam3b58a702019-09-11 16:55:13 +0200855 interface to stream I/O. It inherits :class:`IOBase`. There is no public
856 constructor.
Georg Brandl014197c2008-04-09 18:40:51 +0000857
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000858 :class:`TextIOBase` provides or overrides these data attributes and
859 methods in addition to those from :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000860
861 .. attribute:: encoding
862
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000863 The name of the encoding used to decode the stream's bytes into
Georg Brandl014197c2008-04-09 18:40:51 +0000864 strings, and to encode strings into bytes.
865
Benjamin Peterson0926ad12009-06-06 18:02:12 +0000866 .. attribute:: errors
867
868 The error setting of the decoder or encoder.
869
Georg Brandl014197c2008-04-09 18:40:51 +0000870 .. attribute:: newlines
871
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000872 A string, a tuple of strings, or ``None``, indicating the newlines
Antoine Pitrou497a7672009-09-17 17:18:01 +0000873 translated so far. Depending on the implementation and the initial
874 constructor flags, this may not be available.
Georg Brandl014197c2008-04-09 18:40:51 +0000875
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000876 .. attribute:: buffer
877
878 The underlying binary buffer (a :class:`BufferedIOBase` instance) that
879 :class:`TextIOBase` deals with. This is not part of the
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300880 :class:`TextIOBase` API and may not exist in some implementations.
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000881
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000882 .. method:: detach()
883
Antoine Pitrou497a7672009-09-17 17:18:01 +0000884 Separate the underlying binary buffer from the :class:`TextIOBase` and
885 return it.
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000886
887 After the underlying buffer has been detached, the :class:`TextIOBase` is
888 in an unusable state.
889
890 Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not
891 have the concept of an underlying buffer and calling this method will
892 raise :exc:`UnsupportedOperation`.
893
Benjamin Petersonedc36472009-05-01 20:48:14 +0000894 .. versionadded:: 3.1
895
Andrés Delfinob6bb77c2018-07-07 17:17:16 -0300896 .. method:: read(size=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000897
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300898 Read and return at most *size* characters from the stream as a single
899 :class:`str`. If *size* is negative or ``None``, reads until EOF.
Georg Brandl014197c2008-04-09 18:40:51 +0000900
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300901 .. method:: readline(size=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000902
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000903 Read until newline or EOF and return a single ``str``. If the stream is
904 already at EOF, an empty string is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000905
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300906 If *size* is specified, at most *size* characters will be read.
Antoine Pitrou707bd4e2012-07-25 22:38:33 +0200907
Benjamin Peterson2a3d4d92019-07-10 19:43:04 -0700908 .. method:: seek(offset, whence=SEEK_SET)
Antoine Pitrouf49d1522012-01-21 20:20:49 +0100909
Martin Panterdb4220e2015-09-11 03:58:30 +0000910 Change the stream position to the given *offset*. Behaviour depends on
911 the *whence* parameter. The default value for *whence* is
912 :data:`SEEK_SET`.
Antoine Pitrouf49d1522012-01-21 20:20:49 +0100913
914 * :data:`SEEK_SET` or ``0``: seek from the start of the stream
915 (the default); *offset* must either be a number returned by
916 :meth:`TextIOBase.tell`, or zero. Any other *offset* value
917 produces undefined behaviour.
918 * :data:`SEEK_CUR` or ``1``: "seek" to the current position;
919 *offset* must be zero, which is a no-operation (all other values
920 are unsupported).
921 * :data:`SEEK_END` or ``2``: seek to the end of the stream;
922 *offset* must be zero (all other values are unsupported).
923
924 Return the new absolute position as an opaque number.
925
926 .. versionadded:: 3.1
927 The ``SEEK_*`` constants.
928
929 .. method:: tell()
930
931 Return the current stream position as an opaque number. The number
932 does not usually represent a number of bytes in the underlying
933 binary storage.
934
Georg Brandl014197c2008-04-09 18:40:51 +0000935 .. method:: write(s)
936
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000937 Write the string *s* to the stream and return the number of characters
938 written.
Georg Brandl014197c2008-04-09 18:40:51 +0000939
940
Antoine Pitrou664091b2011-07-23 22:00:03 +0200941.. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, \
942 line_buffering=False, write_through=False)
Georg Brandl014197c2008-04-09 18:40:51 +0000943
Géry Ogam3b58a702019-09-11 16:55:13 +0200944 A buffered text stream providing higher-level access to a
945 :class:`BufferedIOBase` buffered binary stream. It inherits
946 :class:`TextIOBase`.
Georg Brandl014197c2008-04-09 18:40:51 +0000947
948 *encoding* gives the name of the encoding that the stream will be decoded or
Andrew Svetlov4805fa82012-08-13 22:11:14 +0300949 encoded with. It defaults to
950 :func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`.
Inada Naoki48274832021-03-29 12:28:14 +0900951 ``encoding="locale"`` can be used to specify the current locale's encoding
952 explicitly. See :ref:`io-text-encoding` for more information.
Georg Brandl014197c2008-04-09 18:40:51 +0000953
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000954 *errors* is an optional string that specifies how encoding and decoding
955 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError`
956 exception if there is an encoding error (the default of ``None`` has the same
957 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding
958 errors can lead to data loss.) ``'replace'`` causes a replacement marker
Serhiy Storchaka07985ef2015-01-25 22:56:57 +0200959 (such as ``'?'``) to be inserted where there is malformed data.
960 ``'backslashreplace'`` causes malformed data to be replaced by a
961 backslashed escape sequence. When writing, ``'xmlcharrefreplace'``
962 (replace with the appropriate XML character reference) or ``'namereplace'``
963 (replace with ``\N{...}`` escape sequences) can be used. Any other error
964 handling name that has been registered with
Serhiy Storchaka166ebc42014-11-25 13:57:17 +0200965 :func:`codecs.register_error` is also valid.
Georg Brandl014197c2008-04-09 18:40:51 +0000966
R David Murray1b00f252012-08-15 10:43:58 -0400967 .. index::
968 single: universal newlines; io.TextIOWrapper class
969
Antoine Pitrou0c1c0d42012-08-04 00:55:38 +0200970 *newline* controls how line endings are handled. It can be ``None``,
971 ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows:
972
R David Murray1b00f252012-08-15 10:43:58 -0400973 * When reading input from the stream, if *newline* is ``None``,
R David Murrayee0a9452012-08-15 11:05:36 -0400974 :term:`universal newlines` mode is enabled. Lines in the input can end in
975 ``'\n'``, ``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'``
Géry Ogam3b58a702019-09-11 16:55:13 +0200976 before being returned to the caller. If *newline* is ``''``, universal
977 newlines mode is enabled, but line endings are returned to the caller
978 untranslated. If *newline* has any of the other legal values, input lines
979 are only terminated by the given string, and the line ending is returned to
980 the caller untranslated.
Antoine Pitrou0c1c0d42012-08-04 00:55:38 +0200981
Georg Brandl296d1be2012-08-14 09:39:07 +0200982 * When writing output to the stream, if *newline* is ``None``, any ``'\n'``
983 characters written are translated to the system default line separator,
984 :data:`os.linesep`. If *newline* is ``''`` or ``'\n'``, no translation
985 takes place. If *newline* is any of the other legal values, any ``'\n'``
986 characters written are translated to the given string.
Georg Brandl014197c2008-04-09 18:40:51 +0000987
988 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
Elena Oat7ffd4c52018-05-14 17:48:01 +0300989 write contains a newline character or a carriage return.
Georg Brandl014197c2008-04-09 18:40:51 +0000990
Antoine Pitrou664091b2011-07-23 22:00:03 +0200991 If *write_through* is ``True``, calls to :meth:`write` are guaranteed
992 not to be buffered: any data written on the :class:`TextIOWrapper`
993 object is immediately handled to its underlying binary *buffer*.
994
995 .. versionchanged:: 3.3
996 The *write_through* argument has been added.
997
Victor Stinnerf86a5e82012-06-05 13:43:22 +0200998 .. versionchanged:: 3.3
999 The default *encoding* is now ``locale.getpreferredencoding(False)``
1000 instead of ``locale.getpreferredencoding()``. Don't change temporary the
1001 locale encoding using :func:`locale.setlocale`, use the current locale
1002 encoding instead of the user preferred encoding.
1003
Inada Naoki48274832021-03-29 12:28:14 +09001004 .. versionchanged:: 3.10
1005 The *encoding* argument now supports the ``"locale"`` dummy encoding name.
1006
Géry Ogam3b58a702019-09-11 16:55:13 +02001007 :class:`TextIOWrapper` provides these data attributes and methods in
1008 addition to those from :class:`TextIOBase` and :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +00001009
Georg Brandl014197c2008-04-09 18:40:51 +00001010 .. attribute:: line_buffering
1011
1012 Whether line buffering is enabled.
Georg Brandl48310cd2009-01-03 21:18:54 +00001013
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02001014 .. attribute:: write_through
1015
1016 Whether writes are passed immediately to the underlying binary
1017 buffer.
1018
1019 .. versionadded:: 3.7
1020
INADA Naoki507434f2017-12-21 09:59:53 +09001021 .. method:: reconfigure(*[, encoding][, errors][, newline][, \
1022 line_buffering][, write_through])
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02001023
INADA Naoki507434f2017-12-21 09:59:53 +09001024 Reconfigure this text stream using new settings for *encoding*,
1025 *errors*, *newline*, *line_buffering* and *write_through*.
1026
1027 Parameters not specified keep current settings, except
Harmon35068bd2019-06-19 16:01:27 -05001028 ``errors='strict'`` is used when *encoding* is specified but
INADA Naoki507434f2017-12-21 09:59:53 +09001029 *errors* is not specified.
1030
1031 It is not possible to change the encoding or newline if some data
1032 has already been read from the stream. On the other hand, changing
1033 encoding after write is possible.
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02001034
1035 This method does an implicit stream flush before setting the
1036 new parameters.
1037
1038 .. versionadded:: 3.7
1039
Georg Brandl014197c2008-04-09 18:40:51 +00001040
Julien Palard5c1f15b2021-01-25 15:46:06 +01001041.. class:: StringIO(initial_value='', newline='\\n')
Georg Brandl014197c2008-04-09 18:40:51 +00001042
Géry Ogam3b58a702019-09-11 16:55:13 +02001043 A text stream using an in-memory text buffer. It inherits
1044 :class:`TextIOBase`.
1045
1046 The text buffer is discarded when the :meth:`~IOBase.close` method is
1047 called.
Georg Brandl014197c2008-04-09 18:40:51 +00001048
Martin Pantercfad5432015-10-10 03:01:20 +00001049 The initial value of the buffer can be set by providing *initial_value*.
1050 If newline translation is enabled, newlines will be encoded as if by
1051 :meth:`~TextIOBase.write`. The stream is positioned at the start of
1052 the buffer.
1053
Géry Ogam3b58a702019-09-11 16:55:13 +02001054 The *newline* argument works like that of :class:`TextIOWrapper`,
1055 except that when writing output to the stream, if *newline* is ``None``,
1056 newlines are written as ``\n`` on all platforms.
Georg Brandl014197c2008-04-09 18:40:51 +00001057
Mark Summerfielde6d5f302008-04-21 10:29:45 +00001058 :class:`StringIO` provides this method in addition to those from
Géry Ogam3b58a702019-09-11 16:55:13 +02001059 :class:`TextIOBase` and :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +00001060
1061 .. method:: getvalue()
1062
Serhiy Storchakac057c382015-02-03 02:00:18 +02001063 Return a ``str`` containing the entire contents of the buffer.
Martin Pantercfad5432015-10-10 03:01:20 +00001064 Newlines are decoded as if by :meth:`~TextIOBase.read`, although
1065 the stream position is not changed.
Georg Brandl014197c2008-04-09 18:40:51 +00001066
Georg Brandl2932d932008-05-30 06:27:09 +00001067 Example usage::
1068
1069 import io
1070
1071 output = io.StringIO()
1072 output.write('First line.\n')
1073 print('Second line.', file=output)
1074
1075 # Retrieve file contents -- this will be
1076 # 'First line.\nSecond line.\n'
1077 contents = output.getvalue()
1078
Georg Brandl48310cd2009-01-03 21:18:54 +00001079 # Close object and discard memory buffer --
Georg Brandl2932d932008-05-30 06:27:09 +00001080 # .getvalue() will now raise an exception.
1081 output.close()
Georg Brandl014197c2008-04-09 18:40:51 +00001082
Antoine Pitroub530e142010-08-30 12:41:00 +00001083
R David Murray1b00f252012-08-15 10:43:58 -04001084.. index::
1085 single: universal newlines; io.IncrementalNewlineDecoder class
1086
Georg Brandl014197c2008-04-09 18:40:51 +00001087.. class:: IncrementalNewlineDecoder
1088
R David Murray1b00f252012-08-15 10:43:58 -04001089 A helper codec that decodes newlines for :term:`universal newlines` mode.
1090 It inherits :class:`codecs.IncrementalDecoder`.
Georg Brandl014197c2008-04-09 18:40:51 +00001091
Antoine Pitroubed81c82010-12-03 19:14:17 +00001092
Antoine Pitroubed81c82010-12-03 19:14:17 +00001093Performance
Benjamin Petersonedf51322011-02-24 03:03:46 +00001094-----------
1095
1096This section discusses the performance of the provided concrete I/O
1097implementations.
Antoine Pitroubed81c82010-12-03 19:14:17 +00001098
1099Binary I/O
Benjamin Petersonedf51322011-02-24 03:03:46 +00001100^^^^^^^^^^
Antoine Pitroubed81c82010-12-03 19:14:17 +00001101
Benjamin Petersonedf51322011-02-24 03:03:46 +00001102By reading and writing only large chunks of data even when the user asks for a
1103single byte, buffered I/O hides any inefficiency in calling and executing the
1104operating system's unbuffered I/O routines. The gain depends on the OS and the
1105kind of I/O which is performed. For example, on some modern OSes such as Linux,
1106unbuffered disk I/O can be as fast as buffered I/O. The bottom line, however,
1107is that buffered I/O offers predictable performance regardless of the platform
Eli Benderskyf877a7c2012-07-14 21:22:25 +03001108and the backing device. Therefore, it is almost always preferable to use
1109buffered I/O rather than unbuffered I/O for binary data.
Antoine Pitroubed81c82010-12-03 19:14:17 +00001110
1111Text I/O
Benjamin Petersonedf51322011-02-24 03:03:46 +00001112^^^^^^^^
Antoine Pitroubed81c82010-12-03 19:14:17 +00001113
1114Text I/O over a binary storage (such as a file) is significantly slower than
Benjamin Petersonedf51322011-02-24 03:03:46 +00001115binary I/O over the same storage, because it requires conversions between
1116unicode and binary data using a character codec. This can become noticeable
1117handling huge amounts of text data like large log files. Also,
1118:meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both quite slow
1119due to the reconstruction algorithm used.
Antoine Pitroubed81c82010-12-03 19:14:17 +00001120
1121:class:`StringIO`, however, is a native in-memory unicode container and will
1122exhibit similar speed to :class:`BytesIO`.
1123
1124Multi-threading
1125^^^^^^^^^^^^^^^
1126
Benjamin Petersonedf51322011-02-24 03:03:46 +00001127:class:`FileIO` objects are thread-safe to the extent that the operating system
1128calls (such as ``read(2)`` under Unix) they wrap are thread-safe too.
Antoine Pitroubed81c82010-12-03 19:14:17 +00001129
1130Binary buffered objects (instances of :class:`BufferedReader`,
1131:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
1132protect their internal structures using a lock; it is therefore safe to call
1133them from multiple threads at once.
1134
1135:class:`TextIOWrapper` objects are not thread-safe.
1136
1137Reentrancy
1138^^^^^^^^^^
1139
1140Binary buffered objects (instances of :class:`BufferedReader`,
1141:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
1142are not reentrant. While reentrant calls will not happen in normal situations,
Benjamin Petersonedf51322011-02-24 03:03:46 +00001143they can arise from doing I/O in a :mod:`signal` handler. If a thread tries to
Eli Benderskyf877a7c2012-07-14 21:22:25 +03001144re-enter a buffered object which it is already accessing, a :exc:`RuntimeError`
1145is raised. Note this doesn't prohibit a different thread from entering the
Benjamin Petersonedf51322011-02-24 03:03:46 +00001146buffered object.
Antoine Pitroubed81c82010-12-03 19:14:17 +00001147
Benjamin Petersonedf51322011-02-24 03:03:46 +00001148The above implicitly extends to text files, since the :func:`open()` function
1149will wrap a buffered object inside a :class:`TextIOWrapper`. This includes
Géry Ogam3b58a702019-09-11 16:55:13 +02001150standard streams and therefore affects the built-in :func:`print()` function as
Benjamin Petersonedf51322011-02-24 03:03:46 +00001151well.