blob: b99324cdb38ad6582037875dde8ddb5659bfd7cb [file] [log] [blame]
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +00001:mod:`io` --- Core tools for working with streams
2=================================================
3
4.. module:: io
5 :synopsis: Core tools for working with streams.
6.. moduleauthor:: Guido van Rossum <guido@python.org>
7.. moduleauthor:: Mike Verdone <mike.verdone@gmail.com>
8.. moduleauthor:: Mark Russell <mark.russell@zen.co.uk>
Antoine Pitrouc9062ca2009-10-01 17:08:03 +00009.. moduleauthor:: Antoine Pitrou <solipsis@pitrou.net>
10.. moduleauthor:: Amaury Forgeot d'Arc <amauryfa@gmail.com>
11.. moduleauthor:: Benjamin Peterson <benjamin@python.org>
Benjamin Peterson51a37032009-01-11 19:48:15 +000012.. sectionauthor:: Benjamin Peterson <benjamin@python.org>
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000013
Benjamin Peterson183515c2010-08-28 20:56:50 +000014.. versionadded:: 2.6
15
Antoine Pitrouc9062ca2009-10-01 17:08:03 +000016The :mod:`io` module provides the Python interfaces to stream handling.
17Under Python 2.x, this is proposed as an alternative to the built-in
18:class:`file` object, but in Python 3.x it is the default interface to
19access files and streams.
20
21.. note::
22
23 Since this module has been designed primarily for Python 3.x, you have to
24 be aware that all uses of "bytes" in this document refer to the
25 :class:`str` type (of which :class:`bytes` is an alias), and all uses
26 of "text" refer to the :class:`unicode` type. Furthermore, those two
27 types are not interchangeable in the :mod:`io` APIs.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000028
29At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
30defines the basic interface to a stream. Note, however, that there is no
Mark Dickinson3e4caeb2009-02-21 20:27:01 +000031separation between reading and writing to streams; implementations are allowed
Georg Brandl21946af2010-10-06 09:28:45 +000032to raise an :exc:`IOError` if they do not support a given operation.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000033
34Extending :class:`IOBase` is :class:`RawIOBase` which deals simply with the
35reading and writing of raw bytes to a stream. :class:`FileIO` subclasses
Benjamin Petersonad9f6292008-04-21 11:57:40 +000036:class:`RawIOBase` to provide an interface to files in the machine's
37file system.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000038
39:class:`BufferedIOBase` deals with buffering on a raw byte stream
40(:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`,
41:class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are
Benjamin Petersonad9f6292008-04-21 11:57:40 +000042readable, writable, and both readable and writable.
43:class:`BufferedRandom` provides a buffered interface to random access
44streams. :class:`BytesIO` is a simple stream of in-memory bytes.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000045
Benjamin Petersonad9f6292008-04-21 11:57:40 +000046Another :class:`IOBase` subclass, :class:`TextIOBase`, deals with
47streams whose bytes represent text, and handles encoding and decoding
Antoine Pitrouc9062ca2009-10-01 17:08:03 +000048from and to :class:`unicode` strings. :class:`TextIOWrapper`, which extends
49it, is a buffered text interface to a buffered raw stream
Serhiy Storchaka1ae56fb2016-12-02 23:13:42 +020050(:class:`BufferedIOBase`). Finally, :class:`~io.StringIO` is an in-memory
Antoine Pitrouc9062ca2009-10-01 17:08:03 +000051stream for unicode text.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000052
53Argument names are not part of the specification, and only the arguments of
Georg Brandl9fa61bb2009-07-26 14:19:57 +000054:func:`.open` are intended to be used as keyword arguments.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000055
56
57Module Interface
58----------------
59
60.. data:: DEFAULT_BUFFER_SIZE
61
62 An int containing the default buffer size used by the module's buffered I/O
Georg Brandl9fa61bb2009-07-26 14:19:57 +000063 classes. :func:`.open` uses the file's blksize (as obtained by
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000064 :func:`os.stat`) if possible.
65
Benjamin Petersona9bd6d52010-04-27 21:01:54 +000066.. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000067
Antoine Pitrouc9062ca2009-10-01 17:08:03 +000068 Open *file* and return a corresponding stream. If the file cannot be opened,
69 an :exc:`IOError` is raised.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000070
Georg Brandlb917af82010-08-01 21:33:42 +000071 *file* is either a string giving the pathname (absolute or
72 relative to the current working directory) of the file to be opened or
73 an integer file descriptor of the file to be wrapped. (If a file descriptor
74 is given, it is closed when the returned I/O object is closed, unless
75 *closefd* is set to ``False``.)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000076
77 *mode* is an optional string that specifies the mode in which the file is
78 opened. It defaults to ``'r'`` which means open for reading in text mode.
79 Other common values are ``'w'`` for writing (truncating the file if it
80 already exists), and ``'a'`` for appending (which on *some* Unix systems,
81 means that *all* writes append to the end of the file regardless of the
82 current seek position). In text mode, if *encoding* is not specified the
83 encoding used is platform dependent. (For reading and writing raw bytes use
84 binary mode and leave *encoding* unspecified.) The available modes are:
85
86 ========= ===============================================================
87 Character Meaning
88 --------- ---------------------------------------------------------------
89 ``'r'`` open for reading (default)
90 ``'w'`` open for writing, truncating the file first
91 ``'a'`` open for writing, appending to the end of the file if it exists
92 ``'b'`` binary mode
93 ``'t'`` text mode (default)
94 ``'+'`` open a disk file for updating (reading and writing)
R David Murray5618aaa2012-08-15 11:15:39 -040095 ``'U'`` universal newlines mode (for backwards compatibility; should
Benjamin Petersonad9f6292008-04-21 11:57:40 +000096 not be used in new code)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000097 ========= ===============================================================
98
99 The default mode is ``'rt'`` (open for reading text). For binary random
100 access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while
101 ``'r+b'`` opens the file without truncation.
102
103 Python distinguishes between files opened in binary and text modes, even when
104 the underlying operating system doesn't. Files opened in binary mode
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000105 (including ``'b'`` in the *mode* argument) return contents as :class:`bytes`
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000106 objects without any decoding. In text mode (the default, or when ``'t'`` is
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000107 included in the *mode* argument), the contents of the file are returned as
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000108 :class:`unicode` strings, the bytes having been first decoded using a
109 platform-dependent encoding or using the specified *encoding* if given.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000110
Antoine Pitroue812d292009-12-19 21:01:10 +0000111 *buffering* is an optional integer used to set the buffering policy.
112 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
113 line buffering (only usable in text mode), and an integer > 1 to indicate
114 the size of a fixed-size chunk buffer. When no *buffering* argument is
115 given, the default buffering policy works as follows:
116
117 * Binary files are buffered in fixed-size chunks; the size of the buffer
118 is chosen using a heuristic trying to determine the underlying device's
119 "block size" and falling back on :attr:`DEFAULT_BUFFER_SIZE`.
120 On many systems, the buffer will typically be 4096 or 8192 bytes long.
121
122 * "Interactive" text files (files for which :meth:`isatty` returns True)
123 use line buffering. Other text files use the policy described above
124 for binary files.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000125
126 *encoding* is the name of the encoding used to decode or encode the file.
127 This should only be used in text mode. The default encoding is platform
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000128 dependent (whatever :func:`locale.getpreferredencoding` returns), but any
129 encoding supported by Python can be used. See the :mod:`codecs` module for
130 the list of supported encodings.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000131
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000132 *errors* is an optional string that specifies how encoding and decoding
Martin Panter3738ef42016-11-21 00:21:39 +0000133 errors are to be handled—this cannot be used in binary mode. Pass
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000134 ``'strict'`` to raise a :exc:`ValueError` exception if there is an encoding
135 error (the default of ``None`` has the same effect), or pass ``'ignore'`` to
136 ignore errors. (Note that ignoring encoding errors can lead to data loss.)
137 ``'replace'`` causes a replacement marker (such as ``'?'``) to be inserted
138 where there is malformed data. When writing, ``'xmlcharrefreplace'``
139 (replace with the appropriate XML character reference) or
140 ``'backslashreplace'`` (replace with backslashed escape sequences) can be
141 used. Any other error handling name that has been registered with
142 :func:`codecs.register_error` is also valid.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000143
R David Murray5618aaa2012-08-15 11:15:39 -0400144 .. index::
145 single: universal newlines; open() (in module io)
146
R David Murrayc7b8f802012-08-15 11:22:58 -0400147 *newline* controls how :term:`universal newlines` works (it only applies to
148 text mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``.
149 It works as follows:
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000150
151 * On input, if *newline* is ``None``, universal newlines mode is enabled.
152 Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``, and these
153 are translated into ``'\n'`` before being returned to the caller. If it is
R David Murray5618aaa2012-08-15 11:15:39 -0400154 ``''``, universal newlines mode is enabled, but line endings are returned to
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000155 the caller untranslated. If it has any of the other legal values, input
156 lines are only terminated by the given string, and the line ending is
157 returned to the caller untranslated.
158
159 * On output, if *newline* is ``None``, any ``'\n'`` characters written are
160 translated to the system default line separator, :data:`os.linesep`. If
161 *newline* is ``''``, no translation takes place. If *newline* is any of
162 the other legal values, any ``'\n'`` characters written are translated to
163 the given string.
164
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000165 If *closefd* is ``False`` and a file descriptor rather than a filename was
166 given, the underlying file descriptor will be kept open when the file is
167 closed. If a filename is given *closefd* has no effect and must be ``True``
168 (the default).
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000169
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000170 The type of file object returned by the :func:`.open` function depends on the
171 mode. When :func:`.open` is used to open a file in a text mode (``'w'``,
172 ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a subclass of
173 :class:`TextIOBase` (specifically :class:`TextIOWrapper`). When used to open
174 a file in a binary mode with buffering, the returned class is a subclass of
175 :class:`BufferedIOBase`. The exact class varies: in read binary mode, it
176 returns a :class:`BufferedReader`; in write binary and append binary modes,
177 it returns a :class:`BufferedWriter`, and in read/write mode, it returns a
178 :class:`BufferedRandom`. When buffering is disabled, the raw stream, a
179 subclass of :class:`RawIOBase`, :class:`FileIO`, is returned.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000180
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000181 It is also possible to use an :class:`unicode` or :class:`bytes` string
182 as a file for both reading and writing. For :class:`unicode` strings
Serhiy Storchaka1ae56fb2016-12-02 23:13:42 +0200183 :class:`~io.StringIO` can be used like a file opened in text mode,
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000184 and for :class:`bytes` a :class:`BytesIO` can be used like a
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000185 file opened in a binary mode.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000186
187
188.. exception:: BlockingIOError
189
190 Error raised when blocking would occur on a non-blocking stream. It inherits
191 :exc:`IOError`.
192
193 In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one
194 attribute:
195
196 .. attribute:: characters_written
197
198 An integer containing the number of characters written to the stream
199 before it blocked.
200
201
202.. exception:: UnsupportedOperation
203
204 An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised
205 when an unsupported operation is called on a stream.
206
207
208I/O Base Classes
209----------------
210
211.. class:: IOBase
212
213 The abstract base class for all I/O classes, acting on streams of bytes.
214 There is no public constructor.
215
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000216 This class provides empty abstract implementations for many methods
217 that derived classes can override selectively; the default
218 implementations represent a file that cannot be read, written or
219 seeked.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000220
221 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
222 or :meth:`write` because their signatures will vary, implementations and
223 clients should consider those methods part of the interface. Also,
Martin Panterb362f752015-11-02 03:37:02 +0000224 implementations may raise an :exc:`IOError` when operations they do not
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000225 support are called.
226
227 The basic type used for binary data read from or written to a file is
Martin Panterc9813d82016-06-03 05:59:20 +0000228 :class:`bytes` (also known as :class:`str`). Method arguments may
229 also be :class:`bytearray` or :class:`memoryview` of arrays of bytes.
230 In some cases, such as :meth:`~RawIOBase.readinto`, a writable object
231 such as :class:`bytearray` is required.
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000232 Text I/O classes work with :class:`unicode` data.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000233
Martin Panterc9813d82016-06-03 05:59:20 +0000234 .. versionchanged:: 2.7
235 Implementations should support :class:`memoryview` arguments.
236
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000237 Note that calling any method (even inquiries) on a closed stream is
238 undefined. Implementations may raise :exc:`IOError` in this case.
239
240 IOBase (and its subclasses) support the iterator protocol, meaning that an
241 :class:`IOBase` object can be iterated over yielding the lines in a stream.
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000242 Lines are defined slightly differently depending on whether the stream is
243 a binary stream (yielding :class:`bytes`), or a text stream (yielding
Meador Inge52957182011-12-03 12:13:42 -0600244 :class:`unicode` strings). See :meth:`~IOBase.readline` below.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000245
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000246 IOBase is also a context manager and therefore supports the
247 :keyword:`with` statement. In this example, *file* is closed after the
248 :keyword:`with` statement's suite is finished---even if an exception occurs::
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000249
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000250 with io.open('spam.txt', 'w') as file:
251 file.write(u'Spam and eggs!')
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000252
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000253 :class:`IOBase` provides these data attributes and methods:
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000254
255 .. method:: close()
256
Amaury Forgeot d'Arc32265652008-11-20 23:34:31 +0000257 Flush and close this stream. This method has no effect if the file is
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000258 already closed. Once the file is closed, any operation on the file
Georg Brandl64879522010-05-10 21:51:33 +0000259 (e.g. reading or writing) will raise a :exc:`ValueError`.
Antoine Pitrou689405e2010-04-28 19:57:33 +0000260
261 As a convenience, it is allowed to call this method more than once;
262 only the first call, however, will have an effect.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000263
264 .. attribute:: closed
265
266 True if the stream is closed.
267
268 .. method:: fileno()
269
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000270 Return the underlying file descriptor (an integer) of the stream if it
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000271 exists. An :exc:`IOError` is raised if the IO object does not use a file
272 descriptor.
273
274 .. method:: flush()
275
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000276 Flush the write buffers of the stream if applicable. This does nothing
277 for read-only and non-blocking streams.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000278
279 .. method:: isatty()
280
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000281 Return ``True`` if the stream is interactive (i.e., connected to
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000282 a terminal/tty device).
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000283
284 .. method:: readable()
285
Serhiy Storchaka26d936a2013-11-29 12:16:53 +0200286 Return ``True`` if the stream can be read from. If ``False``, :meth:`read`
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000287 will raise :exc:`IOError`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000288
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000289 .. method:: readline(limit=-1)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000290
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000291 Read and return one line from the stream. If *limit* is specified, at
292 most *limit* bytes will be read.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000293
294 The line terminator is always ``b'\n'`` for binary files; for text files,
Zachary Warea2dd3b22014-07-18 09:11:48 -0500295 the *newline* argument to :func:`.open` can be used to select the line
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000296 terminator(s) recognized.
297
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000298 .. method:: readlines(hint=-1)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000299
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000300 Read and return a list of lines from the stream. *hint* can be specified
301 to control the number of lines read: no more lines will be read if the
302 total size (in bytes/characters) of all lines so far exceeds *hint*.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000303
Ezio Melotti1b332062013-04-15 19:08:31 +0300304 Note that it's already possible to iterate on file objects using ``for
305 line in file: ...`` without calling ``file.readlines()``.
306
Martin Panter6f804642015-09-11 03:58:30 +0000307 .. method:: seek(offset[, whence])
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000308
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000309 Change the stream position to the given byte *offset*. *offset* is
Martin Panter6f804642015-09-11 03:58:30 +0000310 interpreted relative to the position indicated by *whence*. The default
311 value for *whence* is :data:`SEEK_SET`. Values for *whence* are:
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000312
Georg Brandl88ed8f22009-04-01 21:00:55 +0000313 * :data:`SEEK_SET` or ``0`` -- start of the stream (the default);
314 *offset* should be zero or positive
315 * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may
316 be negative
317 * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually
318 negative
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000319
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000320 Return the new absolute position.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000321
Georg Brandl88ed8f22009-04-01 21:00:55 +0000322 .. versionadded:: 2.7
323 The ``SEEK_*`` constants
324
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000325 .. method:: seekable()
326
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000327 Return ``True`` if the stream supports random access. If ``False``,
328 :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000329
330 .. method:: tell()
331
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000332 Return the current stream position.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000333
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000334 .. method:: truncate(size=None)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000335
Antoine Pitrou9fe61992010-05-29 12:06:13 +0000336 Resize the stream to the given *size* in bytes (or the current position
337 if *size* is not specified). The current stream position isn't changed.
338 This resizing can extend or reduce the current file size. In case of
339 extension, the contents of the new file area depend on the platform
340 (on most systems, additional bytes are zero-filled, on Windows they're
341 undetermined). The new file size is returned.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000342
343 .. method:: writable()
344
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000345 Return ``True`` if the stream supports writing. If ``False``,
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000346 :meth:`write` and :meth:`truncate` will raise :exc:`IOError`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000347
348 .. method:: writelines(lines)
349
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000350 Write a list of lines to the stream. Line separators are not added, so it
351 is usual for each of the lines provided to have a line separator at the
352 end.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000353
Benjamin Petersonefda5582014-06-14 18:51:34 -0700354 .. method:: __del__()
355
356 Prepare for object destruction. :class:`IOBase` provides a default
357 implementation of this method that calls the instance's
358 :meth:`~IOBase.close` method.
359
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000360
361.. class:: RawIOBase
362
363 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no
364 public constructor.
365
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000366 Raw binary I/O typically provides low-level access to an underlying OS
367 device or API, and does not try to encapsulate it in high-level primitives
368 (this is left to Buffered I/O and Text I/O, described later in this page).
369
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000370 In addition to the attributes and methods from :class:`IOBase`,
371 RawIOBase provides the following methods:
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000372
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000373 .. method:: read(n=-1)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000374
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000375 Read up to *n* bytes from the object and return them. As a convenience,
376 if *n* is unspecified or -1, :meth:`readall` is called. Otherwise,
377 only one system call is ever made. Fewer than *n* bytes may be
378 returned if the operating system call returns fewer than *n* bytes.
379
380 If 0 bytes are returned, and *n* was not 0, this indicates end of file.
381 If the object is in non-blocking mode and no bytes are available,
382 ``None`` is returned.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000383
384 .. method:: readall()
385
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000386 Read and return all the bytes from the stream until EOF, using multiple
387 calls to the stream if necessary.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000388
389 .. method:: readinto(b)
390
Martin Panterc9813d82016-06-03 05:59:20 +0000391 Read up to len(b) bytes into *b*, and return the number
392 of bytes read. The object *b* should be a pre-allocated, writable
393 array of bytes, either :class:`bytearray` or :class:`memoryview`.
394 If the object is in non-blocking mode and no
Daniel Stutzbach27f5a7e2010-11-30 17:58:13 +0000395 bytes are available, ``None`` is returned.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000396
397 .. method:: write(b)
398
Martin Panterc9813d82016-06-03 05:59:20 +0000399 Write *b* to the underlying raw stream, and return the
400 number of bytes written. The object *b* should be an array
401 of bytes, either :class:`bytes`, :class:`bytearray`, or
402 :class:`memoryview`. The return value can be less than
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000403 ``len(b)``, depending on specifics of the underlying raw stream, and
404 especially if it is in non-blocking mode. ``None`` is returned if the
405 raw stream is set not to block and no single byte could be readily
Martin Panterc9813d82016-06-03 05:59:20 +0000406 written to it. The caller may release or mutate *b* after
407 this method returns, so the implementation should only access *b*
408 during the method call.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000409
410
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000411.. class:: BufferedIOBase
412
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000413 Base class for binary streams that support some kind of buffering.
414 It inherits :class:`IOBase`. There is no public constructor.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000415
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000416 The main difference with :class:`RawIOBase` is that methods :meth:`read`,
417 :meth:`readinto` and :meth:`write` will try (respectively) to read as much
418 input as requested or to consume all given output, at the expense of
419 making perhaps more than one system call.
420
421 In addition, those methods can raise :exc:`BlockingIOError` if the
422 underlying raw stream is in non-blocking mode and cannot take or give
423 enough data; unlike their :class:`RawIOBase` counterparts, they will
424 never return ``None``.
425
426 Besides, the :meth:`read` method does not have a default
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000427 implementation that defers to :meth:`readinto`.
428
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000429 A typical :class:`BufferedIOBase` implementation should not inherit from a
430 :class:`RawIOBase` implementation, but wrap one, like
431 :class:`BufferedWriter` and :class:`BufferedReader` do.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000432
Senthil Kumaran6f18b982011-07-04 12:50:02 -0700433 :class:`BufferedIOBase` provides or overrides these methods and attribute in
434 addition to those from :class:`IOBase`:
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000435
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000436 .. attribute:: raw
437
438 The underlying raw stream (a :class:`RawIOBase` instance) that
439 :class:`BufferedIOBase` deals with. This is not part of the
440 :class:`BufferedIOBase` API and may not exist on some implementations.
441
442 .. method:: detach()
443
444 Separate the underlying raw stream from the buffer and return it.
445
446 After the raw stream has been detached, the buffer is in an unusable
447 state.
448
449 Some buffers, like :class:`BytesIO`, do not have the concept of a single
450 raw stream to return from this method. They raise
451 :exc:`UnsupportedOperation`.
452
453 .. versionadded:: 2.7
454
455 .. method:: read(n=-1)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000456
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000457 Read and return up to *n* bytes. If the argument is omitted, ``None``, or
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000458 negative, data is read and returned until EOF is reached. An empty bytes
459 object is returned if the stream is already at EOF.
460
461 If the argument is positive, and the underlying raw stream is not
462 interactive, multiple raw reads may be issued to satisfy the byte count
463 (unless EOF is reached first). But for interactive raw streams, at most
464 one raw read will be issued, and a short result does not imply that EOF is
465 imminent.
466
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000467 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
468 non blocking-mode, and has no data available at the moment.
469
470 .. method:: read1(n=-1)
471
472 Read and return up to *n* bytes, with at most one call to the underlying
473 raw stream's :meth:`~RawIOBase.read` method. This can be useful if you
474 are implementing your own buffering on top of a :class:`BufferedIOBase`
475 object.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000476
477 .. method:: readinto(b)
478
Martin Panterc9813d82016-06-03 05:59:20 +0000479 Read up to len(b) bytes into *b*, and return the number of bytes read.
480 The object *b* should be a pre-allocated, writable array of bytes,
481 either :class:`bytearray` or :class:`memoryview`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000482
483 Like :meth:`read`, multiple reads may be issued to the underlying raw
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000484 stream, unless the latter is 'interactive'.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000485
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000486 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
487 non blocking-mode, and has no data available at the moment.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000488
489 .. method:: write(b)
490
Martin Panterc9813d82016-06-03 05:59:20 +0000491 Write *b*, and return the number of bytes written
492 (always equal to ``len(b)``, since if the write fails
493 an :exc:`IOError` will be raised). The object *b* should be
494 an array of bytes, either :class:`bytes`, :class:`bytearray`,
495 or :class:`memoryview`. Depending on the actual
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000496 implementation, these bytes may be readily written to the underlying
497 stream, or held in a buffer for performance and latency reasons.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000498
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000499 When in non-blocking mode, a :exc:`BlockingIOError` is raised if the
500 data needed to be written to the raw stream but it couldn't accept
501 all the data without blocking.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000502
Martin Panterc9813d82016-06-03 05:59:20 +0000503 The caller may release or mutate *b* after this method returns,
504 so the implementation should only access *b* during the method call.
505
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000506
Benjamin Petersonb6c7beb2009-01-19 16:17:54 +0000507Raw File I/O
508------------
509
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000510.. class:: FileIO(name, mode='r', closefd=True)
Benjamin Petersonb6c7beb2009-01-19 16:17:54 +0000511
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000512 :class:`FileIO` represents an OS-level file containing bytes data.
513 It implements the :class:`RawIOBase` interface (and therefore the
514 :class:`IOBase` interface, too).
515
516 The *name* can be one of two things:
517
518 * a string representing the path to the file which will be opened;
519 * an integer representing the number of an existing OS-level file descriptor
520 to which the resulting :class:`FileIO` object will give access.
Benjamin Petersonb6c7beb2009-01-19 16:17:54 +0000521
522 The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing,
523 or appending. The file will be created if it doesn't exist when opened for
524 writing or appending; it will be truncated when opened for writing. Add a
525 ``'+'`` to the mode to allow simultaneous reading and writing.
526
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000527 The :meth:`read` (when called with a positive argument), :meth:`readinto`
528 and :meth:`write` methods on this class will only make one system call.
529
Benjamin Petersonb6c7beb2009-01-19 16:17:54 +0000530 In addition to the attributes and methods from :class:`IOBase` and
531 :class:`RawIOBase`, :class:`FileIO` provides the following data
532 attributes and methods:
533
534 .. attribute:: mode
535
536 The mode as given in the constructor.
537
538 .. attribute:: name
539
540 The file name. This is the file descriptor of the file when no name is
541 given in the constructor.
542
Benjamin Petersonb6c7beb2009-01-19 16:17:54 +0000543
544Buffered Streams
545----------------
546
Antoine Pitrou4cb64ad2010-12-03 19:31:52 +0000547Buffered I/O streams provide a higher-level interface to an I/O device
548than raw I/O does.
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000549
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000550.. class:: BytesIO([initial_bytes])
551
552 A stream implementation using an in-memory bytes buffer. It inherits
553 :class:`BufferedIOBase`.
554
Martin Panterc9813d82016-06-03 05:59:20 +0000555 The optional argument *initial_bytes* is a :class:`bytes` object that
556 contains initial data.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000557
558 :class:`BytesIO` provides or overrides these methods in addition to those
559 from :class:`BufferedIOBase` and :class:`IOBase`:
560
561 .. method:: getvalue()
562
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000563 Return ``bytes`` containing the entire contents of the buffer.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000564
565 .. method:: read1()
566
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000567 In :class:`BytesIO`, this is the same as :meth:`read`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000568
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000569
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000570.. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000571
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000572 A buffer providing higher-level access to a readable, sequential
573 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
574 When reading data from this object, a larger amount of data may be
575 requested from the underlying raw stream, and kept in an internal buffer.
576 The buffered data can then be returned directly on subsequent reads.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000577
578 The constructor creates a :class:`BufferedReader` for the given readable
579 *raw* stream and *buffer_size*. If *buffer_size* is omitted,
580 :data:`DEFAULT_BUFFER_SIZE` is used.
581
582 :class:`BufferedReader` provides or overrides these methods in addition to
583 those from :class:`BufferedIOBase` and :class:`IOBase`:
584
585 .. method:: peek([n])
586
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000587 Return bytes from the stream without advancing the position. At most one
588 single read on the raw stream is done to satisfy the call. The number of
589 bytes returned may be less or more than requested.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000590
591 .. method:: read([n])
592
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000593 Read and return *n* bytes, or if *n* is not given or negative, until EOF
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000594 or if the read call would block in non-blocking mode.
595
596 .. method:: read1(n)
597
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000598 Read and return up to *n* bytes with only one call on the raw stream. If
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000599 at least one byte is buffered, only buffered bytes are returned.
600 Otherwise, one raw stream read call is made.
601
602
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000603.. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000604
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000605 A buffer providing higher-level access to a writeable, sequential
606 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
607 When writing to this object, data is normally held into an internal
608 buffer. The buffer will be written out to the underlying :class:`RawIOBase`
609 object under various conditions, including:
610
611 * when the buffer gets too small for all pending data;
612 * when :meth:`flush()` is called;
613 * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects);
614 * when the :class:`BufferedWriter` object is closed or destroyed.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000615
616 The constructor creates a :class:`BufferedWriter` for the given writeable
617 *raw* stream. If the *buffer_size* is not given, it defaults to
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000618 :data:`DEFAULT_BUFFER_SIZE`.
619
620 A third argument, *max_buffer_size*, is supported, but unused and deprecated.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000621
622 :class:`BufferedWriter` provides or overrides these methods in addition to
623 those from :class:`BufferedIOBase` and :class:`IOBase`:
624
625 .. method:: flush()
626
627 Force bytes held in the buffer into the raw stream. A
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000628 :exc:`BlockingIOError` should be raised if the raw stream blocks.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000629
630 .. method:: write(b)
631
Martin Panterc9813d82016-06-03 05:59:20 +0000632 Write *b*, and return the number of bytes written.
633 The object *b* should be an array of bytes, either
634 :class:`bytes`, :class:`bytearray`, or :class:`memoryview`.
635 When in non-blocking mode, a :exc:`BlockingIOError` is raised
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000636 if the buffer needs to be written out but the raw stream blocks.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000637
638
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000639.. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000640
641 A buffered interface to random access streams. It inherits
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000642 :class:`BufferedReader` and :class:`BufferedWriter`, and further supports
643 :meth:`seek` and :meth:`tell` functionality.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000644
645 The constructor creates a reader and writer for a seekable raw stream, given
646 in the first argument. If the *buffer_size* is omitted it defaults to
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000647 :data:`DEFAULT_BUFFER_SIZE`.
648
649 A third argument, *max_buffer_size*, is supported, but unused and deprecated.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000650
651 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
652 :class:`BufferedWriter` can do.
653
654
Antoine Pitroub3843562011-08-20 19:51:31 +0200655.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE)
656
657 A buffered I/O object combining two unidirectional :class:`RawIOBase`
658 objects -- one readable, the other writeable -- into a single bidirectional
659 endpoint. It inherits :class:`BufferedIOBase`.
660
661 *reader* and *writer* are :class:`RawIOBase` objects that are readable and
662 writeable respectively. If the *buffer_size* is omitted it defaults to
663 :data:`DEFAULT_BUFFER_SIZE`.
664
665 A fourth argument, *max_buffer_size*, is supported, but unused and
666 deprecated.
667
668 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods
669 except for :meth:`~BufferedIOBase.detach`, which raises
670 :exc:`UnsupportedOperation`.
671
672 .. warning::
Éric Araujo69d09652014-03-12 19:35:54 -0400673
Antoine Pitroub3843562011-08-20 19:51:31 +0200674 :class:`BufferedRWPair` does not attempt to synchronize accesses to
675 its underlying raw streams. You should not pass it the same object
676 as reader and writer; use :class:`BufferedRandom` instead.
677
678
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000679Text I/O
680--------
681
682.. class:: TextIOBase
683
Martin Panter6a8163a2016-04-15 02:14:19 +0000684 Base class for text streams. This class provides a unicode character
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000685 and line based interface to stream I/O. There is no :meth:`readinto`
686 method because Python's :class:`unicode` strings are immutable.
687 It inherits :class:`IOBase`. There is no public constructor.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000688
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000689 :class:`TextIOBase` provides or overrides these data attributes and
690 methods in addition to those from :class:`IOBase`:
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000691
692 .. attribute:: encoding
693
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000694 The name of the encoding used to decode the stream's bytes into
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000695 strings, and to encode strings into bytes.
696
Antoine Pitrou19690592009-06-12 20:14:08 +0000697 .. attribute:: errors
698
699 The error setting of the decoder or encoder.
700
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000701 .. attribute:: newlines
702
Benjamin Petersonad9f6292008-04-21 11:57:40 +0000703 A string, a tuple of strings, or ``None``, indicating the newlines
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000704 translated so far. Depending on the implementation and the initial
705 constructor flags, this may not be available.
706
707 .. attribute:: buffer
708
709 The underlying binary buffer (a :class:`BufferedIOBase` instance) that
710 :class:`TextIOBase` deals with. This is not part of the
711 :class:`TextIOBase` API and may not exist on some implementations.
712
713 .. method:: detach()
714
715 Separate the underlying binary buffer from the :class:`TextIOBase` and
716 return it.
717
718 After the underlying buffer has been detached, the :class:`TextIOBase` is
719 in an unusable state.
720
Serhiy Storchaka1ae56fb2016-12-02 23:13:42 +0200721 Some :class:`TextIOBase` implementations, like :class:`~io.StringIO`, may not
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000722 have the concept of an underlying buffer and calling this method will
723 raise :exc:`UnsupportedOperation`.
724
725 .. versionadded:: 2.7
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000726
727 .. method:: read(n)
728
Benjamin Peterson3c399d12008-04-22 02:16:03 +0000729 Read and return at most *n* characters from the stream as a single
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000730 :class:`unicode`. If *n* is negative or ``None``, reads until EOF.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000731
Antoine Pitroub37f14c2012-07-25 22:38:33 +0200732 .. method:: readline(limit=-1)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000733
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000734 Read until newline or EOF and return a single ``unicode``. If the
735 stream is already at EOF, an empty string is returned.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000736
Antoine Pitroub37f14c2012-07-25 22:38:33 +0200737 If *limit* is specified, at most *limit* characters will be read.
738
Martin Panter6f804642015-09-11 03:58:30 +0000739 .. method:: seek(offset[, whence])
Antoine Pitrou8fc732f2012-01-21 20:27:59 +0100740
Martin Panter6f804642015-09-11 03:58:30 +0000741 Change the stream position to the given *offset*. Behaviour depends on
742 the *whence* parameter. The default value for *whence* is
743 :data:`SEEK_SET`.
Antoine Pitrou8fc732f2012-01-21 20:27:59 +0100744
745 * :data:`SEEK_SET` or ``0``: seek from the start of the stream
746 (the default); *offset* must either be a number returned by
747 :meth:`TextIOBase.tell`, or zero. Any other *offset* value
748 produces undefined behaviour.
749 * :data:`SEEK_CUR` or ``1``: "seek" to the current position;
750 *offset* must be zero, which is a no-operation (all other values
751 are unsupported).
752 * :data:`SEEK_END` or ``2``: seek to the end of the stream;
753 *offset* must be zero (all other values are unsupported).
754
755 Return the new absolute position as an opaque number.
756
757 .. versionadded:: 2.7
758 The ``SEEK_*`` constants.
759
760 .. method:: tell()
761
762 Return the current stream position as an opaque number. The number
763 does not usually represent a number of bytes in the underlying
764 binary storage.
765
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000766 .. method:: write(s)
767
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000768 Write the :class:`unicode` string *s* to the stream and return the
769 number of characters written.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000770
771
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000772.. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000773
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000774 A buffered text stream over a :class:`BufferedIOBase` binary stream.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000775 It inherits :class:`TextIOBase`.
776
777 *encoding* gives the name of the encoding that the stream will be decoded or
778 encoded with. It defaults to :func:`locale.getpreferredencoding`.
779
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000780 *errors* is an optional string that specifies how encoding and decoding
781 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError`
782 exception if there is an encoding error (the default of ``None`` has the same
783 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding
784 errors can lead to data loss.) ``'replace'`` causes a replacement marker
Benjamin Petersona7d09032008-04-19 19:47:34 +0000785 (such as ``'?'``) to be inserted where there is malformed data. When
786 writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character
787 reference) or ``'backslashreplace'`` (replace with backslashed escape
788 sequences) can be used. Any other error handling name that has been
789 registered with :func:`codecs.register_error` is also valid.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000790
R David Murray5618aaa2012-08-15 11:15:39 -0400791 .. index::
792 single: universal newlines; io.TextIOWrapper class
793
Antoine Pitrou76370f42012-08-04 00:55:38 +0200794 *newline* controls how line endings are handled. It can be ``None``,
795 ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows:
796
R David Murray5618aaa2012-08-15 11:15:39 -0400797 * On input, if *newline* is ``None``, :term:`universal newlines` mode is
R David Murrayc7b8f802012-08-15 11:22:58 -0400798 enabled. Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``,
799 and these are translated into ``'\n'`` before being returned to the
800 caller. If it is ``''``, universal newlines mode is enabled, but line
801 endings are returned to the caller untranslated. If it has any of the
802 other legal values, input lines are only terminated by the given string,
803 and the line ending is returned to the caller untranslated.
Antoine Pitrou76370f42012-08-04 00:55:38 +0200804
805 * On output, if *newline* is ``None``, any ``'\n'`` characters written are
806 translated to the system default line separator, :data:`os.linesep`. If
807 *newline* is ``''``, no translation takes place. If *newline* is any of
808 the other legal values, any ``'\n'`` characters written are translated to
809 the given string.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000810
811 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
812 write contains a newline character.
813
Antoine Pitrou19690592009-06-12 20:14:08 +0000814 :class:`TextIOWrapper` provides one attribute in addition to those of
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000815 :class:`TextIOBase` and its parents:
816
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000817 .. attribute:: line_buffering
818
819 Whether line buffering is enabled.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000820
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000821
Antoine Pitrou8a2e22e2014-02-02 22:48:25 +0100822.. class:: StringIO(initial_value=u'', newline=u'\\n')
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000823
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000824 An in-memory stream for unicode text. It inherits :class:`TextIOWrapper`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000825
Martin Panterdab305e2015-10-10 02:52:30 +0000826 The initial value of the buffer can be set by providing *initial_value*.
827 If newline translation is enabled, newlines will be encoded as if by
828 :meth:`~TextIOBase.write`. The stream is positioned at the start of
829 the buffer.
830
831 The *newline* argument works like that of :class:`TextIOWrapper`.
832 The default is to consider only ``\n`` characters as ends of lines and
833 to do no newline translation. If *newline* is set to ``None``,
834 newlines are written as ``\n`` on all platforms, but universal
835 newline decoding is still performed when reading.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000836
Serhiy Storchaka1ae56fb2016-12-02 23:13:42 +0200837 :class:`~io.StringIO` provides this method in addition to those from
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000838 :class:`TextIOWrapper` and its parents:
839
840 .. method:: getvalue()
841
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000842 Return a ``unicode`` containing the entire contents of the buffer at any
Serhiy Storchaka1ae56fb2016-12-02 23:13:42 +0200843 time before the :class:`~io.StringIO` object's :meth:`close` method is
Martin Panterdab305e2015-10-10 02:52:30 +0000844 called. Newlines are decoded as if by :meth:`~TextIOBase.read`,
845 although the stream position is not changed.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000846
Antoine Pitrouc9062ca2009-10-01 17:08:03 +0000847 Example usage::
848
849 import io
850
851 output = io.StringIO()
852 output.write(u'First line.\n')
853 output.write(u'Second line.\n')
854
855 # Retrieve file contents -- this will be
856 # u'First line.\nSecond line.\n'
857 contents = output.getvalue()
858
859 # Close object and discard memory buffer --
860 # .getvalue() will now raise an exception.
861 output.close()
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000862
Antoine Pitrou4cb64ad2010-12-03 19:31:52 +0000863
R David Murray5618aaa2012-08-15 11:15:39 -0400864.. index::
865 single: universal newlines; io.IncrementalNewlineDecoder class
866
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000867.. class:: IncrementalNewlineDecoder
868
R David Murray5618aaa2012-08-15 11:15:39 -0400869 A helper codec that decodes newlines for :term:`universal newlines` mode.
870 It inherits :class:`codecs.IncrementalDecoder`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000871
Antoine Pitrou4cb64ad2010-12-03 19:31:52 +0000872
873Advanced topics
874---------------
875
876Here we will discuss several advanced topics pertaining to the concrete
877I/O implementations described above.
878
879Performance
880^^^^^^^^^^^
881
882Binary I/O
883""""""""""
884
885By reading and writing only large chunks of data even when the user asks
886for a single byte, buffered I/O is designed to hide any inefficiency in
887calling and executing the operating system's unbuffered I/O routines. The
888gain will vary very much depending on the OS and the kind of I/O which is
889performed (for example, on some contemporary OSes such as Linux, unbuffered
890disk I/O can be as fast as buffered I/O). The bottom line, however, is
891that buffered I/O will offer you predictable performance regardless of the
892platform and the backing device. Therefore, it is most always preferable to
893use buffered I/O rather than unbuffered I/O.
894
895Text I/O
896""""""""
897
898Text I/O over a binary storage (such as a file) is significantly slower than
899binary I/O over the same storage, because it implies conversions from
900unicode to binary data using a character codec. This can become noticeable
901if you handle huge amounts of text data (for example very large log files).
Antoine Pitrou893e7c62011-02-04 20:17:53 +0000902Also, :meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both
903quite slow due to the reconstruction algorithm used.
Antoine Pitrou4cb64ad2010-12-03 19:31:52 +0000904
Serhiy Storchaka1ae56fb2016-12-02 23:13:42 +0200905:class:`~io.StringIO`, however, is a native in-memory unicode container and will
Antoine Pitrou4cb64ad2010-12-03 19:31:52 +0000906exhibit similar speed to :class:`BytesIO`.
907
908Multi-threading
909^^^^^^^^^^^^^^^
910
911:class:`FileIO` objects are thread-safe to the extent that the operating
912system calls (such as ``read(2)`` under Unix) they are wrapping are thread-safe
913too.
914
915Binary buffered objects (instances of :class:`BufferedReader`,
916:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
917protect their internal structures using a lock; it is therefore safe to call
918them from multiple threads at once.
919
920:class:`TextIOWrapper` objects are not thread-safe.
921
922Reentrancy
923^^^^^^^^^^
924
925Binary buffered objects (instances of :class:`BufferedReader`,
926:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
927are not reentrant. While reentrant calls will not happen in normal situations,
928they can arise if you are doing I/O in a :mod:`signal` handler. If it is
929attempted to enter a buffered object again while already being accessed
930*from the same thread*, then a :exc:`RuntimeError` is raised.
931
932The above implicitly extends to text files, since the :func:`open()`
933function will wrap a buffered object inside a :class:`TextIOWrapper`. This
934includes standard streams and therefore affects the built-in function
935:func:`print()` as well.
936