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