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> |
| 9 | .. sectionauthor:: Benjamin Peterson |
| 10 | |
| 11 | The :mod:`io` module provides the Python interfaces to stream handling. The |
| 12 | builtin :func:`open` function is defined in this module. |
| 13 | |
| 14 | At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It |
| 15 | defines the basic interface to a stream. Note, however, that there is no |
| 16 | seperation between reading and writing to streams; implementations are allowed |
| 17 | to throw an :exc:`IOError` if they do not support a given operation. |
| 18 | |
| 19 | Extending :class:`IOBase` is :class:`RawIOBase` which deals simply with the |
| 20 | reading and writing of raw bytes to a stream. :class:`FileIO` subclasses |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 21 | :class:`RawIOBase` to provide an interface to files in the machine's |
| 22 | file system. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 23 | |
| 24 | :class:`BufferedIOBase` deals with buffering on a raw byte stream |
| 25 | (:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`, |
| 26 | :class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 27 | readable, writable, and both readable and writable. |
| 28 | :class:`BufferedRandom` provides a buffered interface to random access |
| 29 | streams. :class:`BytesIO` is a simple stream of in-memory bytes. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 30 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 31 | Another :class:`IOBase` subclass, :class:`TextIOBase`, deals with |
| 32 | streams whose bytes represent text, and handles encoding and decoding |
| 33 | from and to strings. :class:`TextIOWrapper`, which extends it, is a |
| 34 | buffered text interface to a buffered raw stream |
| 35 | (:class:`BufferedIOBase`). Finally, :class:`StringIO` is an in-memory |
| 36 | stream for text. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 37 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 38 | Argument names are not part of the specification, and only the arguments of |
Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 39 | :func:`open` are intended to be used as keyword arguments. |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 40 | |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 41 | |
| 42 | Module Interface |
| 43 | ---------------- |
| 44 | |
| 45 | .. data:: DEFAULT_BUFFER_SIZE |
| 46 | |
| 47 | An int containing the default buffer size used by the module's buffered I/O |
Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 48 | classes. :func:`open` uses the file's blksize (as obtained by |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 49 | :func:`os.stat`) if possible. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 50 | |
| 51 | .. function:: open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]]) |
| 52 | |
Benjamin Peterson | dd21912 | 2008-04-11 21:17:32 +0000 | [diff] [blame] | 53 | Open *file* and return a stream. If the file cannot be opened, an |
| 54 | :exc:`IOError` is raised. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 55 | |
Benjamin Peterson | dd21912 | 2008-04-11 21:17:32 +0000 | [diff] [blame] | 56 | *file* is either a string giving the name (and the path if the file isn't in |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 57 | the current working directory) of the file to be opened or a file |
| 58 | descriptor of the file to be opened. (If a file descriptor is given, |
| 59 | for example, from :func:`os.fdopen`, it is closed when the returned |
| 60 | I/O object is closed, unless *closefd* is set to ``False``.) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 61 | |
Benjamin Peterson | dd21912 | 2008-04-11 21:17:32 +0000 | [diff] [blame] | 62 | *mode* is an optional string that specifies the mode in which the file is |
| 63 | opened. It defaults to ``'r'`` which means open for reading in text mode. |
| 64 | Other common values are ``'w'`` for writing (truncating the file if it |
| 65 | already exists), and ``'a'`` for appending (which on *some* Unix systems, |
| 66 | means that *all* writes append to the end of the file regardless of the |
| 67 | current seek position). In text mode, if *encoding* is not specified the |
| 68 | encoding used is platform dependent. (For reading and writing raw bytes use |
| 69 | binary mode and leave *encoding* unspecified.) The available modes are: |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 70 | |
| 71 | ========= =============================================================== |
| 72 | Character Meaning |
| 73 | --------- --------------------------------------------------------------- |
| 74 | ``'r'`` open for reading (default) |
| 75 | ``'w'`` open for writing, truncating the file first |
| 76 | ``'a'`` open for writing, appending to the end of the file if it exists |
| 77 | ``'b'`` binary mode |
| 78 | ``'t'`` text mode (default) |
| 79 | ``'+'`` open a disk file for updating (reading and writing) |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 80 | ``'U'`` universal newline mode (for backwards compatibility; should |
| 81 | not be used in new code) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 82 | ========= =============================================================== |
| 83 | |
| 84 | The default mode is ``'rt'`` (open for reading text). For binary random |
| 85 | access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while |
| 86 | ``'r+b'`` opens the file without truncation. |
| 87 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 88 | Python distinguishes between files opened in binary and text modes, even when |
| 89 | the underlying operating system doesn't. Files opened in binary mode |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 90 | (including ``'b'`` in the *mode* argument) return contents as ``bytes`` |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 91 | 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^] | 92 | 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] | 93 | strings, the bytes having been first decoded using a platform-dependent |
| 94 | encoding or using the specified *encoding* if given. |
Benjamin Peterson | dd21912 | 2008-04-11 21:17:32 +0000 | [diff] [blame] | 95 | |
| 96 | *buffering* is an optional integer used to set the buffering policy. By |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 97 | default full buffering is on. Pass 0 to switch buffering off (only allowed |
| 98 | in binary mode), 1 to set line buffering, and an integer > 1 for full |
| 99 | buffering. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 100 | |
| 101 | *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] | 102 | This should only be used in text mode. The default encoding is platform |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 103 | dependent, but any encoding supported by Python can be used. See the |
Benjamin Peterson | dd21912 | 2008-04-11 21:17:32 +0000 | [diff] [blame] | 104 | :mod:`codecs` module for the list of supported encodings. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 105 | |
Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 106 | *errors* is an optional string that specifies how encoding and decoding |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 107 | errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError` |
| 108 | exception if there is an encoding error (the default of ``None`` has the same |
| 109 | effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding |
| 110 | errors can lead to data loss.) ``'replace'`` causes a replacement marker |
| 111 | (such as ``'?'``) to be inserted where there is malformed data. When |
| 112 | writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character |
| 113 | reference) or ``'backslashreplace'`` (replace with backslashed escape |
| 114 | sequences) can be used. Any other error handling name that has been |
| 115 | registered with :func:`codecs.register_error` is also valid. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 116 | |
| 117 | *newline* controls how universal newlines works (it only applies to text |
| 118 | mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It |
| 119 | works as follows: |
| 120 | |
| 121 | * On input, if *newline* is ``None``, universal newlines mode is enabled. |
| 122 | Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``, and these |
| 123 | are translated into ``'\n'`` before being returned to the caller. If it is |
| 124 | ``''``, universal newline mode is enabled, but line endings are returned to |
| 125 | the caller untranslated. If it has any of the other legal values, input |
| 126 | lines are only terminated by the given string, and the line ending is |
| 127 | returned to the caller untranslated. |
| 128 | |
| 129 | * On output, if *newline* is ``None``, any ``'\n'`` characters written are |
| 130 | translated to the system default line separator, :data:`os.linesep`. If |
| 131 | *newline* is ``''``, no translation takes place. If *newline* is any of |
| 132 | the other legal values, any ``'\n'`` characters written are translated to |
| 133 | the given string. |
| 134 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 135 | If *closefd* is ``False`` and a file descriptor rather than a |
| 136 | filename was given, the underlying file descriptor will be kept open |
| 137 | when the file is closed. If a filename is given *closefd* has no |
| 138 | effect but must be ``True`` (the default). |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 139 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 140 | The type of file object returned by the :func:`open` function depends |
| 141 | on the mode. When :func:`open` is used to open a file in a text mode |
| 142 | (``'w'``, ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a |
| 143 | :class:`TextIOWrapper`. When used to open a file in a binary mode, |
| 144 | the returned class varies: in read binary mode, it returns a |
| 145 | :class:`BufferedReader`; in write binary and append binary modes, it |
| 146 | returns a :class:`BufferedWriter`, and in read/write mode, it returns |
| 147 | a :class:`BufferedRandom`. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 148 | |
| 149 | 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] | 150 | 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^] | 151 | a text mode, and for bytearrays a :class:`BytesIO` can be used like a |
| 152 | file opened in a binary mode. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | .. exception:: BlockingIOError |
| 156 | |
| 157 | Error raised when blocking would occur on a non-blocking stream. It inherits |
| 158 | :exc:`IOError`. |
| 159 | |
| 160 | In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one |
| 161 | attribute: |
| 162 | |
| 163 | .. attribute:: characters_written |
| 164 | |
| 165 | An integer containing the number of characters written to the stream |
| 166 | before it blocked. |
| 167 | |
| 168 | |
| 169 | .. exception:: UnsupportedOperation |
| 170 | |
| 171 | An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised |
| 172 | when an unsupported operation is called on a stream. |
| 173 | |
| 174 | |
| 175 | I/O Base Classes |
| 176 | ---------------- |
| 177 | |
| 178 | .. class:: IOBase |
| 179 | |
| 180 | The abstract base class for all I/O classes, acting on streams of bytes. |
| 181 | There is no public constructor. |
| 182 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 183 | This class provides empty abstract implementations for many methods |
| 184 | that derived classes can override selectively; the default |
| 185 | implementations represent a file that cannot be read, written or |
| 186 | seeked. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 187 | |
| 188 | Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`, |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 189 | or :meth:`write` because their signatures will vary, implementations and |
| 190 | clients should consider those methods part of the interface. Also, |
| 191 | implementations may raise a :exc:`IOError` when operations they do not |
| 192 | support are called. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 193 | |
| 194 | The basic type used for binary data read from or written to a file is |
| 195 | :class:`bytes`. :class:`bytearray`\s are accepted too, and in some cases |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 196 | (such as :class:`readinto`) required. Text I/O classes work with |
| 197 | :class:`str` data. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 198 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 199 | Note that calling any method (even inquiries) on a closed stream is |
| 200 | undefined. Implementations may raise :exc:`IOError` in this case. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 201 | |
| 202 | IOBase (and its subclasses) support the iterator protocol, meaning that an |
| 203 | :class:`IOBase` object can be iterated over yielding the lines in a stream. |
| 204 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 205 | IOBase is also a context manager and therefore supports the |
| 206 | :keyword:`with` statement. In this example, *file* is closed after the |
| 207 | :keyword:`with` statement's suite is finished---even if an exception occurs:: |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 208 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 209 | with open('spam.txt', 'w') as file: |
| 210 | file.write('Spam and eggs!') |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 211 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 212 | :class:`IOBase` provides these data attributes and methods: |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 213 | |
| 214 | .. method:: close() |
| 215 | |
| 216 | Flush and close this stream. This method has no effect if the file is |
| 217 | already closed. |
| 218 | |
| 219 | .. attribute:: closed |
| 220 | |
| 221 | True if the stream is closed. |
| 222 | |
| 223 | .. method:: fileno() |
| 224 | |
| 225 | Return the underlying file descriptor (an integer) of the stream, if it |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 226 | 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] | 227 | descriptor. |
| 228 | |
| 229 | .. method:: flush() |
| 230 | |
Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 231 | Flush the write buffers of the stream if applicable. This does nothing |
| 232 | for read-only and non-blocking streams. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 233 | |
| 234 | .. method:: isatty() |
| 235 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 236 | Returns ``True`` if the stream is interactive (i.e., connected to |
| 237 | a terminal/tty device). |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 238 | |
| 239 | .. method:: readable() |
| 240 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 241 | Returns ``True`` if the stream can be read from. If False, |
| 242 | :meth:`read` will raise :exc:`IOError`. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 243 | |
| 244 | .. method:: readline([limit]) |
| 245 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 246 | Reads and returns one line from the stream. If *limit* is |
| 247 | specified, at most *limit* bytes will be read. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 248 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 249 | The line terminator is always ``b'\n'`` for binary files; for text files, |
Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 250 | 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] | 251 | terminator(s) recognized. |
| 252 | |
| 253 | .. method:: readlines([hint]) |
| 254 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 255 | Returns a list of lines from the stream. *hint* can be specified to |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 256 | control the number of lines read: no more lines will be read if the total |
| 257 | size (in bytes/characters) of all lines so far exceeds *hint*. |
| 258 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 259 | .. method:: seek(offset[, whence]) |
| 260 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 261 | Change the stream position to the given byte *offset*. *offset* is |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 262 | interpreted relative to the position indicated by *whence*. Values for |
| 263 | *whence* are: |
| 264 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 265 | * ``0`` -- start of the stream (the default); *offset* should be zero or positive |
| 266 | * ``1`` -- current stream position; *offset* may be negative |
| 267 | * ``2`` -- end of the stream; *offset* is usually negative |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 268 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 269 | Returns the new absolute position. |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 270 | |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 271 | .. method:: seekable() |
| 272 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 273 | Returns ``True`` if the stream supports random access. If |
| 274 | ``False``, :meth:`seek`, :meth:`tell` and :meth:`truncate` will |
| 275 | raise :exc:`IOError`. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 276 | |
| 277 | .. method:: tell() |
| 278 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 279 | Returns the current stream position. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 280 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 281 | .. method:: truncate([size]) |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 282 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 283 | Truncates the file to at most *size* bytes. *size* defaults to the current |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 284 | file position, as returned by :meth:`tell`. |
| 285 | |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 286 | .. method:: writable() |
| 287 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 288 | Returns ``True`` if the stream supports writing. If ``False``, |
| 289 | :meth:`write` and :meth:`truncate` will raise :exc:`IOError`. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 290 | |
| 291 | .. method:: writelines(lines) |
| 292 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 293 | Writes a list of lines to the stream. Line separators are not |
| 294 | added, so it is usual for each of the lines provided to have a |
| 295 | line separator at the end. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 296 | |
| 297 | |
| 298 | .. class:: RawIOBase |
| 299 | |
| 300 | Base class for raw binary I/O. It inherits :class:`IOBase`. There is no |
| 301 | public constructor. |
| 302 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 303 | In addition to the attributes and methods from :class:`IOBase`, |
| 304 | RawIOBase provides the following methods: |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 305 | |
Benjamin Peterson | b47aace | 2008-04-09 21:38:38 +0000 | [diff] [blame] | 306 | .. method:: read([n]) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 307 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 308 | Reads and returns all the bytes from the stream until EOF, or if *n* is |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 309 | specified, up to *n* bytes. An empty bytes object is returned on EOF; |
| 310 | ``None`` is returned if the object is set not to block and has no data to |
| 311 | read. |
| 312 | |
Benjamin Peterson | b47aace | 2008-04-09 21:38:38 +0000 | [diff] [blame] | 313 | .. method:: readall() |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 314 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 315 | Reads and returns all the bytes from the stream until EOF, using |
| 316 | multiple calls to the stream if necessary. |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 317 | |
| 318 | .. method:: readinto(b) |
| 319 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 320 | Reads up to len(b) bytes into bytearray *b* and returns the number |
| 321 | of bytes read. |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 322 | |
| 323 | .. method:: write(b) |
| 324 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 325 | Writes the given bytes or bytearray object, *b*, to the underlying |
| 326 | raw stream and returns the number of bytes written (never less |
| 327 | than ``len(b)``, since if the write fails an :exc:`IOError` will |
| 328 | be raised). |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 329 | |
| 330 | |
| 331 | Raw File I/O |
| 332 | ------------ |
| 333 | |
| 334 | .. class:: FileIO(name[, mode]) |
| 335 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 336 | :class:`FileIO` represents a file containing bytes data. It implements |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 337 | the :class:`RawIOBase` interface (and therefore the :class:`IOBase` |
| 338 | interface, too). |
| 339 | |
| 340 | The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing, |
| 341 | or appending. The file will be created if it doesn't exist when opened for |
| 342 | writing or appending; it will be truncated when opened for writing. Add a |
| 343 | ``'+'`` to the mode to allow simultaneous reading and writing. |
| 344 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 345 | In addition to the attributes and methods from :class:`IOBase` and |
| 346 | :class:`RawIOBase`, :class:`FileIO` provides the following data |
| 347 | attributes and methods: |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 348 | |
| 349 | .. attribute:: mode |
| 350 | |
| 351 | The mode as given in the constructor. |
| 352 | |
| 353 | .. attribute:: name |
| 354 | |
| 355 | The file name. |
| 356 | |
| 357 | .. method:: read([n]) |
| 358 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 359 | Reads and returns at most *n* bytes. Only one system call is made, so |
| 360 | it is possible that less data than was requested is returned. Call |
| 361 | :func:`len` on the returned bytes object to see how many bytes |
| 362 | were actually returned (In non-blocking mode, ``None`` is returned |
| 363 | when no data is available.) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 364 | |
| 365 | .. method:: readall() |
| 366 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 367 | Reads and returns the entire file's contents in a single bytes |
| 368 | object. As much as immediately available is returned in |
| 369 | non-blocking mode. If the EOF has been reached, ``b''`` is |
| 370 | returned. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 371 | |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 372 | .. method:: write(b) |
| 373 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 374 | Write the bytes or bytearray object, *b*, to the file, and return |
| 375 | the number actually written. Only one system call is made, so it |
| 376 | is possible that only some of the data is written. |
| 377 | |
| 378 | Note that the inherited ``readinto()`` method should not be used on |
| 379 | :class:`FileIO` objects. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 380 | |
| 381 | |
| 382 | Buffered Streams |
| 383 | ---------------- |
| 384 | |
| 385 | .. class:: BufferedIOBase |
| 386 | |
| 387 | Base class for streams that support buffering. It inherits :class:`IOBase`. |
| 388 | There is no public constructor. |
| 389 | |
| 390 | The main difference with :class:`RawIOBase` is that the :meth:`read` method |
| 391 | supports omitting the *size* argument, and does not have a default |
| 392 | implementation that defers to :meth:`readinto`. |
| 393 | |
| 394 | In addition, :meth:`read`, :meth:`readinto`, and :meth:`write` may raise |
| 395 | :exc:`BlockingIOError` if the underlying raw stream is in non-blocking mode |
| 396 | and not ready; unlike their raw counterparts, they will never return |
| 397 | ``None``. |
| 398 | |
| 399 | A typical implementation should not inherit from a :class:`RawIOBase` |
| 400 | implementation, but wrap one like :class:`BufferedWriter` and |
| 401 | :class:`BufferedReader`. |
| 402 | |
| 403 | :class:`BufferedIOBase` provides or overrides these methods in addition to |
| 404 | those from :class:`IOBase`: |
| 405 | |
| 406 | .. method:: read([n]) |
| 407 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 408 | Reads and returns up to *n* bytes. If the argument is omitted, ``None``, or |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 409 | negative, data is read and returned until EOF is reached. An empty bytes |
| 410 | object is returned if the stream is already at EOF. |
| 411 | |
| 412 | If the argument is positive, and the underlying raw stream is not |
| 413 | interactive, multiple raw reads may be issued to satisfy the byte count |
| 414 | (unless EOF is reached first). But for interactive raw streams, at most |
| 415 | one raw read will be issued, and a short result does not imply that EOF is |
| 416 | imminent. |
| 417 | |
| 418 | A :exc:`BlockingIOError` is raised if the underlying raw stream has no |
| 419 | data at the moment. |
| 420 | |
| 421 | .. method:: readinto(b) |
| 422 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 423 | Reads up to len(b) bytes into bytearray *b* and returns the number of bytes |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 424 | read. |
| 425 | |
| 426 | Like :meth:`read`, multiple reads may be issued to the underlying raw |
| 427 | stream, unless the latter is 'interactive.' |
| 428 | |
| 429 | A :exc:`BlockingIOError` is raised if the underlying raw stream has no |
| 430 | data at the moment. |
| 431 | |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 432 | .. method:: write(b) |
| 433 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 434 | Writes the given bytes or bytearray object, *b*, to the underlying |
| 435 | raw stream and returns the number of bytes written (never less than |
| 436 | ``len(b)``, since if the write fails an :exc:`IOError` will |
| 437 | be raised). |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 438 | |
| 439 | A :exc:`BlockingIOError` is raised if the buffer is full, and the |
| 440 | underlying raw stream cannot accept more data at the moment. |
| 441 | |
| 442 | |
| 443 | .. class:: BytesIO([initial_bytes]) |
| 444 | |
| 445 | A stream implementation using an in-memory bytes buffer. It inherits |
| 446 | :class:`BufferedIOBase`. |
| 447 | |
| 448 | The argument *initial_bytes* is an optional initial bytearray. |
| 449 | |
| 450 | :class:`BytesIO` provides or overrides these methods in addition to those |
| 451 | from :class:`BufferedIOBase` and :class:`IOBase`: |
| 452 | |
| 453 | .. method:: getvalue() |
| 454 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 455 | Returns a bytes object containing the entire contents of the |
| 456 | buffer. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 457 | |
| 458 | .. method:: read1() |
| 459 | |
Benjamin Peterson | 9efcc4b | 2008-04-14 21:30:21 +0000 | [diff] [blame] | 460 | In :class:`BytesIO`, this is the same as :meth:`read`. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 461 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 462 | .. method:: truncate([size]) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 463 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 464 | Truncates the buffer to at most *size* bytes. *size* defaults to the current |
Benjamin Peterson | 9efcc4b | 2008-04-14 21:30:21 +0000 | [diff] [blame] | 465 | stream position, as returned by :meth:`tell`. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 466 | |
| 467 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 468 | .. class:: BufferedReader(raw[, buffer_size]) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 469 | |
Benjamin Peterson | 13d4a61 | 2008-04-13 23:46:27 +0000 | [diff] [blame] | 470 | A buffer for a readable, sequential :class:`RawIOBase` object. It inherits |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 471 | :class:`BufferedIOBase`. |
| 472 | |
| 473 | The constructor creates a :class:`BufferedReader` for the given readable |
| 474 | *raw* stream and *buffer_size*. If *buffer_size* is omitted, |
| 475 | :data:`DEFAULT_BUFFER_SIZE` is used. |
| 476 | |
| 477 | :class:`BufferedReader` provides or overrides these methods in addition to |
| 478 | those from :class:`BufferedIOBase` and :class:`IOBase`: |
| 479 | |
| 480 | .. method:: peek([n]) |
| 481 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 482 | Returns 1 (or *n* if specified) bytes from a buffer without |
| 483 | advancing the position. Only a single read on the raw stream is done to |
| 484 | satisfy the call. The number of bytes returned may be less than |
| 485 | requested since at most all the buffer's bytes from the current |
| 486 | position to the end are returned. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 487 | |
| 488 | .. method:: read([n]) |
| 489 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 490 | Reads and returns *n* bytes, or if *n* is not given or negative, until EOF |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 491 | or if the read call would block in non-blocking mode. |
| 492 | |
| 493 | .. method:: read1(n) |
| 494 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 495 | Reads and returns 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] | 496 | at least one byte is buffered, only buffered bytes are returned. |
| 497 | Otherwise, one raw stream read call is made. |
| 498 | |
| 499 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 500 | .. class:: BufferedWriter(raw[, buffer_size[, max_buffer_size]]) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 501 | |
| 502 | A buffer for a writeable sequential RawIO object. It inherits |
| 503 | :class:`BufferedIOBase`. |
| 504 | |
| 505 | The constructor creates a :class:`BufferedWriter` for the given writeable |
| 506 | *raw* stream. If the *buffer_size* is not given, it defaults to |
| 507 | :data:`DEAFULT_BUFFER_SIZE`. If *max_buffer_size* is omitted, it defaults to |
| 508 | twice the buffer size. |
| 509 | |
| 510 | :class:`BufferedWriter` provides or overrides these methods in addition to |
| 511 | those from :class:`BufferedIOBase` and :class:`IOBase`: |
| 512 | |
| 513 | .. method:: flush() |
| 514 | |
| 515 | Force bytes held in the buffer into the raw stream. A |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 516 | :exc:`BlockingIOError` should be raised if the raw stream blocks. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 517 | |
| 518 | .. method:: write(b) |
| 519 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 520 | Writes the bytes or bytearray object, *b*, onto the raw stream and |
| 521 | returns the number of bytes written. A :exc:`BlockingIOError` is |
| 522 | raised when the raw stream blocks. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 523 | |
| 524 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 525 | .. class:: BufferedRWPair(reader, writer[, buffer_size[, max_buffer_size]]) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 526 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 527 | A combined buffered writer and reader object for a raw stream that can be |
| 528 | written to and read from. It has and supports both :meth:`read`, :meth:`write`, |
| 529 | and their variants. This is useful for sockets and two-way pipes. |
| 530 | It inherits :class:`BufferedIOBase`. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 531 | |
| 532 | *reader* and *writer* are :class:`RawIOBase` objects that are readable and |
| 533 | writeable respectively. If the *buffer_size* is omitted it defaults to |
| 534 | :data:`DEFAULT_BUFFER_SIZE`. The *max_buffer_size* (for the buffered writer) |
| 535 | defaults to twice the buffer size. |
| 536 | |
| 537 | :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods. |
| 538 | |
| 539 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 540 | .. class:: BufferedRandom(raw[, buffer_size[, max_buffer_size]]) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 541 | |
| 542 | A buffered interface to random access streams. It inherits |
| 543 | :class:`BufferedReader` and :class:`BufferedWriter`. |
| 544 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 545 | 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] | 546 | in the first argument. If the *buffer_size* is omitted it defaults to |
| 547 | :data:`DEFAULT_BUFFER_SIZE`. The *max_buffer_size* (for the buffered writer) |
| 548 | defaults to twice the buffer size. |
| 549 | |
| 550 | :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or |
| 551 | :class:`BufferedWriter` can do. |
| 552 | |
| 553 | |
| 554 | Text I/O |
| 555 | -------- |
| 556 | |
| 557 | .. class:: TextIOBase |
| 558 | |
| 559 | Base class for text streams. This class provides a character and line based |
| 560 | interface to stream I/O. There is no :meth:`readinto` method because |
| 561 | Python's character strings are immutable. It inherits :class:`IOBase`. |
| 562 | There is no public constructor. |
| 563 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 564 | :class:`TextIOBase` provides or overrides these data attributes and |
| 565 | methods in addition to those from :class:`IOBase`: |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 566 | |
| 567 | .. attribute:: encoding |
| 568 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 569 | 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] | 570 | strings, and to encode strings into bytes. |
| 571 | |
| 572 | .. attribute:: newlines |
| 573 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 574 | A string, a tuple of strings, or ``None``, indicating the newlines |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 575 | translated so far. |
| 576 | |
| 577 | .. method:: read(n) |
| 578 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 579 | Reads and returns at most *n* characters from the stream as a |
| 580 | single :class:`str`. If *n* is negative or ``None``, reads to EOF. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 581 | |
| 582 | .. method:: readline() |
| 583 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 584 | Reads until newline or EOF and returns a single :class:`str`. If |
| 585 | the stream is already at EOF, an empty string is returned. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 586 | |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 587 | .. method:: write(s) |
| 588 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 589 | Writes the string *s* to the stream and returns the number of |
| 590 | characters written. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 591 | |
| 592 | |
| 593 | .. class:: TextIOWrapper(buffer[, encoding[, errors[, newline[, line_buffering]]]]) |
| 594 | |
| 595 | A buffered text stream over a :class:`BufferedIOBase` raw stream, *buffer*. |
| 596 | It inherits :class:`TextIOBase`. |
| 597 | |
| 598 | *encoding* gives the name of the encoding that the stream will be decoded or |
| 599 | encoded with. It defaults to :func:`locale.getpreferredencoding`. |
| 600 | |
Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 601 | *errors* is an optional string that specifies how encoding and decoding |
| 602 | errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError` |
| 603 | exception if there is an encoding error (the default of ``None`` has the same |
| 604 | effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding |
| 605 | errors can lead to data loss.) ``'replace'`` causes a replacement marker |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 606 | (such as ``'?'``) to be inserted where there is malformed data. When |
| 607 | writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character |
| 608 | reference) or ``'backslashreplace'`` (replace with backslashed escape |
| 609 | sequences) can be used. Any other error handling name that has been |
| 610 | registered with :func:`codecs.register_error` is also valid. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 611 | |
| 612 | *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``. It |
| 613 | controls the handling of line endings. If it is ``None``, universal newlines |
| 614 | is enabled. With this enabled, on input, the lines endings ``'\n'``, |
| 615 | ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to |
| 616 | the caller. Conversely, on output, ``'\n'`` is translated to the system |
| 617 | default line seperator, :data:`os.linesep`. If *newline* is any other of its |
| 618 | legal values, that newline becomes the newline when the file is read and it |
| 619 | is returned untranslated. On output, ``'\n'`` is converted to the *newline*. |
| 620 | |
| 621 | If *line_buffering* is ``True``, :meth:`flush` is implied when a call to |
| 622 | write contains a newline character. |
| 623 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 624 | :class:`TextIOWrapper` provides these data attributes in addition to those of |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 625 | :class:`TextIOBase` and its parents: |
| 626 | |
| 627 | .. attribute:: errors |
| 628 | |
| 629 | The encoding and decoding error setting. |
| 630 | |
| 631 | .. attribute:: line_buffering |
| 632 | |
| 633 | Whether line buffering is enabled. |
| 634 | |
| 635 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 636 | .. class:: StringIO([initial_value[, encoding[, errors[, newline]]]]) |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 637 | |
| 638 | An in-memory stream for text. It in inherits :class:`TextIOWrapper`. |
| 639 | |
| 640 | Create a new StringIO stream with an inital value, encoding, error handling, |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 641 | and newline setting. See :class:`TextIOWrapper`\'s constructor for more |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 642 | information. |
| 643 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 644 | :class:`StringIO` provides this method in addition to those from |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 645 | :class:`TextIOWrapper` and its parents: |
| 646 | |
| 647 | .. method:: getvalue() |
| 648 | |
Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame^] | 649 | Returns a :class:`str` containing the entire contents of the buffer. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 650 | |
| 651 | |
| 652 | .. class:: IncrementalNewlineDecoder |
| 653 | |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 654 | A helper codec that decodes newlines for universal newlines mode. It |
| 655 | inherits :class:`codecs.IncrementalDecoder`. |
Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 656 | |