| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1 | :mod:`io` --- Core tools for working with streams |
| 2 | ================================================= |
| 3 | |
| 4 | .. module:: io |
| 5 | :synopsis: Core tools for working with streams. |
| Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 7 | .. 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 Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 10 | .. moduleauthor:: Antoine Pitrou <solipsis@pitrou.net> |
| 11 | .. moduleauthor:: Amaury Forgeot d'Arc <amauryfa@gmail.com> |
| Benjamin Peterson | ef9f2bd | 2009-05-01 20:45:43 +0000 | [diff] [blame] | 12 | .. moduleauthor:: Benjamin Peterson <benjamin@python.org> |
| Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 13 | .. sectionauthor:: Benjamin Peterson <benjamin@python.org> |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 14 | |
| Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 15 | **Source code:** :source:`Lib/io.py` |
| 16 | |
| 17 | -------------- |
| 18 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 19 | .. _io-overview: |
| 20 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 21 | Overview |
| 22 | -------- |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 23 | |
| R David Murray | 9f0c940 | 2012-08-17 20:33:54 -0400 | [diff] [blame] | 24 | .. index:: |
| 25 | single: file object; io module |
| 26 | |
| 27 | The :mod:`io` module provides Python's main facilities for dealing with various |
| 28 | types of I/O. There are three main types of I/O: *text I/O*, *binary I/O* |
| 29 | and *raw I/O*. These are generic categories, and various backing stores can |
| 30 | be used for each of them. A concrete object belonging to any of these |
| 31 | categories is called a :term:`file object`. Other common terms are *stream* |
| 32 | and *file-like object*. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 33 | |
| Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి) | cd44980 | 2018-11-12 09:36:18 +0530 | [diff] [blame] | 34 | Independent of its category, each concrete stream object will also have |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 35 | various capabilities: it can be read-only, write-only, or read-write. It can |
| 36 | also allow arbitrary random access (seeking forwards or backwards to any |
| 37 | location), or only sequential access (for example in the case of a socket or |
| 38 | pipe). |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 39 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 40 | All streams are careful about the type of data you give to them. For example |
| 41 | giving a :class:`str` object to the ``write()`` method of a binary stream |
| Stéphane Wirtel | e483f02 | 2018-10-26 12:52:11 +0200 | [diff] [blame] | 42 | will raise a :exc:`TypeError`. So will giving a :class:`bytes` object to the |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 43 | ``write()`` method of a text stream. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 44 | |
| Antoine Pitrou | a787b65 | 2011-10-12 19:02:52 +0200 | [diff] [blame] | 45 | .. versionchanged:: 3.3 |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 46 | Operations that used to raise :exc:`IOError` now raise :exc:`OSError`, since |
| 47 | :exc:`IOError` is now an alias of :exc:`OSError`. |
| Antoine Pitrou | a787b65 | 2011-10-12 19:02:52 +0200 | [diff] [blame] | 48 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 49 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 50 | Text I/O |
| 51 | ^^^^^^^^ |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 52 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 53 | Text I/O expects and produces :class:`str` objects. This means that whenever |
| 54 | the backing store is natively made of bytes (such as in the case of a file), |
| 55 | encoding and decoding of data is made transparently as well as optional |
| 56 | translation of platform-specific newline characters. |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 57 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 58 | The easiest way to create a text stream is with :meth:`open()`, optionally |
| 59 | specifying an encoding:: |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 60 | |
| 61 | f = open("myfile.txt", "r", encoding="utf-8") |
| 62 | |
| 63 | In-memory text streams are also available as :class:`StringIO` objects:: |
| 64 | |
| 65 | f = io.StringIO("some initial text data") |
| 66 | |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 67 | The text stream API is described in detail in the documentation of |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 68 | :class:`TextIOBase`. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 69 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 70 | |
| 71 | Binary I/O |
| 72 | ^^^^^^^^^^ |
| 73 | |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 74 | Binary I/O (also called *buffered I/O*) expects |
| 75 | :term:`bytes-like objects <bytes-like object>` and produces :class:`bytes` |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 76 | objects. No encoding, decoding, or newline translation is performed. This |
| 77 | category of streams can be used for all kinds of non-text data, and also when |
| 78 | manual control over the handling of text data is desired. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 79 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 80 | The easiest way to create a binary stream is with :meth:`open()` with ``'b'`` in |
| 81 | the mode string:: |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 82 | |
| 83 | f = open("myfile.jpg", "rb") |
| 84 | |
| 85 | In-memory binary streams are also available as :class:`BytesIO` objects:: |
| 86 | |
| 87 | f = io.BytesIO(b"some initial binary data: \x00\x01") |
| 88 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 89 | The binary stream API is described in detail in the docs of |
| 90 | :class:`BufferedIOBase`. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 91 | |
| 92 | Other library modules may provide additional ways to create text or binary |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 93 | streams. See :meth:`socket.socket.makefile` for example. |
| 94 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 95 | |
| 96 | Raw I/O |
| 97 | ^^^^^^^ |
| 98 | |
| 99 | Raw I/O (also called *unbuffered I/O*) is generally used as a low-level |
| 100 | building-block for binary and text streams; it is rarely useful to directly |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 101 | manipulate a raw stream from user code. Nevertheless, you can create a raw |
| 102 | stream by opening a file in binary mode with buffering disabled:: |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 103 | |
| 104 | f = open("myfile.jpg", "rb", buffering=0) |
| 105 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 106 | The raw stream API is described in detail in the docs of :class:`RawIOBase`. |
| Benjamin Peterson | cc12e1b | 2010-02-19 00:58:13 +0000 | [diff] [blame] | 107 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 108 | |
| Inada Naoki | 4827483 | 2021-03-29 12:28:14 +0900 | [diff] [blame] | 109 | .. _io-text-encoding: |
| 110 | |
| 111 | Text Encoding |
| 112 | ------------- |
| 113 | |
| 114 | The default encoding of :class:`TextIOWrapper` and :func:`open` is |
| 115 | locale-specific (:func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`). |
| 116 | |
| 117 | However, many developers forget to specify the encoding when opening text files |
| 118 | encoded in UTF-8 (e.g. JSON, TOML, Markdown, etc...) since most Unix |
| 119 | platforms use UTF-8 locale by default. This causes bugs because the locale |
| 120 | encoding 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 | |
| 126 | Additionally, while there is no concrete plan as of yet, Python may change |
| 127 | the default text file encoding to UTF-8 in the future. |
| 128 | |
| 129 | Accordingly, it is highly recommended that you specify the encoding |
| 130 | explicitly 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) | a550820 | 2021-12-26 03:52:46 -0800 | [diff] [blame] | 134 | When you need to run existing code on Windows that attempts to open |
| Inada Naoki | 4827483 | 2021-03-29 12:28:14 +0900 | [diff] [blame] | 135 | UTF-8 files using the default locale encoding, you can enable the UTF-8 |
| 136 | mode. See :ref:`UTF-8 mode on Windows <win-utf8-mode>`. |
| 137 | |
| 138 | .. _io-encoding-warning: |
| 139 | |
| 140 | Opt-in EncodingWarning |
| 141 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 142 | |
| 143 | .. versionadded:: 3.10 |
| 144 | See :pep:`597` for more details. |
| 145 | |
| 146 | To find where the default locale encoding is used, you can enable |
| 147 | the ``-X warn_default_encoding`` command line option or set the |
| 148 | :envvar:`PYTHONWARNDEFAULTENCODING` environment variable, which will |
| 149 | emit an :exc:`EncodingWarning` when the default encoding is used. |
| 150 | |
| 151 | If you are providing an API that uses :func:`open` or |
| 152 | :class:`TextIOWrapper` and passes ``encoding=None`` as a parameter, you |
| 153 | can use :func:`text_encoding` so that callers of the API will emit an |
| 154 | :exc:`EncodingWarning` if they don't pass an ``encoding``. However, |
| 155 | please consider using UTF-8 by default (i.e. ``encoding="utf-8"``) for |
| 156 | new APIs. |
| 157 | |
| 158 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 159 | High-level Module Interface |
| 160 | --------------------------- |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 161 | |
| 162 | .. data:: DEFAULT_BUFFER_SIZE |
| 163 | |
| 164 | An int containing the default buffer size used by the module's buffered I/O |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 165 | classes. :func:`open` uses the file's blksize (as obtained by |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 166 | :func:`os.stat`) if possible. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 167 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 168 | |
| Andrew Svetlov | a60de4f | 2013-02-17 16:55:58 +0200 | [diff] [blame] | 169 | .. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 170 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 171 | This is an alias for the builtin :func:`open` function. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 172 | |
| Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 173 | .. audit-event:: open path,mode,flags io.open |
| Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 174 | |
| Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 175 | This function raises an :ref:`auditing event <auditing>` ``open`` with |
| Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 176 | 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 | |
| Shantanu | 831d58d | 2020-05-01 10:52:10 -0700 | [diff] [blame] | 185 | ``path`` should be a :class:`str` and an absolute path. |
| Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 186 | |
| 187 | The behavior of this function may be overridden by an earlier call to the |
| Shantanu | 831d58d | 2020-05-01 10:52:10 -0700 | [diff] [blame] | 188 | :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 Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 192 | |
| 193 | .. versionadded:: 3.8 |
| 194 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 195 | |
| Inada Naoki | 4827483 | 2021-03-29 12:28:14 +0900 | [diff] [blame] | 196 | .. 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 Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 222 | .. exception:: BlockingIOError |
| 223 | |
| Antoine Pitrou | f55011f | 2011-10-12 18:57:23 +0200 | [diff] [blame] | 224 | This is a compatibility alias for the builtin :exc:`BlockingIOError` |
| 225 | exception. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 226 | |
| 227 | |
| 228 | .. exception:: UnsupportedOperation |
| 229 | |
| Antoine Pitrou | a787b65 | 2011-10-12 19:02:52 +0200 | [diff] [blame] | 230 | An exception inheriting :exc:`OSError` and :exc:`ValueError` that is raised |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 231 | when an unsupported operation is called on a stream. |
| 232 | |
| 233 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 234 | .. seealso:: |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 235 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 236 | :mod:`sys` |
| 237 | contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`, |
| 238 | and :data:`sys.stderr`. |
| 239 | |
| 240 | |
| 241 | Class hierarchy |
| 242 | --------------- |
| 243 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 244 | The 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 |
| 246 | specify the various categories of streams, then concrete classes providing the |
| 247 | standard stream implementations. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 248 | |
| 249 | .. note:: |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 250 | |
| 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 Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 254 | :meth:`~IOBase.readinto` and :meth:`~IOBase.readline`. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 255 | |
| 256 | At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It |
| 257 | defines the basic interface to a stream. Note, however, that there is no |
| 258 | separation between reading and writing to streams; implementations are allowed |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 259 | to raise :exc:`UnsupportedOperation` if they do not support a given operation. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 260 | |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 261 | The :class:`RawIOBase` ABC extends :class:`IOBase`. It deals with the reading |
| 262 | and writing of bytes to a stream. :class:`FileIO` subclasses :class:`RawIOBase` |
| 263 | to provide an interface to files in the machine's file system. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 264 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 265 | The :class:`BufferedIOBase` ABC extends :class:`IOBase`. It deals with |
| 266 | buffering on a raw binary stream (:class:`RawIOBase`). Its subclasses, |
| 267 | :class:`BufferedWriter`, :class:`BufferedReader`, and :class:`BufferedRWPair` |
| 268 | buffer raw binary streams that are readable, writable, and both readable and writable, |
| 269 | respectively. :class:`BufferedRandom` provides a buffered interface to seekable streams. |
| 270 | Another :class:`BufferedIOBase` subclass, :class:`BytesIO`, is a stream of |
| 271 | in-memory bytes. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 272 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 273 | The :class:`TextIOBase` ABC extends :class:`IOBase`. It deals with |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 274 | streams whose bytes represent text, and handles encoding and decoding to and |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 275 | from strings. :class:`TextIOWrapper`, which extends :class:`TextIOBase`, is a buffered text |
| 276 | interface to a buffered raw stream (:class:`BufferedIOBase`). Finally, |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 277 | :class:`StringIO` is an in-memory stream for text. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 278 | |
| 279 | Argument names are not part of the specification, and only the arguments of |
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 280 | :func:`open` are intended to be used as keyword arguments. |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 281 | |
| Andrew Svetlov | ed636a8 | 2012-12-06 12:20:56 +0200 | [diff] [blame] | 282 | The following table summarizes the ABCs provided by the :mod:`io` module: |
| 283 | |
| Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 284 | .. tabularcolumns:: |l|l|L|L| |
| 285 | |
| Andrew Svetlov | ed636a8 | 2012-12-06 12:20:56 +0200 | [diff] [blame] | 286 | ========================= ================== ======================== ================================================== |
| 287 | ABC 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 Khurana | 1b74f9b | 2017-12-11 19:12:09 +0530 | [diff] [blame] | 296 | :class:`BufferedIOBase` :class:`IOBase` ``detach``, ``read``, Inherited :class:`IOBase` methods, ``readinto``, |
| 297 | ``read1``, and ``write`` and ``readinto1`` |
| Andrew Svetlov | ed636a8 | 2012-12-06 12:20:56 +0200 | [diff] [blame] | 298 | :class:`TextIOBase` :class:`IOBase` ``detach``, ``read``, Inherited :class:`IOBase` methods, ``encoding``, |
| 299 | ``readline``, and ``errors``, and ``newlines`` |
| 300 | ``write`` |
| 301 | ========================= ================== ======================== ================================================== |
| 302 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 303 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 304 | I/O Base Classes |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 305 | ^^^^^^^^^^^^^^^^ |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 306 | |
| 307 | .. class:: IOBase |
| 308 | |
| Miss Islington (bot) | bdce188 | 2022-03-04 10:33:57 -0800 | [diff] [blame^] | 309 | The abstract base class for all I/O classes. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 310 | |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 311 | This class provides empty abstract implementations for many methods |
| 312 | that derived classes can override selectively; the default |
| 313 | implementations represent a file that cannot be read, written or |
| 314 | seeked. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 315 | |
| Steve Palmer | 7b97ab3 | 2019-04-09 05:35:27 +0100 | [diff] [blame] | 316 | Even though :class:`IOBase` does not declare :meth:`read` |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 317 | or :meth:`write` because their signatures will vary, implementations and |
| 318 | clients should consider those methods part of the interface. Also, |
| Antoine Pitrou | a787b65 | 2011-10-12 19:02:52 +0200 | [diff] [blame] | 319 | implementations may raise a :exc:`ValueError` (or :exc:`UnsupportedOperation`) |
| 320 | when operations they do not support are called. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 321 | |
| 322 | The basic type used for binary data read from or written to a file is |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 323 | :class:`bytes`. Other :term:`bytes-like objects <bytes-like object>` are |
| Steve Palmer | 7b97ab3 | 2019-04-09 05:35:27 +0100 | [diff] [blame] | 324 | accepted as method arguments too. Text I/O classes work with :class:`str` data. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 325 | |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 326 | Note that calling any method (even inquiries) on a closed stream is |
| Antoine Pitrou | a787b65 | 2011-10-12 19:02:52 +0200 | [diff] [blame] | 327 | undefined. Implementations may raise :exc:`ValueError` in this case. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 328 | |
| Éric Araujo | 3f7c0e4 | 2012-12-08 22:53:43 -0500 | [diff] [blame] | 329 | :class:`IOBase` (and its subclasses) supports the iterator protocol, meaning |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 330 | that an :class:`IOBase` object can be iterated over yielding the lines in a |
| 331 | stream. Lines are defined slightly differently depending on whether the |
| 332 | stream is a binary stream (yielding bytes), or a text stream (yielding |
| 333 | character strings). See :meth:`~IOBase.readline` below. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 334 | |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 335 | :class:`IOBase` is also a context manager and therefore supports the |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 336 | :keyword:`with` statement. In this example, *file* is closed after the |
| Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 337 | :keyword:`!with` statement's suite is finished---even if an exception occurs:: |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 338 | |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 339 | with open('spam.txt', 'w') as file: |
| 340 | file.write('Spam and eggs!') |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 341 | |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 342 | :class:`IOBase` provides these data attributes and methods: |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 343 | |
| 344 | .. method:: close() |
| 345 | |
| Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 346 | Flush and close this stream. This method has no effect if the file is |
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 347 | already closed. Once the file is closed, any operation on the file |
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 348 | (e.g. reading or writing) will raise a :exc:`ValueError`. |
| Antoine Pitrou | f9fc08f | 2010-04-28 19:59:32 +0000 | [diff] [blame] | 349 | |
| 350 | As a convenience, it is allowed to call this method more than once; |
| 351 | only the first call, however, will have an effect. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 352 | |
| 353 | .. attribute:: closed |
| 354 | |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 355 | ``True`` if the stream is closed. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 356 | |
| 357 | .. method:: fileno() |
| 358 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 359 | Return the underlying file descriptor (an integer) of the stream if it |
| Antoine Pitrou | a787b65 | 2011-10-12 19:02:52 +0200 | [diff] [blame] | 360 | exists. An :exc:`OSError` is raised if the IO object does not use a file |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 361 | descriptor. |
| 362 | |
| 363 | .. method:: flush() |
| 364 | |
| Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 365 | Flush the write buffers of the stream if applicable. This does nothing |
| 366 | for read-only and non-blocking streams. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 367 | |
| 368 | .. method:: isatty() |
| 369 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 370 | Return ``True`` if the stream is interactive (i.e., connected to |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 371 | a terminal/tty device). |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 372 | |
| 373 | .. method:: readable() |
| 374 | |
| Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 375 | Return ``True`` if the stream can be read from. If ``False``, :meth:`read` |
| Antoine Pitrou | a787b65 | 2011-10-12 19:02:52 +0200 | [diff] [blame] | 376 | will raise :exc:`OSError`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 377 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 378 | .. method:: readline(size=-1) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 379 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 380 | Read and return one line from the stream. If *size* is specified, at |
| 381 | most *size* bytes will be read. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 382 | |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 383 | The line terminator is always ``b'\n'`` for binary files; for text files, |
| Zachary Ware | 0069eac | 2014-07-18 09:11:48 -0500 | [diff] [blame] | 384 | the *newline* argument to :func:`open` can be used to select the line |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 385 | terminator(s) recognized. |
| 386 | |
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 387 | .. method:: readlines(hint=-1) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 388 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 389 | Read and return a list of lines from the stream. *hint* can be specified |
| 390 | to control the number of lines read: no more lines will be read if the |
| 391 | total size (in bytes/characters) of all lines so far exceeds *hint*. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 392 | |
| Miss Islington (bot) | 9b369c9 | 2021-07-13 07:41:12 -0700 | [diff] [blame] | 393 | *hint* values of ``0`` or less, as well as ``None``, are treated as no |
| 394 | hint. |
| 395 | |
| Ezio Melotti | ed3cd7e | 2013-04-15 19:08:31 +0300 | [diff] [blame] | 396 | Note that it's already possible to iterate on file objects using ``for |
| 397 | line in file: ...`` without calling ``file.readlines()``. |
| 398 | |
| Benjamin Peterson | 2a3d4d9 | 2019-07-10 19:43:04 -0700 | [diff] [blame] | 399 | .. method:: seek(offset, whence=SEEK_SET) |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 400 | |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 401 | Change the stream position to the given byte *offset*. *offset* is |
| Martin Panter | db4220e | 2015-09-11 03:58:30 +0000 | [diff] [blame] | 402 | interpreted relative to the position indicated by *whence*. The default |
| 403 | value for *whence* is :data:`SEEK_SET`. Values for *whence* are: |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 404 | |
| Benjamin Peterson | 0e4caf4 | 2009-04-01 21:22:20 +0000 | [diff] [blame] | 405 | * :data:`SEEK_SET` or ``0`` -- start of the stream (the default); |
| 406 | *offset* should be zero or positive |
| 407 | * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may |
| 408 | be negative |
| 409 | * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually |
| 410 | negative |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 411 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 412 | Return the new absolute position. |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 413 | |
| Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 414 | .. versionadded:: 3.1 |
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 415 | The ``SEEK_*`` constants. |
| Benjamin Peterson | 0e4caf4 | 2009-04-01 21:22:20 +0000 | [diff] [blame] | 416 | |
| Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 417 | .. versionadded:: 3.3 |
| 418 | Some operating systems could support additional values, like |
| 419 | :data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`. The valid values |
| 420 | for a file could depend on it being open in text or binary mode. |
| 421 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 422 | .. method:: seekable() |
| 423 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 424 | Return ``True`` if the stream supports random access. If ``False``, |
| Antoine Pitrou | a787b65 | 2011-10-12 19:02:52 +0200 | [diff] [blame] | 425 | :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`OSError`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 426 | |
| 427 | .. method:: tell() |
| 428 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 429 | Return the current stream position. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 430 | |
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 431 | .. method:: truncate(size=None) |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 432 | |
| Antoine Pitrou | 2016dc9 | 2010-05-29 12:08:25 +0000 | [diff] [blame] | 433 | Resize the stream to the given *size* in bytes (or the current position |
| 434 | if *size* is not specified). The current stream position isn't changed. |
| 435 | This resizing can extend or reduce the current file size. In case of |
| 436 | extension, the contents of the new file area depend on the platform |
| Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 437 | (on most systems, additional bytes are zero-filled). The new file size |
| 438 | is returned. |
| 439 | |
| Emmanuel Arias | 522630a | 2019-02-15 16:02:38 -0300 | [diff] [blame] | 440 | .. versionchanged:: 3.5 |
| 441 | Windows will now zero-fill files when extending. |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 442 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 443 | .. method:: writable() |
| 444 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 445 | Return ``True`` if the stream supports writing. If ``False``, |
| Antoine Pitrou | a787b65 | 2011-10-12 19:02:52 +0200 | [diff] [blame] | 446 | :meth:`write` and :meth:`truncate` will raise :exc:`OSError`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 447 | |
| 448 | .. method:: writelines(lines) |
| 449 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 450 | Write a list of lines to the stream. Line separators are not added, so it |
| 451 | is usual for each of the lines provided to have a line separator at the |
| 452 | end. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 453 | |
| Benjamin Peterson | ef8abfc | 2014-06-14 18:51:34 -0700 | [diff] [blame] | 454 | .. method:: __del__() |
| 455 | |
| 456 | Prepare for object destruction. :class:`IOBase` provides a default |
| 457 | implementation of this method that calls the instance's |
| 458 | :meth:`~IOBase.close` method. |
| 459 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 460 | |
| 461 | .. class:: RawIOBase |
| 462 | |
| Miss Islington (bot) | bdce188 | 2022-03-04 10:33:57 -0800 | [diff] [blame^] | 463 | Base class for raw binary streams. It inherits :class:`IOBase`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 464 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 465 | Raw binary streams typically provide low-level access to an underlying OS |
| 466 | device or API, and do not try to encapsulate it in high-level primitives |
| 467 | (this functionality is done at a higher-level in buffered binary streams and text streams, described later |
| 468 | in this page). |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 469 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 470 | :class:`RawIOBase` provides these methods in addition to those from |
| 471 | :class:`IOBase`: |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 472 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 473 | .. method:: read(size=-1) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 474 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 475 | Read up to *size* bytes from the object and return them. As a convenience, |
| Sanyam Khurana | 1b74f9b | 2017-12-11 19:12:09 +0530 | [diff] [blame] | 476 | if *size* is unspecified or -1, all bytes until EOF are returned. |
| 477 | Otherwise, only one system call is ever made. Fewer than *size* bytes may |
| 478 | be returned if the operating system call returns fewer than *size* bytes. |
| Antoine Pitrou | 78ddbe6 | 2009-10-01 16:24:45 +0000 | [diff] [blame] | 479 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 480 | If 0 bytes are returned, and *size* was not 0, this indicates end of file. |
| Antoine Pitrou | 78ddbe6 | 2009-10-01 16:24:45 +0000 | [diff] [blame] | 481 | If the object is in non-blocking mode and no bytes are available, |
| 482 | ``None`` is returned. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 483 | |
| Sanyam Khurana | 1b74f9b | 2017-12-11 19:12:09 +0530 | [diff] [blame] | 484 | The default implementation defers to :meth:`readall` and |
| 485 | :meth:`readinto`. |
| 486 | |
| Benjamin Peterson | b47aace | 2008-04-09 21:38:38 +0000 | [diff] [blame] | 487 | .. method:: readall() |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 488 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 489 | Read and return all the bytes from the stream until EOF, using multiple |
| 490 | calls to the stream if necessary. |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 491 | |
| 492 | .. method:: readinto(b) |
| 493 | |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 494 | Read bytes into a pre-allocated, writable |
| 495 | :term:`bytes-like object` *b*, and return the |
| Steve Palmer | 7b97ab3 | 2019-04-09 05:35:27 +0100 | [diff] [blame] | 496 | number of bytes read. For example, *b* might be a :class:`bytearray`. |
| 497 | If the object is in non-blocking mode and no bytes |
| Benjamin Peterson | 2a1a490 | 2014-06-22 14:19:07 -0700 | [diff] [blame] | 498 | are available, ``None`` is returned. |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 499 | |
| 500 | .. method:: write(b) |
| 501 | |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 502 | Write the given :term:`bytes-like object`, *b*, to the |
| 503 | underlying raw stream, and return the number of |
| 504 | bytes written. This can be less than the length of *b* in |
| 505 | bytes, depending on specifics of the underlying raw |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 506 | stream, and especially if it is in non-blocking mode. ``None`` is |
| 507 | returned if the raw stream is set not to block and no single byte could |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 508 | be readily written to it. The caller may release or mutate *b* after |
| 509 | this method returns, so the implementation should only access *b* |
| 510 | during the method call. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 511 | |
| 512 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 513 | .. class:: BufferedIOBase |
| 514 | |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 515 | Base class for binary streams that support some kind of buffering. |
| Miss Islington (bot) | bdce188 | 2022-03-04 10:33:57 -0800 | [diff] [blame^] | 516 | It inherits :class:`IOBase`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 517 | |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 518 | The main difference with :class:`RawIOBase` is that methods :meth:`read`, |
| 519 | :meth:`readinto` and :meth:`write` will try (respectively) to read as much |
| 520 | input as requested or to consume all given output, at the expense of |
| 521 | making perhaps more than one system call. |
| 522 | |
| 523 | In addition, those methods can raise :exc:`BlockingIOError` if the |
| 524 | underlying raw stream is in non-blocking mode and cannot take or give |
| 525 | enough data; unlike their :class:`RawIOBase` counterparts, they will |
| 526 | never return ``None``. |
| 527 | |
| 528 | Besides, the :meth:`read` method does not have a default |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 529 | implementation that defers to :meth:`readinto`. |
| 530 | |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 531 | A typical :class:`BufferedIOBase` implementation should not inherit from a |
| 532 | :class:`RawIOBase` implementation, but wrap one, like |
| 533 | :class:`BufferedWriter` and :class:`BufferedReader` do. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 534 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 535 | :class:`BufferedIOBase` provides or overrides these data attributes and |
| 536 | methods in addition to those from :class:`IOBase`: |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 537 | |
| Benjamin Peterson | c609b6b | 2009-06-28 17:32:20 +0000 | [diff] [blame] | 538 | .. attribute:: raw |
| 539 | |
| 540 | The underlying raw stream (a :class:`RawIOBase` instance) that |
| 541 | :class:`BufferedIOBase` deals with. This is not part of the |
| 542 | :class:`BufferedIOBase` API and may not exist on some implementations. |
| 543 | |
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 544 | .. method:: detach() |
| 545 | |
| 546 | Separate the underlying raw stream from the buffer and return it. |
| 547 | |
| 548 | After the raw stream has been detached, the buffer is in an unusable |
| 549 | state. |
| 550 | |
| 551 | Some buffers, like :class:`BytesIO`, do not have the concept of a single |
| 552 | raw stream to return from this method. They raise |
| 553 | :exc:`UnsupportedOperation`. |
| 554 | |
| Benjamin Peterson | edc3647 | 2009-05-01 20:48:14 +0000 | [diff] [blame] | 555 | .. versionadded:: 3.1 |
| 556 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 557 | .. method:: read(size=-1) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 558 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 559 | Read and return up to *size* bytes. If the argument is omitted, ``None``, |
| 560 | or negative, data is read and returned until EOF is reached. An empty |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 561 | :class:`bytes` object is returned if the stream is already at EOF. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 562 | |
| 563 | If the argument is positive, and the underlying raw stream is not |
| 564 | interactive, multiple raw reads may be issued to satisfy the byte count |
| 565 | (unless EOF is reached first). But for interactive raw streams, at most |
| 566 | one raw read will be issued, and a short result does not imply that EOF is |
| 567 | imminent. |
| 568 | |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 569 | A :exc:`BlockingIOError` is raised if the underlying raw stream is in |
| 570 | non blocking-mode, and has no data available at the moment. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 571 | |
| Martin Panter | ccb2c0e | 2016-10-20 23:48:14 +0000 | [diff] [blame] | 572 | .. method:: read1([size]) |
| Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 573 | |
| Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 574 | Read and return up to *size* bytes, with at most one call to the |
| 575 | underlying raw stream's :meth:`~RawIOBase.read` (or |
| Benjamin Peterson | 2a1a490 | 2014-06-22 14:19:07 -0700 | [diff] [blame] | 576 | :meth:`~RawIOBase.readinto`) method. This can be useful if you are |
| 577 | implementing your own buffering on top of a :class:`BufferedIOBase` |
| 578 | object. |
| Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 579 | |
| Martin Panter | 4e94679 | 2016-10-21 23:00:10 +0000 | [diff] [blame] | 580 | If *size* is ``-1`` (the default), an arbitrary number of bytes are |
| Martin Panter | ccb2c0e | 2016-10-20 23:48:14 +0000 | [diff] [blame] | 581 | returned (more than zero unless EOF is reached). |
| 582 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 583 | .. method:: readinto(b) |
| 584 | |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 585 | Read bytes into a pre-allocated, writable |
| 586 | :term:`bytes-like object` *b* and return the number of bytes read. |
| Steve Palmer | 7b97ab3 | 2019-04-09 05:35:27 +0100 | [diff] [blame] | 587 | For example, *b* might be a :class:`bytearray`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 588 | |
| 589 | Like :meth:`read`, multiple reads may be issued to the underlying raw |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 590 | stream, unless the latter is interactive. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 591 | |
| Benjamin Peterson | 2a1a490 | 2014-06-22 14:19:07 -0700 | [diff] [blame] | 592 | A :exc:`BlockingIOError` is raised if the underlying raw stream is in non |
| 593 | blocking-mode, and has no data available at the moment. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 594 | |
| Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 595 | .. method:: readinto1(b) |
| 596 | |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 597 | Read bytes into a pre-allocated, writable |
| 598 | :term:`bytes-like object` *b*, using at most one call to |
| Benjamin Peterson | 2a1a490 | 2014-06-22 14:19:07 -0700 | [diff] [blame] | 599 | the underlying raw stream's :meth:`~RawIOBase.read` (or |
| 600 | :meth:`~RawIOBase.readinto`) method. Return the number of bytes read. |
| Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 601 | |
| Benjamin Peterson | 2a1a490 | 2014-06-22 14:19:07 -0700 | [diff] [blame] | 602 | A :exc:`BlockingIOError` is raised if the underlying raw stream is in non |
| 603 | blocking-mode, and has no data available at the moment. |
| Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 604 | |
| 605 | .. versionadded:: 3.5 |
| 606 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 607 | .. method:: write(b) |
| 608 | |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 609 | Write the given :term:`bytes-like object`, *b*, and return the number |
| 610 | of bytes written (always equal to the length of *b* in bytes, since if |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 611 | the write fails an :exc:`OSError` will be raised). Depending on the |
| 612 | actual implementation, these bytes may be readily written to the |
| 613 | underlying stream, or held in a buffer for performance and latency |
| 614 | reasons. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 615 | |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 616 | When in non-blocking mode, a :exc:`BlockingIOError` is raised if the |
| 617 | data needed to be written to the raw stream but it couldn't accept |
| 618 | all the data without blocking. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 619 | |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 620 | The caller may release or mutate *b* after this method returns, |
| 621 | so the implementation should only access *b* during the method call. |
| 622 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 623 | |
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 624 | Raw File I/O |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 625 | ^^^^^^^^^^^^ |
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 626 | |
| Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 627 | .. class:: FileIO(name, mode='r', closefd=True, opener=None) |
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 628 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 629 | A raw binary stream representing an OS-level file containing bytes data. It |
| 630 | inherits :class:`RawIOBase`. |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 631 | |
| 632 | The *name* can be one of two things: |
| 633 | |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 634 | * a character string or :class:`bytes` object representing the path to the |
| Serhiy Storchaka | 4adf01c | 2016-10-19 18:30:05 +0300 | [diff] [blame] | 635 | file which will be opened. In this case closefd must be ``True`` (the default) |
| Robert Collins | 933430a | 2014-10-18 13:32:43 +1300 | [diff] [blame] | 636 | otherwise an error will be raised. |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 637 | * an integer representing the number of an existing OS-level file descriptor |
| Robert Collins | 933430a | 2014-10-18 13:32:43 +1300 | [diff] [blame] | 638 | to which the resulting :class:`FileIO` object will give access. When the |
| 639 | FileIO object is closed this fd will be closed as well, unless *closefd* |
| 640 | is set to ``False``. |
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 641 | |
| Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 642 | The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading |
| Charles-François Natali | d612de1 | 2012-01-14 11:51:00 +0100 | [diff] [blame] | 643 | (default), writing, exclusive creation or appending. The file will be |
| 644 | created if it doesn't exist when opened for writing or appending; it will be |
| 645 | truncated when opened for writing. :exc:`FileExistsError` will be raised if |
| 646 | it already exists when opened for creating. Opening a file for creating |
| 647 | implies writing, so this mode behaves in a similar way to ``'w'``. Add a |
| 648 | ``'+'`` to the mode to allow simultaneous reading and writing. |
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 649 | |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 650 | The :meth:`read` (when called with a positive argument), :meth:`readinto` |
| 651 | and :meth:`write` methods on this class will only make one system call. |
| 652 | |
| Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 653 | A custom opener can be used by passing a callable as *opener*. The underlying |
| 654 | file descriptor for the file object is then obtained by calling *opener* with |
| 655 | (*name*, *flags*). *opener* must return an open file descriptor (passing |
| 656 | :mod:`os.open` as *opener* results in functionality similar to passing |
| 657 | ``None``). |
| 658 | |
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 659 | The newly created file is :ref:`non-inheritable <fd_inheritance>`. |
| 660 | |
| Éric Araujo | 8f423c9 | 2012-11-03 17:06:52 -0400 | [diff] [blame] | 661 | See the :func:`open` built-in function for examples on using the *opener* |
| 662 | parameter. |
| 663 | |
| Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 664 | .. versionchanged:: 3.3 |
| 665 | The *opener* parameter was added. |
| Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 666 | The ``'x'`` mode was added. |
| Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 667 | |
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 668 | .. versionchanged:: 3.4 |
| 669 | The file is now non-inheritable. |
| 670 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 671 | :class:`FileIO` provides these data attributes in addition to those from |
| 672 | :class:`RawIOBase` and :class:`IOBase`: |
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 673 | |
| 674 | .. attribute:: mode |
| 675 | |
| 676 | The mode as given in the constructor. |
| 677 | |
| 678 | .. attribute:: name |
| 679 | |
| 680 | The file name. This is the file descriptor of the file when no name is |
| 681 | given in the constructor. |
| 682 | |
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 683 | |
| 684 | Buffered Streams |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 685 | ^^^^^^^^^^^^^^^^ |
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 686 | |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 687 | Buffered I/O streams provide a higher-level interface to an I/O device |
| 688 | than raw I/O does. |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 689 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 690 | .. class:: BytesIO([initial_bytes]) |
| 691 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 692 | A binary stream using an in-memory bytes buffer. It inherits |
| Serhiy Storchaka | c057c38 | 2015-02-03 02:00:18 +0200 | [diff] [blame] | 693 | :class:`BufferedIOBase`. The buffer is discarded when the |
| 694 | :meth:`~IOBase.close` method is called. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 695 | |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 696 | The optional argument *initial_bytes* is a :term:`bytes-like object` that |
| 697 | contains initial data. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 698 | |
| 699 | :class:`BytesIO` provides or overrides these methods in addition to those |
| 700 | from :class:`BufferedIOBase` and :class:`IOBase`: |
| 701 | |
| Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 702 | .. method:: getbuffer() |
| 703 | |
| 704 | Return a readable and writable view over the contents of the buffer |
| 705 | without copying them. Also, mutating the view will transparently |
| 706 | update the contents of the buffer:: |
| 707 | |
| 708 | >>> b = io.BytesIO(b"abcdef") |
| 709 | >>> view = b.getbuffer() |
| 710 | >>> view[2:4] = b"56" |
| 711 | >>> b.getvalue() |
| 712 | b'ab56ef' |
| 713 | |
| 714 | .. note:: |
| 715 | As long as the view exists, the :class:`BytesIO` object cannot be |
| Serhiy Storchaka | c057c38 | 2015-02-03 02:00:18 +0200 | [diff] [blame] | 716 | resized or closed. |
| Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 717 | |
| 718 | .. versionadded:: 3.2 |
| 719 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 720 | .. method:: getvalue() |
| 721 | |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 722 | Return :class:`bytes` containing the entire contents of the buffer. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 723 | |
| Serhiy Storchaka | c057c38 | 2015-02-03 02:00:18 +0200 | [diff] [blame] | 724 | |
| Martin Panter | ccb2c0e | 2016-10-20 23:48:14 +0000 | [diff] [blame] | 725 | .. method:: read1([size]) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 726 | |
| Martin Panter | ccb2c0e | 2016-10-20 23:48:14 +0000 | [diff] [blame] | 727 | In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.read`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 728 | |
| Martin Panter | ccb2c0e | 2016-10-20 23:48:14 +0000 | [diff] [blame] | 729 | .. versionchanged:: 3.7 |
| 730 | The *size* argument is now optional. |
| Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 731 | |
| Martin Panter | ccb2c0e | 2016-10-20 23:48:14 +0000 | [diff] [blame] | 732 | .. method:: readinto1(b) |
| 733 | |
| 734 | In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.readinto`. |
| Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 735 | |
| 736 | .. versionadded:: 3.5 |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 737 | |
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 738 | .. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 739 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 740 | A buffered binary stream providing higher-level access to a readable, non |
| 741 | seekable :class:`RawIOBase` raw binary stream. It inherits |
| 742 | :class:`BufferedIOBase`. |
| 743 | |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 744 | When reading data from this object, a larger amount of data may be |
| 745 | requested from the underlying raw stream, and kept in an internal buffer. |
| 746 | The buffered data can then be returned directly on subsequent reads. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 747 | |
| 748 | The constructor creates a :class:`BufferedReader` for the given readable |
| 749 | *raw* stream and *buffer_size*. If *buffer_size* is omitted, |
| 750 | :data:`DEFAULT_BUFFER_SIZE` is used. |
| 751 | |
| 752 | :class:`BufferedReader` provides or overrides these methods in addition to |
| 753 | those from :class:`BufferedIOBase` and :class:`IOBase`: |
| 754 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 755 | .. method:: peek([size]) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 756 | |
| Benjamin Peterson | c43a26d | 2009-06-16 23:09:24 +0000 | [diff] [blame] | 757 | Return bytes from the stream without advancing the position. At most one |
| Benjamin Peterson | 2a8b54d | 2009-06-14 14:37:23 +0000 | [diff] [blame] | 758 | single read on the raw stream is done to satisfy the call. The number of |
| 759 | bytes returned may be less or more than requested. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 760 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 761 | .. method:: read([size]) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 762 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 763 | Read and return *size* bytes, or if *size* is not given or negative, until |
| 764 | EOF or if the read call would block in non-blocking mode. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 765 | |
| Martin Panter | ccb2c0e | 2016-10-20 23:48:14 +0000 | [diff] [blame] | 766 | .. method:: read1([size]) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 767 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 768 | Read and return up to *size* bytes with only one call on the raw stream. |
| 769 | If at least one byte is buffered, only buffered bytes are returned. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 770 | Otherwise, one raw stream read call is made. |
| 771 | |
| Martin Panter | ccb2c0e | 2016-10-20 23:48:14 +0000 | [diff] [blame] | 772 | .. versionchanged:: 3.7 |
| 773 | The *size* argument is now optional. |
| 774 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 775 | |
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 776 | .. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 777 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 778 | A buffered binary stream providing higher-level access to a writeable, non |
| 779 | seekable :class:`RawIOBase` raw binary stream. It inherits |
| 780 | :class:`BufferedIOBase`. |
| 781 | |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 782 | When writing to this object, data is normally placed into an internal |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 783 | buffer. The buffer will be written out to the underlying :class:`RawIOBase` |
| 784 | object under various conditions, including: |
| 785 | |
| 786 | * when the buffer gets too small for all pending data; |
| 787 | * when :meth:`flush()` is called; |
| 788 | * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects); |
| 789 | * when the :class:`BufferedWriter` object is closed or destroyed. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 790 | |
| 791 | The constructor creates a :class:`BufferedWriter` for the given writeable |
| 792 | *raw* stream. If the *buffer_size* is not given, it defaults to |
| Benjamin Peterson | 394ee00 | 2009-03-05 22:33:59 +0000 | [diff] [blame] | 793 | :data:`DEFAULT_BUFFER_SIZE`. |
| 794 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 795 | :class:`BufferedWriter` provides or overrides these methods in addition to |
| 796 | those from :class:`BufferedIOBase` and :class:`IOBase`: |
| 797 | |
| 798 | .. method:: flush() |
| 799 | |
| 800 | Force bytes held in the buffer into the raw stream. A |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 801 | :exc:`BlockingIOError` should be raised if the raw stream blocks. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 802 | |
| 803 | .. method:: write(b) |
| 804 | |
| Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 805 | Write the :term:`bytes-like object`, *b*, and return the |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 806 | number of bytes written. When in non-blocking mode, a |
| 807 | :exc:`BlockingIOError` is raised if the buffer needs to be written out but |
| 808 | the raw stream blocks. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 809 | |
| 810 | |
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 811 | .. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 812 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 813 | A buffered binary stream providing higher-level access to a seekable |
| 814 | :class:`RawIOBase` raw binary stream. It inherits :class:`BufferedReader` |
| 815 | and :class:`BufferedWriter`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 816 | |
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 817 | The constructor creates a reader and writer for a seekable raw stream, given |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 818 | in the first argument. If the *buffer_size* is omitted it defaults to |
| Benjamin Peterson | 394ee00 | 2009-03-05 22:33:59 +0000 | [diff] [blame] | 819 | :data:`DEFAULT_BUFFER_SIZE`. |
| 820 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 821 | :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or |
| Christopher Head | b13552c | 2019-04-12 08:50:41 -0700 | [diff] [blame] | 822 | :class:`BufferedWriter` can do. In addition, :meth:`seek` and :meth:`tell` |
| 823 | are guaranteed to be implemented. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 824 | |
| 825 | |
| Antoine Pitrou | 13d2895 | 2011-08-20 19:48:43 +0200 | [diff] [blame] | 826 | .. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE) |
| 827 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 828 | A buffered binary stream providing higher-level access to two non seekable |
| 829 | :class:`RawIOBase` raw binary streams---one readable, the other writeable. |
| 830 | It inherits :class:`BufferedIOBase`. |
| Antoine Pitrou | 13d2895 | 2011-08-20 19:48:43 +0200 | [diff] [blame] | 831 | |
| 832 | *reader* and *writer* are :class:`RawIOBase` objects that are readable and |
| 833 | writeable respectively. If the *buffer_size* is omitted it defaults to |
| 834 | :data:`DEFAULT_BUFFER_SIZE`. |
| 835 | |
| Antoine Pitrou | 13d2895 | 2011-08-20 19:48:43 +0200 | [diff] [blame] | 836 | :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods |
| 837 | except for :meth:`~BufferedIOBase.detach`, which raises |
| 838 | :exc:`UnsupportedOperation`. |
| 839 | |
| 840 | .. warning:: |
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 841 | |
| Antoine Pitrou | 13d2895 | 2011-08-20 19:48:43 +0200 | [diff] [blame] | 842 | :class:`BufferedRWPair` does not attempt to synchronize accesses to |
| 843 | its underlying raw streams. You should not pass it the same object |
| 844 | as reader and writer; use :class:`BufferedRandom` instead. |
| 845 | |
| 846 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 847 | Text I/O |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 848 | ^^^^^^^^ |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 849 | |
| 850 | .. class:: TextIOBase |
| 851 | |
| 852 | Base class for text streams. This class provides a character and line based |
| Miss Islington (bot) | bdce188 | 2022-03-04 10:33:57 -0800 | [diff] [blame^] | 853 | interface to stream I/O. It inherits :class:`IOBase`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 854 | |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 855 | :class:`TextIOBase` provides or overrides these data attributes and |
| 856 | methods in addition to those from :class:`IOBase`: |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 857 | |
| 858 | .. attribute:: encoding |
| 859 | |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 860 | The name of the encoding used to decode the stream's bytes into |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 861 | strings, and to encode strings into bytes. |
| 862 | |
| Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 863 | .. attribute:: errors |
| 864 | |
| 865 | The error setting of the decoder or encoder. |
| 866 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 867 | .. attribute:: newlines |
| 868 | |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 869 | A string, a tuple of strings, or ``None``, indicating the newlines |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 870 | translated so far. Depending on the implementation and the initial |
| 871 | constructor flags, this may not be available. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 872 | |
| Benjamin Peterson | c609b6b | 2009-06-28 17:32:20 +0000 | [diff] [blame] | 873 | .. attribute:: buffer |
| 874 | |
| 875 | The underlying binary buffer (a :class:`BufferedIOBase` instance) that |
| 876 | :class:`TextIOBase` deals with. This is not part of the |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 877 | :class:`TextIOBase` API and may not exist in some implementations. |
| Benjamin Peterson | c609b6b | 2009-06-28 17:32:20 +0000 | [diff] [blame] | 878 | |
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 879 | .. method:: detach() |
| 880 | |
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 881 | Separate the underlying binary buffer from the :class:`TextIOBase` and |
| 882 | return it. |
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 883 | |
| 884 | After the underlying buffer has been detached, the :class:`TextIOBase` is |
| 885 | in an unusable state. |
| 886 | |
| 887 | Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not |
| 888 | have the concept of an underlying buffer and calling this method will |
| 889 | raise :exc:`UnsupportedOperation`. |
| 890 | |
| Benjamin Peterson | edc3647 | 2009-05-01 20:48:14 +0000 | [diff] [blame] | 891 | .. versionadded:: 3.1 |
| 892 | |
| Andrés Delfino | b6bb77c | 2018-07-07 17:17:16 -0300 | [diff] [blame] | 893 | .. method:: read(size=-1) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 894 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 895 | Read and return at most *size* characters from the stream as a single |
| 896 | :class:`str`. If *size* is negative or ``None``, reads until EOF. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 897 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 898 | .. method:: readline(size=-1) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 899 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 900 | Read until newline or EOF and return a single ``str``. If the stream is |
| 901 | already at EOF, an empty string is returned. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 902 | |
| Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 903 | If *size* is specified, at most *size* characters will be read. |
| Antoine Pitrou | 707bd4e | 2012-07-25 22:38:33 +0200 | [diff] [blame] | 904 | |
| Benjamin Peterson | 2a3d4d9 | 2019-07-10 19:43:04 -0700 | [diff] [blame] | 905 | .. method:: seek(offset, whence=SEEK_SET) |
| Antoine Pitrou | f49d152 | 2012-01-21 20:20:49 +0100 | [diff] [blame] | 906 | |
| Martin Panter | db4220e | 2015-09-11 03:58:30 +0000 | [diff] [blame] | 907 | Change the stream position to the given *offset*. Behaviour depends on |
| 908 | the *whence* parameter. The default value for *whence* is |
| 909 | :data:`SEEK_SET`. |
| Antoine Pitrou | f49d152 | 2012-01-21 20:20:49 +0100 | [diff] [blame] | 910 | |
| 911 | * :data:`SEEK_SET` or ``0``: seek from the start of the stream |
| 912 | (the default); *offset* must either be a number returned by |
| 913 | :meth:`TextIOBase.tell`, or zero. Any other *offset* value |
| 914 | produces undefined behaviour. |
| 915 | * :data:`SEEK_CUR` or ``1``: "seek" to the current position; |
| 916 | *offset* must be zero, which is a no-operation (all other values |
| 917 | are unsupported). |
| 918 | * :data:`SEEK_END` or ``2``: seek to the end of the stream; |
| 919 | *offset* must be zero (all other values are unsupported). |
| 920 | |
| 921 | Return the new absolute position as an opaque number. |
| 922 | |
| 923 | .. versionadded:: 3.1 |
| 924 | The ``SEEK_*`` constants. |
| 925 | |
| 926 | .. method:: tell() |
| 927 | |
| 928 | Return the current stream position as an opaque number. The number |
| 929 | does not usually represent a number of bytes in the underlying |
| 930 | binary storage. |
| 931 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 932 | .. method:: write(s) |
| 933 | |
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 934 | Write the string *s* to the stream and return the number of characters |
| 935 | written. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 936 | |
| 937 | |
| Antoine Pitrou | 664091b | 2011-07-23 22:00:03 +0200 | [diff] [blame] | 938 | .. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, \ |
| 939 | line_buffering=False, write_through=False) |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 940 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 941 | A buffered text stream providing higher-level access to a |
| 942 | :class:`BufferedIOBase` buffered binary stream. It inherits |
| 943 | :class:`TextIOBase`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 944 | |
| 945 | *encoding* gives the name of the encoding that the stream will be decoded or |
| Andrew Svetlov | 4805fa8 | 2012-08-13 22:11:14 +0300 | [diff] [blame] | 946 | encoded with. It defaults to |
| 947 | :func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`. |
| Inada Naoki | 4827483 | 2021-03-29 12:28:14 +0900 | [diff] [blame] | 948 | ``encoding="locale"`` can be used to specify the current locale's encoding |
| 949 | explicitly. See :ref:`io-text-encoding` for more information. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 950 | |
| Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 951 | *errors* is an optional string that specifies how encoding and decoding |
| 952 | errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError` |
| 953 | exception if there is an encoding error (the default of ``None`` has the same |
| 954 | effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding |
| 955 | errors can lead to data loss.) ``'replace'`` causes a replacement marker |
| Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 956 | (such as ``'?'``) to be inserted where there is malformed data. |
| 957 | ``'backslashreplace'`` causes malformed data to be replaced by a |
| 958 | backslashed escape sequence. When writing, ``'xmlcharrefreplace'`` |
| 959 | (replace with the appropriate XML character reference) or ``'namereplace'`` |
| 960 | (replace with ``\N{...}`` escape sequences) can be used. Any other error |
| 961 | handling name that has been registered with |
| Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 962 | :func:`codecs.register_error` is also valid. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 963 | |
| R David Murray | 1b00f25 | 2012-08-15 10:43:58 -0400 | [diff] [blame] | 964 | .. index:: |
| 965 | single: universal newlines; io.TextIOWrapper class |
| 966 | |
| Antoine Pitrou | 0c1c0d4 | 2012-08-04 00:55:38 +0200 | [diff] [blame] | 967 | *newline* controls how line endings are handled. It can be ``None``, |
| 968 | ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows: |
| 969 | |
| R David Murray | 1b00f25 | 2012-08-15 10:43:58 -0400 | [diff] [blame] | 970 | * When reading input from the stream, if *newline* is ``None``, |
| R David Murray | ee0a945 | 2012-08-15 11:05:36 -0400 | [diff] [blame] | 971 | :term:`universal newlines` mode is enabled. Lines in the input can end in |
| 972 | ``'\n'``, ``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'`` |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 973 | before being returned to the caller. If *newline* is ``''``, universal |
| 974 | newlines mode is enabled, but line endings are returned to the caller |
| 975 | untranslated. If *newline* has any of the other legal values, input lines |
| 976 | are only terminated by the given string, and the line ending is returned to |
| 977 | the caller untranslated. |
| Antoine Pitrou | 0c1c0d4 | 2012-08-04 00:55:38 +0200 | [diff] [blame] | 978 | |
| Georg Brandl | 296d1be | 2012-08-14 09:39:07 +0200 | [diff] [blame] | 979 | * When writing output to the stream, if *newline* is ``None``, any ``'\n'`` |
| 980 | characters written are translated to the system default line separator, |
| 981 | :data:`os.linesep`. If *newline* is ``''`` or ``'\n'``, no translation |
| 982 | takes place. If *newline* is any of the other legal values, any ``'\n'`` |
| 983 | characters written are translated to the given string. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 984 | |
| 985 | If *line_buffering* is ``True``, :meth:`flush` is implied when a call to |
| Elena Oat | 7ffd4c5 | 2018-05-14 17:48:01 +0300 | [diff] [blame] | 986 | write contains a newline character or a carriage return. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 987 | |
| Antoine Pitrou | 664091b | 2011-07-23 22:00:03 +0200 | [diff] [blame] | 988 | If *write_through* is ``True``, calls to :meth:`write` are guaranteed |
| 989 | not to be buffered: any data written on the :class:`TextIOWrapper` |
| 990 | object is immediately handled to its underlying binary *buffer*. |
| 991 | |
| 992 | .. versionchanged:: 3.3 |
| 993 | The *write_through* argument has been added. |
| 994 | |
| Victor Stinner | f86a5e8 | 2012-06-05 13:43:22 +0200 | [diff] [blame] | 995 | .. versionchanged:: 3.3 |
| 996 | The default *encoding* is now ``locale.getpreferredencoding(False)`` |
| 997 | instead of ``locale.getpreferredencoding()``. Don't change temporary the |
| 998 | locale encoding using :func:`locale.setlocale`, use the current locale |
| 999 | encoding instead of the user preferred encoding. |
| 1000 | |
| Inada Naoki | 4827483 | 2021-03-29 12:28:14 +0900 | [diff] [blame] | 1001 | .. versionchanged:: 3.10 |
| 1002 | The *encoding* argument now supports the ``"locale"`` dummy encoding name. |
| 1003 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 1004 | :class:`TextIOWrapper` provides these data attributes and methods in |
| 1005 | addition to those from :class:`TextIOBase` and :class:`IOBase`: |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1006 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1007 | .. attribute:: line_buffering |
| 1008 | |
| 1009 | Whether line buffering is enabled. |
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1010 | |
| Antoine Pitrou | 3c2817b | 2017-06-03 12:32:28 +0200 | [diff] [blame] | 1011 | .. attribute:: write_through |
| 1012 | |
| 1013 | Whether writes are passed immediately to the underlying binary |
| 1014 | buffer. |
| 1015 | |
| 1016 | .. versionadded:: 3.7 |
| 1017 | |
| INADA Naoki | 507434f | 2017-12-21 09:59:53 +0900 | [diff] [blame] | 1018 | .. method:: reconfigure(*[, encoding][, errors][, newline][, \ |
| 1019 | line_buffering][, write_through]) |
| Antoine Pitrou | 3c2817b | 2017-06-03 12:32:28 +0200 | [diff] [blame] | 1020 | |
| INADA Naoki | 507434f | 2017-12-21 09:59:53 +0900 | [diff] [blame] | 1021 | Reconfigure this text stream using new settings for *encoding*, |
| 1022 | *errors*, *newline*, *line_buffering* and *write_through*. |
| 1023 | |
| 1024 | Parameters not specified keep current settings, except |
| Harmon | 35068bd | 2019-06-19 16:01:27 -0500 | [diff] [blame] | 1025 | ``errors='strict'`` is used when *encoding* is specified but |
| INADA Naoki | 507434f | 2017-12-21 09:59:53 +0900 | [diff] [blame] | 1026 | *errors* is not specified. |
| 1027 | |
| 1028 | It is not possible to change the encoding or newline if some data |
| 1029 | has already been read from the stream. On the other hand, changing |
| 1030 | encoding after write is possible. |
| Antoine Pitrou | 3c2817b | 2017-06-03 12:32:28 +0200 | [diff] [blame] | 1031 | |
| 1032 | This method does an implicit stream flush before setting the |
| 1033 | new parameters. |
| 1034 | |
| 1035 | .. versionadded:: 3.7 |
| 1036 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1037 | |
| Julien Palard | 5c1f15b | 2021-01-25 15:46:06 +0100 | [diff] [blame] | 1038 | .. class:: StringIO(initial_value='', newline='\\n') |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1039 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 1040 | A text stream using an in-memory text buffer. It inherits |
| 1041 | :class:`TextIOBase`. |
| 1042 | |
| 1043 | The text buffer is discarded when the :meth:`~IOBase.close` method is |
| 1044 | called. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1045 | |
| Martin Panter | cfad543 | 2015-10-10 03:01:20 +0000 | [diff] [blame] | 1046 | The initial value of the buffer can be set by providing *initial_value*. |
| 1047 | If newline translation is enabled, newlines will be encoded as if by |
| 1048 | :meth:`~TextIOBase.write`. The stream is positioned at the start of |
| 1049 | the buffer. |
| 1050 | |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 1051 | The *newline* argument works like that of :class:`TextIOWrapper`, |
| 1052 | except that when writing output to the stream, if *newline* is ``None``, |
| 1053 | newlines are written as ``\n`` on all platforms. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1054 | |
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 1055 | :class:`StringIO` provides this method in addition to those from |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 1056 | :class:`TextIOBase` and :class:`IOBase`: |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1057 | |
| 1058 | .. method:: getvalue() |
| 1059 | |
| Serhiy Storchaka | c057c38 | 2015-02-03 02:00:18 +0200 | [diff] [blame] | 1060 | Return a ``str`` containing the entire contents of the buffer. |
| Martin Panter | cfad543 | 2015-10-10 03:01:20 +0000 | [diff] [blame] | 1061 | Newlines are decoded as if by :meth:`~TextIOBase.read`, although |
| 1062 | the stream position is not changed. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1063 | |
| Georg Brandl | 2932d93 | 2008-05-30 06:27:09 +0000 | [diff] [blame] | 1064 | Example usage:: |
| 1065 | |
| 1066 | import io |
| 1067 | |
| 1068 | output = io.StringIO() |
| 1069 | output.write('First line.\n') |
| 1070 | print('Second line.', file=output) |
| 1071 | |
| 1072 | # Retrieve file contents -- this will be |
| 1073 | # 'First line.\nSecond line.\n' |
| 1074 | contents = output.getvalue() |
| 1075 | |
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1076 | # Close object and discard memory buffer -- |
| Georg Brandl | 2932d93 | 2008-05-30 06:27:09 +0000 | [diff] [blame] | 1077 | # .getvalue() will now raise an exception. |
| 1078 | output.close() |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1079 | |
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 1080 | |
| R David Murray | 1b00f25 | 2012-08-15 10:43:58 -0400 | [diff] [blame] | 1081 | .. index:: |
| 1082 | single: universal newlines; io.IncrementalNewlineDecoder class |
| 1083 | |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1084 | .. class:: IncrementalNewlineDecoder |
| 1085 | |
| R David Murray | 1b00f25 | 2012-08-15 10:43:58 -0400 | [diff] [blame] | 1086 | A helper codec that decodes newlines for :term:`universal newlines` mode. |
| 1087 | It inherits :class:`codecs.IncrementalDecoder`. |
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1088 | |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 1089 | |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 1090 | Performance |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1091 | ----------- |
| 1092 | |
| 1093 | This section discusses the performance of the provided concrete I/O |
| 1094 | implementations. |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 1095 | |
| 1096 | Binary I/O |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1097 | ^^^^^^^^^^ |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 1098 | |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1099 | By reading and writing only large chunks of data even when the user asks for a |
| 1100 | single byte, buffered I/O hides any inefficiency in calling and executing the |
| 1101 | operating system's unbuffered I/O routines. The gain depends on the OS and the |
| 1102 | kind of I/O which is performed. For example, on some modern OSes such as Linux, |
| 1103 | unbuffered disk I/O can be as fast as buffered I/O. The bottom line, however, |
| 1104 | is that buffered I/O offers predictable performance regardless of the platform |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 1105 | and the backing device. Therefore, it is almost always preferable to use |
| 1106 | buffered I/O rather than unbuffered I/O for binary data. |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 1107 | |
| 1108 | Text I/O |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1109 | ^^^^^^^^ |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 1110 | |
| 1111 | Text I/O over a binary storage (such as a file) is significantly slower than |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1112 | binary I/O over the same storage, because it requires conversions between |
| 1113 | unicode and binary data using a character codec. This can become noticeable |
| 1114 | handling huge amounts of text data like large log files. Also, |
| 1115 | :meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both quite slow |
| 1116 | due to the reconstruction algorithm used. |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 1117 | |
| 1118 | :class:`StringIO`, however, is a native in-memory unicode container and will |
| 1119 | exhibit similar speed to :class:`BytesIO`. |
| 1120 | |
| 1121 | Multi-threading |
| 1122 | ^^^^^^^^^^^^^^^ |
| 1123 | |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1124 | :class:`FileIO` objects are thread-safe to the extent that the operating system |
| 1125 | calls (such as ``read(2)`` under Unix) they wrap are thread-safe too. |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 1126 | |
| 1127 | Binary buffered objects (instances of :class:`BufferedReader`, |
| 1128 | :class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`) |
| 1129 | protect their internal structures using a lock; it is therefore safe to call |
| 1130 | them from multiple threads at once. |
| 1131 | |
| 1132 | :class:`TextIOWrapper` objects are not thread-safe. |
| 1133 | |
| 1134 | Reentrancy |
| 1135 | ^^^^^^^^^^ |
| 1136 | |
| 1137 | Binary buffered objects (instances of :class:`BufferedReader`, |
| 1138 | :class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`) |
| 1139 | are not reentrant. While reentrant calls will not happen in normal situations, |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1140 | they can arise from doing I/O in a :mod:`signal` handler. If a thread tries to |
| Eli Bendersky | f877a7c | 2012-07-14 21:22:25 +0300 | [diff] [blame] | 1141 | re-enter a buffered object which it is already accessing, a :exc:`RuntimeError` |
| 1142 | is raised. Note this doesn't prohibit a different thread from entering the |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1143 | buffered object. |
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 1144 | |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1145 | The above implicitly extends to text files, since the :func:`open()` function |
| 1146 | will wrap a buffered object inside a :class:`TextIOWrapper`. This includes |
| Géry Ogam | 3b58a70 | 2019-09-11 16:55:13 +0200 | [diff] [blame] | 1147 | standard streams and therefore affects the built-in :func:`print()` function as |
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 1148 | well. |