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