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