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