| 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 |  | 
 | 14 | The :mod:`io` module provides the Python interfaces to stream handling.  The | 
| Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 15 | built-in :func:`open` function is defined in this module. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 16 |  | 
 | 17 | At the top of the I/O hierarchy is the abstract base class :class:`IOBase`.  It | 
 | 18 | defines the basic interface to a stream.  Note, however, that there is no | 
| Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 19 | separation between reading and writing to streams; implementations are allowed | 
| Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 20 | to raise an :exc:`IOError` if they do not support a given operation. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 21 |  | 
 | 22 | Extending :class:`IOBase` is :class:`RawIOBase` which deals simply with the | 
 | 23 | reading and writing of raw bytes to a stream.  :class:`FileIO` subclasses | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 24 | :class:`RawIOBase` to provide an interface to files in the machine's | 
 | 25 | file system. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 26 |  | 
 | 27 | :class:`BufferedIOBase` deals with buffering on a raw byte stream | 
 | 28 | (:class:`RawIOBase`).  Its subclasses, :class:`BufferedWriter`, | 
 | 29 | :class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 30 | readable, writable, and both readable and writable. | 
 | 31 | :class:`BufferedRandom` provides a buffered interface to random access | 
 | 32 | streams.  :class:`BytesIO` is a simple stream of in-memory bytes. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 33 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 34 | Another :class:`IOBase` subclass, :class:`TextIOBase`, deals with | 
 | 35 | streams whose bytes represent text, and handles encoding and decoding | 
 | 36 | from and to strings. :class:`TextIOWrapper`, which extends it, is a | 
 | 37 | buffered text interface to a buffered raw stream | 
 | 38 | (:class:`BufferedIOBase`). Finally, :class:`StringIO` is an in-memory | 
 | 39 | stream for text. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 40 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 41 | Argument names are not part of the specification, and only the arguments of | 
| Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 42 | :func:`.open` are intended to be used as keyword arguments. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 43 |  | 
| Benjamin Peterson | cc12e1b | 2010-02-19 00:58:13 +0000 | [diff] [blame] | 44 | .. seealso:: | 
 | 45 |    :mod:`sys` | 
| Benjamin Peterson | 2c2dc37 | 2010-02-19 01:05:32 +0000 | [diff] [blame] | 46 |        contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`, | 
| Benjamin Peterson | cc12e1b | 2010-02-19 00:58:13 +0000 | [diff] [blame] | 47 |        and :data:`sys.stderr`. | 
 | 48 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 49 |  | 
 | 50 | Module Interface | 
 | 51 | ---------------- | 
 | 52 |  | 
 | 53 | .. data:: DEFAULT_BUFFER_SIZE | 
 | 54 |  | 
 | 55 |    An int containing the default buffer size used by the module's buffered I/O | 
| Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 56 |    classes.  :func:`.open` uses the file's blksize (as obtained by | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 57 |    :func:`os.stat`) if possible. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 58 |  | 
| Benjamin Peterson | 95e392c | 2010-04-27 21:07:21 +0000 | [diff] [blame] | 59 | .. 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] | 60 |  | 
| Benjamin Peterson | 52c3bf1 | 2009-03-23 02:44:58 +0000 | [diff] [blame] | 61 |    Open *file* and return a corresponding stream.  If the file cannot be opened, | 
 | 62 |    an :exc:`IOError` is raised. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 63 |  | 
| Georg Brandl | 70f355d | 2010-07-19 06:52:35 +0000 | [diff] [blame] | 64 |    *file* is either a string or bytes object giving the pathname (absolute or | 
 | 65 |    relative to the current working directory) of the file to be opened or | 
| Benjamin Peterson | 52c3bf1 | 2009-03-23 02:44:58 +0000 | [diff] [blame] | 66 |    an integer file descriptor of the file to be wrapped.  (If a file descriptor | 
 | 67 |    is given, it is closed when the returned I/O object is closed, unless | 
 | 68 |    *closefd* is set to ``False``.) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 69 |  | 
| Benjamin Peterson | dd21912 | 2008-04-11 21:17:32 +0000 | [diff] [blame] | 70 |    *mode* is an optional string that specifies the mode in which the file is | 
 | 71 |    opened.  It defaults to ``'r'`` which means open for reading in text mode. | 
 | 72 |    Other common values are ``'w'`` for writing (truncating the file if it | 
 | 73 |    already exists), and ``'a'`` for appending (which on *some* Unix systems, | 
 | 74 |    means that *all* writes append to the end of the file regardless of the | 
 | 75 |    current seek position).  In text mode, if *encoding* is not specified the | 
 | 76 |    encoding used is platform dependent. (For reading and writing raw bytes use | 
 | 77 |    binary mode and leave *encoding* unspecified.)  The available modes are: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 78 |  | 
 | 79 |    ========= =============================================================== | 
 | 80 |    Character Meaning | 
 | 81 |    --------- --------------------------------------------------------------- | 
 | 82 |    ``'r'``   open for reading (default) | 
 | 83 |    ``'w'``   open for writing, truncating the file first | 
 | 84 |    ``'a'``   open for writing, appending to the end of the file if it exists | 
 | 85 |    ``'b'``   binary mode | 
 | 86 |    ``'t'``   text mode (default) | 
 | 87 |    ``'+'``   open a disk file for updating (reading and writing) | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 88 |    ``'U'``   universal newline mode (for backwards compatibility; should | 
 | 89 |              not be used in new code) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 90 |    ========= =============================================================== | 
 | 91 |  | 
 | 92 |    The default mode is ``'rt'`` (open for reading text).  For binary random | 
 | 93 |    access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while | 
 | 94 |    ``'r+b'`` opens the file without truncation. | 
 | 95 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 96 |    Python distinguishes between files opened in binary and text modes, even when | 
 | 97 |    the underlying operating system doesn't.  Files opened in binary mode | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 98 |    (including ``'b'`` in the *mode* argument) return contents as ``bytes`` | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 99 |    objects without any decoding.  In text mode (the default, or when ``'t'`` is | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 100 |    included in the *mode* argument), the contents of the file are returned as | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 101 |    strings, the bytes having been first decoded using a platform-dependent | 
 | 102 |    encoding or using the specified *encoding* if given. | 
| Benjamin Peterson | dd21912 | 2008-04-11 21:17:32 +0000 | [diff] [blame] | 103 |  | 
| Antoine Pitrou | d5587bc | 2009-12-19 21:08:31 +0000 | [diff] [blame] | 104 |    *buffering* is an optional integer used to set the buffering policy. | 
 | 105 |    Pass 0 to switch buffering off (only allowed in binary mode), 1 to select | 
 | 106 |    line buffering (only usable in text mode), and an integer > 1 to indicate | 
 | 107 |    the size of a fixed-size chunk buffer.  When no *buffering* argument is | 
 | 108 |    given, the default buffering policy works as follows: | 
 | 109 |  | 
 | 110 |    * Binary files are buffered in fixed-size chunks; the size of the buffer | 
 | 111 |      is chosen using a heuristic trying to determine the underlying device's | 
 | 112 |      "block size" and falling back on :attr:`DEFAULT_BUFFER_SIZE`. | 
 | 113 |      On many systems, the buffer will typically be 4096 or 8192 bytes long. | 
 | 114 |  | 
 | 115 |    * "Interactive" text files (files for which :meth:`isatty` returns True) | 
 | 116 |      use line buffering.  Other text files use the policy described above | 
 | 117 |      for binary files. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 118 |  | 
 | 119 |    *encoding* is the name of the encoding used to decode or encode the file. | 
| Benjamin Peterson | dd21912 | 2008-04-11 21:17:32 +0000 | [diff] [blame] | 120 |    This should only be used in text mode.  The default encoding is platform | 
| Benjamin Peterson | 52c3bf1 | 2009-03-23 02:44:58 +0000 | [diff] [blame] | 121 |    dependent (whatever :func:`locale.getpreferredencoding` returns), but any | 
 | 122 |    encoding supported by Python can be used.  See the :mod:`codecs` module for | 
 | 123 |    the list of supported encodings. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 124 |  | 
| Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 125 |    *errors* is an optional string that specifies how encoding and decoding | 
| Benjamin Peterson | 52c3bf1 | 2009-03-23 02:44:58 +0000 | [diff] [blame] | 126 |    errors are to be handled--this cannot be used in binary mode.  Pass | 
 | 127 |    ``'strict'`` to raise a :exc:`ValueError` exception if there is an encoding | 
 | 128 |    error (the default of ``None`` has the same effect), or pass ``'ignore'`` to | 
 | 129 |    ignore errors.  (Note that ignoring encoding errors can lead to data loss.) | 
 | 130 |    ``'replace'`` causes a replacement marker (such as ``'?'``) to be inserted | 
 | 131 |    where there is malformed data.  When writing, ``'xmlcharrefreplace'`` | 
 | 132 |    (replace with the appropriate XML character reference) or | 
 | 133 |    ``'backslashreplace'`` (replace with backslashed escape sequences) can be | 
 | 134 |    used.  Any other error handling name that has been registered with | 
 | 135 |    :func:`codecs.register_error` is also valid. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 136 |  | 
 | 137 |    *newline* controls how universal newlines works (it only applies to text | 
 | 138 |    mode).  It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``.  It | 
 | 139 |    works as follows: | 
 | 140 |  | 
 | 141 |    * On input, if *newline* is ``None``, universal newlines mode is enabled. | 
 | 142 |      Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``, and these | 
 | 143 |      are translated into ``'\n'`` before being returned to the caller.  If it is | 
 | 144 |      ``''``, universal newline mode is enabled, but line endings are returned to | 
 | 145 |      the caller untranslated.  If it has any of the other legal values, input | 
 | 146 |      lines are only terminated by the given string, and the line ending is | 
 | 147 |      returned to the caller untranslated. | 
 | 148 |  | 
 | 149 |    * On output, if *newline* is ``None``, any ``'\n'`` characters written are | 
 | 150 |      translated to the system default line separator, :data:`os.linesep`.  If | 
 | 151 |      *newline* is ``''``, no translation takes place.  If *newline* is any of | 
 | 152 |      the other legal values, any ``'\n'`` characters written are translated to | 
 | 153 |      the given string. | 
 | 154 |  | 
| Benjamin Peterson | 8cad9c7 | 2009-03-23 02:38:01 +0000 | [diff] [blame] | 155 |    If *closefd* is ``False`` and a file descriptor rather than a filename was | 
 | 156 |    given, the underlying file descriptor will be kept open when the file is | 
 | 157 |    closed.  If a filename is given *closefd* has no effect and must be ``True`` | 
 | 158 |    (the default). | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 159 |  | 
| Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 160 |    The type of file object returned by the :func:`.open` function depends on the | 
 | 161 |    mode.  When :func:`.open` is used to open a file in a text mode (``'w'``, | 
| Benjamin Peterson | 8cad9c7 | 2009-03-23 02:38:01 +0000 | [diff] [blame] | 162 |    ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a subclass of | 
 | 163 |    :class:`TextIOBase` (specifically :class:`TextIOWrapper`).  When used to open | 
 | 164 |    a file in a binary mode with buffering, the returned class is a subclass of | 
 | 165 |    :class:`BufferedIOBase`.  The exact class varies: in read binary mode, it | 
 | 166 |    returns a :class:`BufferedReader`; in write binary and append binary modes, | 
 | 167 |    it returns a :class:`BufferedWriter`, and in read/write mode, it returns a | 
 | 168 |    :class:`BufferedRandom`.  When buffering is disabled, the raw stream, a | 
 | 169 |    subclass of :class:`RawIOBase`, :class:`FileIO`, is returned. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 170 |  | 
 | 171 |    It is also possible to use a string or bytearray as a file for both reading | 
| Benjamin Peterson | dd21912 | 2008-04-11 21:17:32 +0000 | [diff] [blame] | 172 |    and writing.  For strings :class:`StringIO` can be used like a file opened in | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 173 |    a text mode, and for bytearrays a :class:`BytesIO` can be used like a | 
 | 174 |    file opened in a binary mode. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 175 |  | 
 | 176 |  | 
 | 177 | .. exception:: BlockingIOError | 
 | 178 |  | 
 | 179 |    Error raised when blocking would occur on a non-blocking stream.  It inherits | 
 | 180 |    :exc:`IOError`. | 
 | 181 |  | 
 | 182 |    In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one | 
 | 183 |    attribute: | 
 | 184 |  | 
 | 185 |    .. attribute:: characters_written | 
 | 186 |  | 
 | 187 |       An integer containing the number of characters written to the stream | 
 | 188 |       before it blocked. | 
 | 189 |  | 
 | 190 |  | 
 | 191 | .. exception:: UnsupportedOperation | 
 | 192 |  | 
 | 193 |    An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised | 
 | 194 |    when an unsupported operation is called on a stream. | 
 | 195 |  | 
 | 196 |  | 
 | 197 | I/O Base Classes | 
 | 198 | ---------------- | 
 | 199 |  | 
 | 200 | .. class:: IOBase | 
 | 201 |  | 
 | 202 |    The abstract base class for all I/O classes, acting on streams of bytes. | 
 | 203 |    There is no public constructor. | 
 | 204 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 205 |    This class provides empty abstract implementations for many methods | 
 | 206 |    that derived classes can override selectively; the default | 
 | 207 |    implementations represent a file that cannot be read, written or | 
 | 208 |    seeked. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 209 |  | 
 | 210 |    Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`, | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 211 |    or :meth:`write` because their signatures will vary, implementations and | 
 | 212 |    clients should consider those methods part of the interface.  Also, | 
 | 213 |    implementations may raise a :exc:`IOError` when operations they do not | 
 | 214 |    support are called. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 215 |  | 
 | 216 |    The basic type used for binary data read from or written to a file is | 
 | 217 |    :class:`bytes`.  :class:`bytearray`\s are accepted too, and in some cases | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 218 |    (such as :class:`readinto`) required.  Text I/O classes work with | 
 | 219 |    :class:`str` data. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 220 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 221 |    Note that calling any method (even inquiries) on a closed stream is | 
 | 222 |    undefined.  Implementations may raise :exc:`IOError` in this case. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 223 |  | 
 | 224 |    IOBase (and its subclasses) support the iterator protocol, meaning that an | 
 | 225 |    :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] | 226 |    Lines are defined slightly differently depending on whether the stream is | 
 | 227 |    a binary stream (yielding bytes), or a text stream (yielding character | 
 | 228 |    strings).  See :meth:`readline` below. | 
| 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 |    IOBase is also a context manager and therefore supports the | 
 | 231 |    :keyword:`with` statement.  In this example, *file* is closed after the | 
 | 232 |    :keyword:`with` statement's suite is finished---even if an exception occurs:: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 233 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 234 |       with open('spam.txt', 'w') as file: | 
 | 235 |           file.write('Spam and eggs!') | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 236 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 237 |    :class:`IOBase` provides these data attributes and methods: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 238 |  | 
 | 239 |    .. method:: close() | 
 | 240 |  | 
| Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 241 |       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] | 242 |       already closed. Once the file is closed, any operation on the file | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 243 |       (e.g. reading or writing) will raise a :exc:`ValueError`. | 
| Antoine Pitrou | f9fc08f | 2010-04-28 19:59:32 +0000 | [diff] [blame] | 244 |  | 
 | 245 |       As a convenience, it is allowed to call this method more than once; | 
 | 246 |       only the first call, however, will have an effect. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 247 |  | 
 | 248 |    .. attribute:: closed | 
 | 249 |  | 
 | 250 |       True if the stream is closed. | 
 | 251 |  | 
 | 252 |    .. method:: fileno() | 
 | 253 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 254 |       Return the underlying file descriptor (an integer) of the stream if it | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 255 |       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] | 256 |       descriptor. | 
 | 257 |  | 
 | 258 |    .. method:: flush() | 
 | 259 |  | 
| Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 260 |       Flush the write buffers of the stream if applicable.  This does nothing | 
 | 261 |       for read-only and non-blocking streams. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 262 |  | 
 | 263 |    .. method:: isatty() | 
 | 264 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 265 |       Return ``True`` if the stream is interactive (i.e., connected to | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 266 |       a terminal/tty device). | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 267 |  | 
 | 268 |    .. method:: readable() | 
 | 269 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 270 |       Return ``True`` if the stream can be read from.  If False, :meth:`read` | 
 | 271 |       will raise :exc:`IOError`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 272 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 273 |    .. method:: readline(limit=-1) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 274 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 275 |       Read and return one line from the stream.  If *limit* is specified, at | 
 | 276 |       most *limit* bytes will be read. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 277 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 278 |       The line terminator is always ``b'\n'`` for binary files; for text files, | 
| Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 279 |       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] | 280 |       terminator(s) recognized. | 
 | 281 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 282 |    .. method:: readlines(hint=-1) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 283 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 284 |       Read and return a list of lines from the stream.  *hint* can be specified | 
 | 285 |       to control the number of lines read: no more lines will be read if the | 
 | 286 |       total size (in bytes/characters) of all lines so far exceeds *hint*. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 287 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 288 |    .. method:: seek(offset, whence=SEEK_SET) | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 289 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 290 |       Change the stream position to the given byte *offset*.  *offset* is | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 291 |       interpreted relative to the position indicated by *whence*.  Values for | 
 | 292 |       *whence* are: | 
 | 293 |  | 
| Benjamin Peterson | 0e4caf4 | 2009-04-01 21:22:20 +0000 | [diff] [blame] | 294 |       * :data:`SEEK_SET` or ``0`` -- start of the stream (the default); | 
 | 295 |         *offset* should be zero or positive | 
 | 296 |       * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may | 
 | 297 |         be negative | 
 | 298 |       * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually | 
 | 299 |         negative | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 300 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 301 |       Return the new absolute position. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 302 |  | 
| Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 303 |       .. versionadded:: 3.1 | 
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 304 |          The ``SEEK_*`` constants. | 
| Benjamin Peterson | 0e4caf4 | 2009-04-01 21:22:20 +0000 | [diff] [blame] | 305 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 306 |    .. method:: seekable() | 
 | 307 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 308 |       Return ``True`` if the stream supports random access.  If ``False``, | 
 | 309 |       :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 310 |  | 
 | 311 |    .. method:: tell() | 
 | 312 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 313 |       Return the current stream position. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 314 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 315 |    .. method:: truncate(size=None) | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 316 |  | 
| Antoine Pitrou | 2016dc9 | 2010-05-29 12:08:25 +0000 | [diff] [blame] | 317 |       Resize the stream to the given *size* in bytes (or the current position | 
 | 318 |       if *size* is not specified).  The current stream position isn't changed. | 
 | 319 |       This resizing can extend or reduce the current file size.  In case of | 
 | 320 |       extension, the contents of the new file area depend on the platform | 
 | 321 |       (on most systems, additional bytes are zero-filled, on Windows they're | 
 | 322 |       undetermined).  The new file size is returned. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 323 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 324 |    .. method:: writable() | 
 | 325 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 326 |       Return ``True`` if the stream supports writing.  If ``False``, | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 327 |       :meth:`write` and :meth:`truncate` will raise :exc:`IOError`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 328 |  | 
 | 329 |    .. method:: writelines(lines) | 
 | 330 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 331 |       Write a list of lines to the stream.  Line separators are not added, so it | 
 | 332 |       is usual for each of the lines provided to have a line separator at the | 
 | 333 |       end. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 334 |  | 
 | 335 |  | 
 | 336 | .. class:: RawIOBase | 
 | 337 |  | 
 | 338 |    Base class for raw binary I/O.  It inherits :class:`IOBase`.  There is no | 
 | 339 |    public constructor. | 
 | 340 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 341 |    Raw binary I/O typically provides low-level access to an underlying OS | 
 | 342 |    device or API, and does not try to encapsulate it in high-level primitives | 
 | 343 |    (this is left to Buffered I/O and Text I/O, described later in this page). | 
 | 344 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 345 |    In addition to the attributes and methods from :class:`IOBase`, | 
 | 346 |    RawIOBase provides the following methods: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 347 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 348 |    .. method:: read(n=-1) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 349 |  | 
| Antoine Pitrou | 78ddbe6 | 2009-10-01 16:24:45 +0000 | [diff] [blame] | 350 |       Read up to *n* bytes from the object and return them.  As a convenience, | 
 | 351 |       if *n* is unspecified or -1, :meth:`readall` is called.  Otherwise, | 
 | 352 |       only one system call is ever made.  Fewer than *n* bytes may be | 
 | 353 |       returned if the operating system call returns fewer than *n* bytes. | 
 | 354 |  | 
 | 355 |       If 0 bytes are returned, and *n* was not 0, this indicates end of file. | 
 | 356 |       If the object is in non-blocking mode and no bytes are available, | 
 | 357 |       ``None`` is returned. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 358 |  | 
| Benjamin Peterson | b47aace | 2008-04-09 21:38:38 +0000 | [diff] [blame] | 359 |    .. method:: readall() | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 360 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 361 |       Read and return all the bytes from the stream until EOF, using multiple | 
 | 362 |       calls to the stream if necessary. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 363 |  | 
 | 364 |    .. method:: readinto(b) | 
 | 365 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 366 |       Read up to len(b) bytes into bytearray *b* and return the number of bytes | 
 | 367 |       read. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 368 |  | 
 | 369 |    .. method:: write(b) | 
 | 370 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 371 |       Write the given bytes or bytearray object, *b*, to the underlying raw | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 372 |       stream and return the number of bytes written.  This can be less than | 
 | 373 |       ``len(b)``, depending on specifics of the underlying raw stream, and | 
 | 374 |       especially if it is in non-blocking mode.  ``None`` is returned if the | 
 | 375 |       raw stream is set not to block and no single byte could be readily | 
 | 376 |       written to it. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 377 |  | 
 | 378 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 379 | .. class:: BufferedIOBase | 
 | 380 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 381 |    Base class for binary streams that support some kind of buffering. | 
 | 382 |    It inherits :class:`IOBase`. There is no public constructor. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 383 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 384 |    The main difference with :class:`RawIOBase` is that methods :meth:`read`, | 
 | 385 |    :meth:`readinto` and :meth:`write` will try (respectively) to read as much | 
 | 386 |    input as requested or to consume all given output, at the expense of | 
 | 387 |    making perhaps more than one system call. | 
 | 388 |  | 
 | 389 |    In addition, those methods can raise :exc:`BlockingIOError` if the | 
 | 390 |    underlying raw stream is in non-blocking mode and cannot take or give | 
 | 391 |    enough data; unlike their :class:`RawIOBase` counterparts, they will | 
 | 392 |    never return ``None``. | 
 | 393 |  | 
 | 394 |    Besides, the :meth:`read` method does not have a default | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 395 |    implementation that defers to :meth:`readinto`. | 
 | 396 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 397 |    A typical :class:`BufferedIOBase` implementation should not inherit from a | 
 | 398 |    :class:`RawIOBase` implementation, but wrap one, like | 
 | 399 |    :class:`BufferedWriter` and :class:`BufferedReader` do. | 
| 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 |    :class:`BufferedIOBase` provides or overrides these members in addition to | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 402 |    those from :class:`IOBase`: | 
 | 403 |  | 
| Benjamin Peterson | c609b6b | 2009-06-28 17:32:20 +0000 | [diff] [blame] | 404 |    .. attribute:: raw | 
 | 405 |  | 
 | 406 |       The underlying raw stream (a :class:`RawIOBase` instance) that | 
 | 407 |       :class:`BufferedIOBase` deals with.  This is not part of the | 
 | 408 |       :class:`BufferedIOBase` API and may not exist on some implementations. | 
 | 409 |  | 
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 410 |    .. method:: detach() | 
 | 411 |  | 
 | 412 |       Separate the underlying raw stream from the buffer and return it. | 
 | 413 |  | 
 | 414 |       After the raw stream has been detached, the buffer is in an unusable | 
 | 415 |       state. | 
 | 416 |  | 
 | 417 |       Some buffers, like :class:`BytesIO`, do not have the concept of a single | 
 | 418 |       raw stream to return from this method.  They raise | 
 | 419 |       :exc:`UnsupportedOperation`. | 
 | 420 |  | 
| Benjamin Peterson | edc3647 | 2009-05-01 20:48:14 +0000 | [diff] [blame] | 421 |       .. versionadded:: 3.1 | 
 | 422 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 423 |    .. method:: read(n=-1) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 424 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 425 |       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] | 426 |       negative, data is read and returned until EOF is reached.  An empty bytes | 
 | 427 |       object is returned if the stream is already at EOF. | 
 | 428 |  | 
 | 429 |       If the argument is positive, and the underlying raw stream is not | 
 | 430 |       interactive, multiple raw reads may be issued to satisfy the byte count | 
 | 431 |       (unless EOF is reached first).  But for interactive raw streams, at most | 
 | 432 |       one raw read will be issued, and a short result does not imply that EOF is | 
 | 433 |       imminent. | 
 | 434 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 435 |       A :exc:`BlockingIOError` is raised if the underlying raw stream is in | 
 | 436 |       non blocking-mode, and has no data available at the moment. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 437 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 438 |    .. method:: read1(n=-1) | 
| Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 439 |  | 
 | 440 |       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] | 441 |       raw stream's :meth:`~RawIOBase.read` method.  This can be useful if you | 
 | 442 |       are implementing your own buffering on top of a :class:`BufferedIOBase` | 
 | 443 |       object. | 
| Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 444 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 445 |    .. method:: readinto(b) | 
 | 446 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 447 |       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] | 448 |       read. | 
 | 449 |  | 
 | 450 |       Like :meth:`read`, multiple reads may be issued to the underlying raw | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 451 |       stream, unless the latter is 'interactive'. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 452 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 453 |       A :exc:`BlockingIOError` is raised if the underlying raw stream is in | 
 | 454 |       non blocking-mode, and has no data available at the moment. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 455 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 456 |    .. method:: write(b) | 
 | 457 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 458 |       Write the given bytes or bytearray object, *b* and return the number | 
 | 459 |       of bytes written (never less than ``len(b)``, since if the write fails | 
 | 460 |       an :exc:`IOError` will be raised).  Depending on the actual | 
 | 461 |       implementation, these bytes may be readily written to the underlying | 
 | 462 |       stream, or held in a buffer for performance and latency reasons. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 463 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 464 |       When in non-blocking mode, a :exc:`BlockingIOError` is raised if the | 
 | 465 |       data needed to be written to the raw stream but it couldn't accept | 
 | 466 |       all the data without blocking. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 467 |  | 
 | 468 |  | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 469 | Raw File I/O | 
 | 470 | ------------ | 
 | 471 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 472 | .. class:: FileIO(name, mode='r', closefd=True) | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 473 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 474 |    :class:`FileIO` represents an OS-level file containing bytes data. | 
 | 475 |    It implements the :class:`RawIOBase` interface (and therefore the | 
 | 476 |    :class:`IOBase` interface, too). | 
 | 477 |  | 
 | 478 |    The *name* can be one of two things: | 
 | 479 |  | 
 | 480 |    * a character string or bytes object representing the path to the file | 
 | 481 |      which will be opened; | 
 | 482 |    * an integer representing the number of an existing OS-level file descriptor | 
 | 483 |      to which the resulting :class:`FileIO` object will give access. | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 484 |  | 
 | 485 |    The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing, | 
 | 486 |    or appending.  The file will be created if it doesn't exist when opened for | 
 | 487 |    writing or appending; it will be truncated when opened for writing.  Add a | 
 | 488 |    ``'+'`` to the mode to allow simultaneous reading and writing. | 
 | 489 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 490 |    The :meth:`read` (when called with a positive argument), :meth:`readinto` | 
 | 491 |    and :meth:`write` methods on this class will only make one system call. | 
 | 492 |  | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 493 |    In addition to the attributes and methods from :class:`IOBase` and | 
 | 494 |    :class:`RawIOBase`, :class:`FileIO` provides the following data | 
 | 495 |    attributes and methods: | 
 | 496 |  | 
 | 497 |    .. attribute:: mode | 
 | 498 |  | 
 | 499 |       The mode as given in the constructor. | 
 | 500 |  | 
 | 501 |    .. attribute:: name | 
 | 502 |  | 
 | 503 |       The file name.  This is the file descriptor of the file when no name is | 
 | 504 |       given in the constructor. | 
 | 505 |  | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 506 |  | 
 | 507 | Buffered Streams | 
 | 508 | ---------------- | 
 | 509 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 510 | In many situations, buffered I/O streams will provide higher performance | 
 | 511 | (bandwidth and latency) than raw I/O streams.  Their API is also more usable. | 
 | 512 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 513 | .. class:: BytesIO([initial_bytes]) | 
 | 514 |  | 
 | 515 |    A stream implementation using an in-memory bytes buffer.  It inherits | 
 | 516 |    :class:`BufferedIOBase`. | 
 | 517 |  | 
 | 518 |    The argument *initial_bytes* is an optional initial bytearray. | 
 | 519 |  | 
 | 520 |    :class:`BytesIO` provides or overrides these methods in addition to those | 
 | 521 |    from :class:`BufferedIOBase` and :class:`IOBase`: | 
 | 522 |  | 
 | 523 |    .. method:: getvalue() | 
 | 524 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 525 |       Return ``bytes`` containing the entire contents of the buffer. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 526 |  | 
 | 527 |    .. method:: read1() | 
 | 528 |  | 
| Benjamin Peterson | 9efcc4b | 2008-04-14 21:30:21 +0000 | [diff] [blame] | 529 |       In :class:`BytesIO`, this is the same as :meth:`read`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 530 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 531 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 532 | .. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 533 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 534 |    A buffer providing higher-level access to a readable, sequential | 
 | 535 |    :class:`RawIOBase` object.  It inherits :class:`BufferedIOBase`. | 
 | 536 |    When reading data from this object, a larger amount of data may be | 
 | 537 |    requested from the underlying raw stream, and kept in an internal buffer. | 
 | 538 |    The buffered data can then be returned directly on subsequent reads. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 539 |  | 
 | 540 |    The constructor creates a :class:`BufferedReader` for the given readable | 
 | 541 |    *raw* stream and *buffer_size*.  If *buffer_size* is omitted, | 
 | 542 |    :data:`DEFAULT_BUFFER_SIZE` is used. | 
 | 543 |  | 
 | 544 |    :class:`BufferedReader` provides or overrides these methods in addition to | 
 | 545 |    those from :class:`BufferedIOBase` and :class:`IOBase`: | 
 | 546 |  | 
 | 547 |    .. method:: peek([n]) | 
 | 548 |  | 
| Benjamin Peterson | c43a26d | 2009-06-16 23:09:24 +0000 | [diff] [blame] | 549 |       Return bytes from the stream without advancing the position.  At most one | 
| Benjamin Peterson | 2a8b54d | 2009-06-14 14:37:23 +0000 | [diff] [blame] | 550 |       single read on the raw stream is done to satisfy the call. The number of | 
 | 551 |       bytes returned may be less or more than requested. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 552 |  | 
 | 553 |    .. method:: read([n]) | 
 | 554 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 555 |       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] | 556 |       or if the read call would block in non-blocking mode. | 
 | 557 |  | 
 | 558 |    .. method:: read1(n) | 
 | 559 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 560 |       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] | 561 |       at least one byte is buffered, only buffered bytes are returned. | 
 | 562 |       Otherwise, one raw stream read call is made. | 
 | 563 |  | 
 | 564 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 565 | .. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 566 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 567 |    A buffer providing higher-level access to a writeable, sequential | 
 | 568 |    :class:`RawIOBase` object.  It inherits :class:`BufferedIOBase`. | 
 | 569 |    When writing to this object, data is normally held into an internal | 
 | 570 |    buffer.  The buffer will be written out to the underlying :class:`RawIOBase` | 
 | 571 |    object under various conditions, including: | 
 | 572 |  | 
 | 573 |    * when the buffer gets too small for all pending data; | 
 | 574 |    * when :meth:`flush()` is called; | 
 | 575 |    * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects); | 
 | 576 |    * when the :class:`BufferedWriter` object is closed or destroyed. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 577 |  | 
 | 578 |    The constructor creates a :class:`BufferedWriter` for the given writeable | 
 | 579 |    *raw* stream.  If the *buffer_size* is not given, it defaults to | 
| Benjamin Peterson | 394ee00 | 2009-03-05 22:33:59 +0000 | [diff] [blame] | 580 |    :data:`DEFAULT_BUFFER_SIZE`. | 
 | 581 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 582 |    A third argument, *max_buffer_size*, is supported, but unused and deprecated. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 583 |  | 
 | 584 |    :class:`BufferedWriter` provides or overrides these methods in addition to | 
 | 585 |    those from :class:`BufferedIOBase` and :class:`IOBase`: | 
 | 586 |  | 
 | 587 |    .. method:: flush() | 
 | 588 |  | 
 | 589 |       Force bytes held in the buffer into the raw stream.  A | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 590 |       :exc:`BlockingIOError` should be raised if the raw stream blocks. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 591 |  | 
 | 592 |    .. method:: write(b) | 
 | 593 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 594 |       Write the bytes or bytearray object, *b* and return the number of bytes | 
 | 595 |       written.  When in non-blocking mode, a :exc:`BlockingIOError` is raised | 
 | 596 |       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] | 597 |  | 
 | 598 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 599 | .. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 600 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 601 |    A buffered I/O object giving a combined, higher-level access to two | 
 | 602 |    sequential :class:`RawIOBase` objects: one readable, the other writeable. | 
 | 603 |    It is useful for pairs of unidirectional communication channels | 
 | 604 |    (pipes, for instance).  It inherits :class:`BufferedIOBase`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 605 |  | 
 | 606 |    *reader* and *writer* are :class:`RawIOBase` objects that are readable and | 
 | 607 |    writeable respectively.  If the *buffer_size* is omitted it defaults to | 
| Benjamin Peterson | 394ee00 | 2009-03-05 22:33:59 +0000 | [diff] [blame] | 608 |    :data:`DEFAULT_BUFFER_SIZE`. | 
 | 609 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 610 |    A fourth argument, *max_buffer_size*, is supported, but unused and | 
 | 611 |    deprecated. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 612 |  | 
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 613 |    :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods | 
 | 614 |    except for :meth:`~BufferedIOBase.detach`, which raises | 
 | 615 |    :exc:`UnsupportedOperation`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 616 |  | 
 | 617 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 618 | .. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 619 |  | 
 | 620 |    A buffered interface to random access streams.  It inherits | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 621 |    :class:`BufferedReader` and :class:`BufferedWriter`, and further supports | 
 | 622 |    :meth:`seek` and :meth:`tell` functionality. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 623 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 624 |    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] | 625 |    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] | 626 |    :data:`DEFAULT_BUFFER_SIZE`. | 
 | 627 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 628 |    A third argument, *max_buffer_size*, is supported, but unused and deprecated. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 629 |  | 
 | 630 |    :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or | 
 | 631 |    :class:`BufferedWriter` can do. | 
 | 632 |  | 
 | 633 |  | 
 | 634 | Text I/O | 
 | 635 | -------- | 
 | 636 |  | 
 | 637 | .. class:: TextIOBase | 
 | 638 |  | 
 | 639 |    Base class for text streams.  This class provides a character and line based | 
 | 640 |    interface to stream I/O.  There is no :meth:`readinto` method because | 
 | 641 |    Python's character strings are immutable.  It inherits :class:`IOBase`. | 
 | 642 |    There is no public constructor. | 
 | 643 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 644 |    :class:`TextIOBase` provides or overrides these data attributes and | 
 | 645 |    methods in addition to those from :class:`IOBase`: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 646 |  | 
 | 647 |    .. attribute:: encoding | 
 | 648 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 649 |       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] | 650 |       strings, and to encode strings into bytes. | 
 | 651 |  | 
| Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 652 |    .. attribute:: errors | 
 | 653 |  | 
 | 654 |       The error setting of the decoder or encoder. | 
 | 655 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 656 |    .. attribute:: newlines | 
 | 657 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 658 |       A string, a tuple of strings, or ``None``, indicating the newlines | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 659 |       translated so far.  Depending on the implementation and the initial | 
 | 660 |       constructor flags, this may not be available. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 661 |  | 
| Benjamin Peterson | c609b6b | 2009-06-28 17:32:20 +0000 | [diff] [blame] | 662 |    .. attribute:: buffer | 
 | 663 |  | 
 | 664 |       The underlying binary buffer (a :class:`BufferedIOBase` instance) that | 
 | 665 |       :class:`TextIOBase` deals with.  This is not part of the | 
 | 666 |       :class:`TextIOBase` API and may not exist on some implementations. | 
 | 667 |  | 
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 668 |    .. method:: detach() | 
 | 669 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 670 |       Separate the underlying binary buffer from the :class:`TextIOBase` and | 
 | 671 |       return it. | 
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 672 |  | 
 | 673 |       After the underlying buffer has been detached, the :class:`TextIOBase` is | 
 | 674 |       in an unusable state. | 
 | 675 |  | 
 | 676 |       Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not | 
 | 677 |       have the concept of an underlying buffer and calling this method will | 
 | 678 |       raise :exc:`UnsupportedOperation`. | 
 | 679 |  | 
| Benjamin Peterson | edc3647 | 2009-05-01 20:48:14 +0000 | [diff] [blame] | 680 |       .. versionadded:: 3.1 | 
 | 681 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 682 |    .. method:: read(n) | 
 | 683 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 684 |       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] | 685 |       :class:`str`.  If *n* is negative or ``None``, reads until EOF. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 686 |  | 
 | 687 |    .. method:: readline() | 
 | 688 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 689 |       Read until newline or EOF and return a single ``str``.  If the stream is | 
 | 690 |       already at EOF, an empty string is returned. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 691 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 692 |    .. method:: write(s) | 
 | 693 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 694 |       Write the string *s* to the stream and return the number of characters | 
 | 695 |       written. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 696 |  | 
 | 697 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 698 | .. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 699 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 700 |    A buffered text stream over a :class:`BufferedIOBase` binary stream. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 701 |    It inherits :class:`TextIOBase`. | 
 | 702 |  | 
 | 703 |    *encoding* gives the name of the encoding that the stream will be decoded or | 
 | 704 |    encoded with.  It defaults to :func:`locale.getpreferredencoding`. | 
 | 705 |  | 
| Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 706 |    *errors* is an optional string that specifies how encoding and decoding | 
 | 707 |    errors are to be handled.  Pass ``'strict'`` to raise a :exc:`ValueError` | 
 | 708 |    exception if there is an encoding error (the default of ``None`` has the same | 
 | 709 |    effect), or pass ``'ignore'`` to ignore errors.  (Note that ignoring encoding | 
 | 710 |    errors can lead to data loss.)  ``'replace'`` causes a replacement marker | 
| Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 711 |    (such as ``'?'``) to be inserted where there is malformed data.  When | 
 | 712 |    writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character | 
 | 713 |    reference) or ``'backslashreplace'`` (replace with backslashed escape | 
 | 714 |    sequences) can be used.  Any other error handling name that has been | 
 | 715 |    registered with :func:`codecs.register_error` is also valid. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 716 |  | 
 | 717 |    *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``.  It | 
 | 718 |    controls the handling of line endings.  If it is ``None``, universal newlines | 
 | 719 |    is enabled.  With this enabled, on input, the lines endings ``'\n'``, | 
 | 720 |    ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to | 
 | 721 |    the caller.  Conversely, on output, ``'\n'`` is translated to the system | 
| Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 722 |    default line separator, :data:`os.linesep`.  If *newline* is any other of its | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 723 |    legal values, that newline becomes the newline when the file is read and it | 
 | 724 |    is returned untranslated.  On output, ``'\n'`` is converted to the *newline*. | 
 | 725 |  | 
 | 726 |    If *line_buffering* is ``True``, :meth:`flush` is implied when a call to | 
 | 727 |    write contains a newline character. | 
 | 728 |  | 
| Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 729 |    :class:`TextIOWrapper` provides one attribute in addition to those of | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 730 |    :class:`TextIOBase` and its parents: | 
 | 731 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 732 |    .. attribute:: line_buffering | 
 | 733 |  | 
 | 734 |       Whether line buffering is enabled. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 735 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 736 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 737 | .. class:: StringIO(initial_value='', newline=None) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 738 |  | 
| Georg Brandl | 2932d93 | 2008-05-30 06:27:09 +0000 | [diff] [blame] | 739 |    An in-memory stream for text.  It inherits :class:`TextIOWrapper`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 740 |  | 
| Benjamin Peterson | aa1c8d8 | 2009-03-09 02:02:23 +0000 | [diff] [blame] | 741 |    The initial value of the buffer (an empty string by default) can be set by | 
 | 742 |    providing *initial_value*.  The *newline* argument works like that of | 
 | 743 |    :class:`TextIOWrapper`.  The default is to do no newline translation. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 744 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 745 |    :class:`StringIO` provides this method in addition to those from | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 746 |    :class:`TextIOWrapper` and its parents: | 
 | 747 |  | 
 | 748 |    .. method:: getvalue() | 
 | 749 |  | 
| Georg Brandl | 2932d93 | 2008-05-30 06:27:09 +0000 | [diff] [blame] | 750 |       Return a ``str`` containing the entire contents of the buffer at any | 
 | 751 |       time before the :class:`StringIO` object's :meth:`close` method is | 
 | 752 |       called. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 753 |  | 
| Georg Brandl | 2932d93 | 2008-05-30 06:27:09 +0000 | [diff] [blame] | 754 |    Example usage:: | 
 | 755 |  | 
 | 756 |       import io | 
 | 757 |  | 
 | 758 |       output = io.StringIO() | 
 | 759 |       output.write('First line.\n') | 
 | 760 |       print('Second line.', file=output) | 
 | 761 |  | 
 | 762 |       # Retrieve file contents -- this will be | 
 | 763 |       # 'First line.\nSecond line.\n' | 
 | 764 |       contents = output.getvalue() | 
 | 765 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 766 |       # Close object and discard memory buffer -- | 
| Georg Brandl | 2932d93 | 2008-05-30 06:27:09 +0000 | [diff] [blame] | 767 |       # .getvalue() will now raise an exception. | 
 | 768 |       output.close() | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 769 |  | 
 | 770 | .. class:: IncrementalNewlineDecoder | 
 | 771 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 772 |    A helper codec that decodes newlines for universal newlines mode.  It | 
 | 773 |    inherits :class:`codecs.IncrementalDecoder`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 774 |  |