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