blob: e83e55ca555dbdffdd491e7063960d2136d0254b [file] [log] [blame]
Georg Brandl014197c2008-04-09 18:40:51 +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>
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00009.. moduleauthor:: Antoine Pitrou <solipsis@pitrou.net>
10.. moduleauthor:: Amaury Forgeot d'Arc <amauryfa@gmail.com>
Benjamin Petersonef9f2bd2009-05-01 20:45:43 +000011.. moduleauthor:: Benjamin Peterson <benjamin@python.org>
Benjamin Peterson058e31e2009-01-16 03:54:08 +000012.. sectionauthor:: Benjamin Peterson <benjamin@python.org>
Georg Brandl014197c2008-04-09 18:40:51 +000013
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000014.. _io-overview:
15
Antoine Pitroub530e142010-08-30 12:41:00 +000016Overview
17--------
Georg Brandl014197c2008-04-09 18:40:51 +000018
R David Murray9f0c9402012-08-17 20:33:54 -040019.. index::
20 single: file object; io module
21
22The :mod:`io` module provides Python's main facilities for dealing with various
23types of I/O. There are three main types of I/O: *text I/O*, *binary I/O*
24and *raw I/O*. These are generic categories, and various backing stores can
25be used for each of them. A concrete object belonging to any of these
26categories is called a :term:`file object`. Other common terms are *stream*
27and *file-like object*.
Georg Brandl014197c2008-04-09 18:40:51 +000028
Antoine Pitroub530e142010-08-30 12:41:00 +000029Independently of its category, each concrete stream object will also have
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000030various capabilities: it can be read-only, write-only, or read-write. It can
31also allow arbitrary random access (seeking forwards or backwards to any
32location), or only sequential access (for example in the case of a socket or
33pipe).
Georg Brandl014197c2008-04-09 18:40:51 +000034
Antoine Pitroub530e142010-08-30 12:41:00 +000035All streams are careful about the type of data you give to them. For example
36giving a :class:`str` object to the ``write()`` method of a binary stream
37will raise a ``TypeError``. So will giving a :class:`bytes` object to the
38``write()`` method of a text stream.
Georg Brandl014197c2008-04-09 18:40:51 +000039
Antoine Pitroua787b652011-10-12 19:02:52 +020040.. versionchanged:: 3.3
Eli Benderskyf877a7c2012-07-14 21:22:25 +030041 Operations that used to raise :exc:`IOError` now raise :exc:`OSError`, since
42 :exc:`IOError` is now an alias of :exc:`OSError`.
Antoine Pitroua787b652011-10-12 19:02:52 +020043
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000044
Antoine Pitroub530e142010-08-30 12:41:00 +000045Text I/O
46^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +000047
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000048Text I/O expects and produces :class:`str` objects. This means that whenever
49the backing store is natively made of bytes (such as in the case of a file),
50encoding and decoding of data is made transparently as well as optional
51translation of platform-specific newline characters.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000052
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000053The easiest way to create a text stream is with :meth:`open()`, optionally
54specifying an encoding::
Antoine Pitroub530e142010-08-30 12:41:00 +000055
56 f = open("myfile.txt", "r", encoding="utf-8")
57
58In-memory text streams are also available as :class:`StringIO` objects::
59
60 f = io.StringIO("some initial text data")
61
Eli Benderskyf877a7c2012-07-14 21:22:25 +030062The text stream API is described in detail in the documentation of
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000063:class:`TextIOBase`.
Antoine Pitroub530e142010-08-30 12:41:00 +000064
Antoine Pitroub530e142010-08-30 12:41:00 +000065
66Binary I/O
67^^^^^^^^^^
68
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000069Binary I/O (also called *buffered I/O*) expects and produces :class:`bytes`
70objects. No encoding, decoding, or newline translation is performed. This
71category of streams can be used for all kinds of non-text data, and also when
72manual control over the handling of text data is desired.
Antoine Pitroub530e142010-08-30 12:41:00 +000073
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000074The easiest way to create a binary stream is with :meth:`open()` with ``'b'`` in
75the mode string::
Antoine Pitroub530e142010-08-30 12:41:00 +000076
77 f = open("myfile.jpg", "rb")
78
79In-memory binary streams are also available as :class:`BytesIO` objects::
80
81 f = io.BytesIO(b"some initial binary data: \x00\x01")
82
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000083The binary stream API is described in detail in the docs of
84:class:`BufferedIOBase`.
Antoine Pitroub530e142010-08-30 12:41:00 +000085
86Other library modules may provide additional ways to create text or binary
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000087streams. See :meth:`socket.socket.makefile` for example.
88
Antoine Pitroub530e142010-08-30 12:41:00 +000089
90Raw I/O
91^^^^^^^
92
93Raw I/O (also called *unbuffered I/O*) is generally used as a low-level
94building-block for binary and text streams; it is rarely useful to directly
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000095manipulate a raw stream from user code. Nevertheless, you can create a raw
96stream by opening a file in binary mode with buffering disabled::
Antoine Pitroub530e142010-08-30 12:41:00 +000097
98 f = open("myfile.jpg", "rb", buffering=0)
99
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000100The raw stream API is described in detail in the docs of :class:`RawIOBase`.
Benjamin Petersoncc12e1b2010-02-19 00:58:13 +0000101
Georg Brandl014197c2008-04-09 18:40:51 +0000102
Antoine Pitroub530e142010-08-30 12:41:00 +0000103High-level Module Interface
104---------------------------
Georg Brandl014197c2008-04-09 18:40:51 +0000105
106.. data:: DEFAULT_BUFFER_SIZE
107
108 An int containing the default buffer size used by the module's buffered I/O
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000109 classes. :func:`open` uses the file's blksize (as obtained by
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000110 :func:`os.stat`) if possible.
Georg Brandl014197c2008-04-09 18:40:51 +0000111
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000112
Benjamin Peterson95e392c2010-04-27 21:07:21 +0000113.. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
Georg Brandl014197c2008-04-09 18:40:51 +0000114
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000115 This is an alias for the builtin :func:`open` function.
Georg Brandl014197c2008-04-09 18:40:51 +0000116
Georg Brandl014197c2008-04-09 18:40:51 +0000117
118.. exception:: BlockingIOError
119
Antoine Pitrouf55011f2011-10-12 18:57:23 +0200120 This is a compatibility alias for the builtin :exc:`BlockingIOError`
121 exception.
Georg Brandl014197c2008-04-09 18:40:51 +0000122
123
124.. exception:: UnsupportedOperation
125
Antoine Pitroua787b652011-10-12 19:02:52 +0200126 An exception inheriting :exc:`OSError` and :exc:`ValueError` that is raised
Georg Brandl014197c2008-04-09 18:40:51 +0000127 when an unsupported operation is called on a stream.
128
129
Antoine Pitroub530e142010-08-30 12:41:00 +0000130In-memory streams
131^^^^^^^^^^^^^^^^^
132
133It is also possible to use a :class:`str` or :class:`bytes`-like object as a
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000134file for both reading and writing. For strings :class:`StringIO` can be used
135like a file opened in text mode. :class:`BytesIO` can be used like a file
136opened in binary mode. Both provide full read-write capabilities with random
137access.
Antoine Pitroub530e142010-08-30 12:41:00 +0000138
139
140.. seealso::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000141
Antoine Pitroub530e142010-08-30 12:41:00 +0000142 :mod:`sys`
143 contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`,
144 and :data:`sys.stderr`.
145
146
147Class hierarchy
148---------------
149
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000150The implementation of I/O streams is organized as a hierarchy of classes. First
151:term:`abstract base classes <abstract base class>` (ABCs), which are used to
152specify the various categories of streams, then concrete classes providing the
153standard stream implementations.
Antoine Pitroub530e142010-08-30 12:41:00 +0000154
155 .. note::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000156
157 The abstract base classes also provide default implementations of some
158 methods in order to help implementation of concrete stream classes. For
159 example, :class:`BufferedIOBase` provides unoptimized implementations of
160 ``readinto()`` and ``readline()``.
Antoine Pitroub530e142010-08-30 12:41:00 +0000161
162At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
163defines the basic interface to a stream. Note, however, that there is no
164separation between reading and writing to streams; implementations are allowed
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000165to raise :exc:`UnsupportedOperation` if they do not support a given operation.
Antoine Pitroub530e142010-08-30 12:41:00 +0000166
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000167The :class:`RawIOBase` ABC extends :class:`IOBase`. It deals with the reading
168and writing of bytes to a stream. :class:`FileIO` subclasses :class:`RawIOBase`
169to provide an interface to files in the machine's file system.
Antoine Pitroub530e142010-08-30 12:41:00 +0000170
171The :class:`BufferedIOBase` ABC deals with buffering on a raw byte stream
172(:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`,
173:class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000174readable, writable, and both readable and writable. :class:`BufferedRandom`
175provides a buffered interface to random access streams. Another
Georg Brandl682d7e02010-10-06 10:26:05 +0000176:class:`BufferedIOBase` subclass, :class:`BytesIO`, is a stream of in-memory
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000177bytes.
Antoine Pitroub530e142010-08-30 12:41:00 +0000178
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000179The :class:`TextIOBase` ABC, another subclass of :class:`IOBase`, deals with
180streams whose bytes represent text, and handles encoding and decoding to and
181from strings. :class:`TextIOWrapper`, which extends it, is a buffered text
182interface to a buffered raw stream (:class:`BufferedIOBase`). Finally,
183:class:`StringIO` is an in-memory stream for text.
Antoine Pitroub530e142010-08-30 12:41:00 +0000184
185Argument names are not part of the specification, and only the arguments of
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000186:func:`open` are intended to be used as keyword arguments.
Antoine Pitroub530e142010-08-30 12:41:00 +0000187
188
Georg Brandl014197c2008-04-09 18:40:51 +0000189I/O Base Classes
Antoine Pitroub530e142010-08-30 12:41:00 +0000190^^^^^^^^^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000191
192.. class:: IOBase
193
194 The abstract base class for all I/O classes, acting on streams of bytes.
195 There is no public constructor.
196
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000197 This class provides empty abstract implementations for many methods
198 that derived classes can override selectively; the default
199 implementations represent a file that cannot be read, written or
200 seeked.
Georg Brandl014197c2008-04-09 18:40:51 +0000201
202 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000203 or :meth:`write` because their signatures will vary, implementations and
204 clients should consider those methods part of the interface. Also,
Antoine Pitroua787b652011-10-12 19:02:52 +0200205 implementations may raise a :exc:`ValueError` (or :exc:`UnsupportedOperation`)
206 when operations they do not support are called.
Georg Brandl014197c2008-04-09 18:40:51 +0000207
208 The basic type used for binary data read from or written to a file is
209 :class:`bytes`. :class:`bytearray`\s are accepted too, and in some cases
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000210 (such as :class:`readinto`) required. Text I/O classes work with
211 :class:`str` data.
Georg Brandl014197c2008-04-09 18:40:51 +0000212
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000213 Note that calling any method (even inquiries) on a closed stream is
Antoine Pitroua787b652011-10-12 19:02:52 +0200214 undefined. Implementations may raise :exc:`ValueError` in this case.
Georg Brandl014197c2008-04-09 18:40:51 +0000215
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300216 :class:`IOBase` (and its subclasses) support the iterator protocol, meaning
217 that an :class:`IOBase` object can be iterated over yielding the lines in a
218 stream. Lines are defined slightly differently depending on whether the
219 stream is a binary stream (yielding bytes), or a text stream (yielding
220 character strings). See :meth:`~IOBase.readline` below.
Georg Brandl014197c2008-04-09 18:40:51 +0000221
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300222 :class:`IOBase` is also a context manager and therefore supports the
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000223 :keyword:`with` statement. In this example, *file* is closed after the
224 :keyword:`with` statement's suite is finished---even if an exception occurs::
Georg Brandl014197c2008-04-09 18:40:51 +0000225
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000226 with open('spam.txt', 'w') as file:
227 file.write('Spam and eggs!')
Georg Brandl014197c2008-04-09 18:40:51 +0000228
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000229 :class:`IOBase` provides these data attributes and methods:
Georg Brandl014197c2008-04-09 18:40:51 +0000230
231 .. method:: close()
232
Christian Heimesecc42a22008-11-05 19:30:32 +0000233 Flush and close this stream. This method has no effect if the file is
Georg Brandl48310cd2009-01-03 21:18:54 +0000234 already closed. Once the file is closed, any operation on the file
Georg Brandl8569e582010-05-19 20:57:08 +0000235 (e.g. reading or writing) will raise a :exc:`ValueError`.
Antoine Pitrouf9fc08f2010-04-28 19:59:32 +0000236
237 As a convenience, it is allowed to call this method more than once;
238 only the first call, however, will have an effect.
Georg Brandl014197c2008-04-09 18:40:51 +0000239
240 .. attribute:: closed
241
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300242 ``True`` if the stream is closed.
Georg Brandl014197c2008-04-09 18:40:51 +0000243
244 .. method:: fileno()
245
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000246 Return the underlying file descriptor (an integer) of the stream if it
Antoine Pitroua787b652011-10-12 19:02:52 +0200247 exists. An :exc:`OSError` is raised if the IO object does not use a file
Georg Brandl014197c2008-04-09 18:40:51 +0000248 descriptor.
249
250 .. method:: flush()
251
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000252 Flush the write buffers of the stream if applicable. This does nothing
253 for read-only and non-blocking streams.
Georg Brandl014197c2008-04-09 18:40:51 +0000254
255 .. method:: isatty()
256
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000257 Return ``True`` if the stream is interactive (i.e., connected to
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000258 a terminal/tty device).
Georg Brandl014197c2008-04-09 18:40:51 +0000259
260 .. method:: readable()
261
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000262 Return ``True`` if the stream can be read from. If False, :meth:`read`
Antoine Pitroua787b652011-10-12 19:02:52 +0200263 will raise :exc:`OSError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000264
Georg Brandl3dd33882009-06-01 17:35:27 +0000265 .. method:: readline(limit=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000266
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000267 Read and return one line from the stream. If *limit* is specified, at
268 most *limit* bytes will be read.
Georg Brandl014197c2008-04-09 18:40:51 +0000269
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000270 The line terminator is always ``b'\n'`` for binary files; for text files,
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000271 the *newlines* argument to :func:`open` can be used to select the line
Georg Brandl014197c2008-04-09 18:40:51 +0000272 terminator(s) recognized.
273
Georg Brandl3dd33882009-06-01 17:35:27 +0000274 .. method:: readlines(hint=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000275
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000276 Read and return a list of lines from the stream. *hint* can be specified
277 to control the number of lines read: no more lines will be read if the
278 total size (in bytes/characters) of all lines so far exceeds *hint*.
Georg Brandl014197c2008-04-09 18:40:51 +0000279
Georg Brandl3dd33882009-06-01 17:35:27 +0000280 .. method:: seek(offset, whence=SEEK_SET)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000281
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000282 Change the stream position to the given byte *offset*. *offset* is
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000283 interpreted relative to the position indicated by *whence*. Values for
284 *whence* are:
285
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000286 * :data:`SEEK_SET` or ``0`` -- start of the stream (the default);
287 *offset* should be zero or positive
288 * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may
289 be negative
290 * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually
291 negative
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000292
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000293 Return the new absolute position.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000294
Raymond Hettinger35a88362009-04-09 00:08:24 +0000295 .. versionadded:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000296 The ``SEEK_*`` constants.
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000297
Jesus Cea94363612012-06-22 18:32:07 +0200298 .. versionadded:: 3.3
299 Some operating systems could support additional values, like
300 :data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`. The valid values
301 for a file could depend on it being open in text or binary mode.
302
Georg Brandl014197c2008-04-09 18:40:51 +0000303 .. method:: seekable()
304
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000305 Return ``True`` if the stream supports random access. If ``False``,
Antoine Pitroua787b652011-10-12 19:02:52 +0200306 :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`OSError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000307
308 .. method:: tell()
309
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000310 Return the current stream position.
Georg Brandl014197c2008-04-09 18:40:51 +0000311
Georg Brandl3dd33882009-06-01 17:35:27 +0000312 .. method:: truncate(size=None)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000313
Antoine Pitrou2016dc92010-05-29 12:08:25 +0000314 Resize the stream to the given *size* in bytes (or the current position
315 if *size* is not specified). The current stream position isn't changed.
316 This resizing can extend or reduce the current file size. In case of
317 extension, the contents of the new file area depend on the platform
318 (on most systems, additional bytes are zero-filled, on Windows they're
319 undetermined). The new file size is returned.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000320
Georg Brandl014197c2008-04-09 18:40:51 +0000321 .. method:: writable()
322
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000323 Return ``True`` if the stream supports writing. If ``False``,
Antoine Pitroua787b652011-10-12 19:02:52 +0200324 :meth:`write` and :meth:`truncate` will raise :exc:`OSError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000325
326 .. method:: writelines(lines)
327
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000328 Write a list of lines to the stream. Line separators are not added, so it
329 is usual for each of the lines provided to have a line separator at the
330 end.
Georg Brandl014197c2008-04-09 18:40:51 +0000331
332
333.. class:: RawIOBase
334
335 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no
336 public constructor.
337
Antoine Pitrou497a7672009-09-17 17:18:01 +0000338 Raw binary I/O typically provides low-level access to an underlying OS
339 device or API, and does not try to encapsulate it in high-level primitives
340 (this is left to Buffered I/O and Text I/O, described later in this page).
341
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000342 In addition to the attributes and methods from :class:`IOBase`,
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300343 :class:`RawIOBase` provides the following methods:
Georg Brandl014197c2008-04-09 18:40:51 +0000344
Georg Brandl3dd33882009-06-01 17:35:27 +0000345 .. method:: read(n=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000346
Antoine Pitrou78ddbe62009-10-01 16:24:45 +0000347 Read up to *n* bytes from the object and return them. As a convenience,
348 if *n* is unspecified or -1, :meth:`readall` is called. Otherwise,
349 only one system call is ever made. Fewer than *n* bytes may be
350 returned if the operating system call returns fewer than *n* bytes.
351
352 If 0 bytes are returned, and *n* was not 0, this indicates end of file.
353 If the object is in non-blocking mode and no bytes are available,
354 ``None`` is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000355
Benjamin Petersonb47aace2008-04-09 21:38:38 +0000356 .. method:: readall()
Georg Brandl014197c2008-04-09 18:40:51 +0000357
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000358 Read and return all the bytes from the stream until EOF, using multiple
359 calls to the stream if necessary.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000360
361 .. method:: readinto(b)
362
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300363 Read up to ``len(b)`` bytes into :class:`bytearray` *b* and return the
364 number of bytes read. If the object is in non-blocking mode and no
Daniel Stutzbachd01df462010-11-30 17:49:53 +0000365 bytes are available, ``None`` is returned.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000366
367 .. method:: write(b)
368
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300369 Write the given :class:`bytes` or :class:`bytearray` object, *b*, to the
370 underlying raw stream and return the number of bytes written. This can
371 be less than ``len(b)``, depending on specifics of the underlying raw
372 stream, and especially if it is in non-blocking mode. ``None`` is
373 returned if the raw stream is set not to block and no single byte could
374 be readily written to it.
Georg Brandl014197c2008-04-09 18:40:51 +0000375
376
Georg Brandl014197c2008-04-09 18:40:51 +0000377.. class:: BufferedIOBase
378
Antoine Pitrou497a7672009-09-17 17:18:01 +0000379 Base class for binary streams that support some kind of buffering.
380 It inherits :class:`IOBase`. There is no public constructor.
Georg Brandl014197c2008-04-09 18:40:51 +0000381
Antoine Pitrou497a7672009-09-17 17:18:01 +0000382 The main difference with :class:`RawIOBase` is that methods :meth:`read`,
383 :meth:`readinto` and :meth:`write` will try (respectively) to read as much
384 input as requested or to consume all given output, at the expense of
385 making perhaps more than one system call.
386
387 In addition, those methods can raise :exc:`BlockingIOError` if the
388 underlying raw stream is in non-blocking mode and cannot take or give
389 enough data; unlike their :class:`RawIOBase` counterparts, they will
390 never return ``None``.
391
392 Besides, the :meth:`read` method does not have a default
Georg Brandl014197c2008-04-09 18:40:51 +0000393 implementation that defers to :meth:`readinto`.
394
Antoine Pitrou497a7672009-09-17 17:18:01 +0000395 A typical :class:`BufferedIOBase` implementation should not inherit from a
396 :class:`RawIOBase` implementation, but wrap one, like
397 :class:`BufferedWriter` and :class:`BufferedReader` do.
Georg Brandl014197c2008-04-09 18:40:51 +0000398
Senthil Kumarana6bac952011-07-04 11:28:30 -0700399 :class:`BufferedIOBase` provides or overrides these methods and attribute in
400 addition to those from :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000401
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000402 .. attribute:: raw
403
404 The underlying raw stream (a :class:`RawIOBase` instance) that
405 :class:`BufferedIOBase` deals with. This is not part of the
406 :class:`BufferedIOBase` API and may not exist on some implementations.
407
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000408 .. method:: detach()
409
410 Separate the underlying raw stream from the buffer and return it.
411
412 After the raw stream has been detached, the buffer is in an unusable
413 state.
414
415 Some buffers, like :class:`BytesIO`, do not have the concept of a single
416 raw stream to return from this method. They raise
417 :exc:`UnsupportedOperation`.
418
Benjamin Petersonedc36472009-05-01 20:48:14 +0000419 .. versionadded:: 3.1
420
Georg Brandl3dd33882009-06-01 17:35:27 +0000421 .. method:: read(n=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000422
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000423 Read and return up to *n* bytes. If the argument is omitted, ``None``, or
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300424 negative, data is read and returned until EOF is reached. An empty
425 :class:`bytes` object is returned if the stream is already at EOF.
Georg Brandl014197c2008-04-09 18:40:51 +0000426
427 If the argument is positive, and the underlying raw stream is not
428 interactive, multiple raw reads may be issued to satisfy the byte count
429 (unless EOF is reached first). But for interactive raw streams, at most
430 one raw read will be issued, and a short result does not imply that EOF is
431 imminent.
432
Antoine Pitrou497a7672009-09-17 17:18:01 +0000433 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
434 non blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000435
Georg Brandl3dd33882009-06-01 17:35:27 +0000436 .. method:: read1(n=-1)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000437
438 Read and return up to *n* bytes, with at most one call to the underlying
Antoine Pitrou497a7672009-09-17 17:18:01 +0000439 raw stream's :meth:`~RawIOBase.read` method. This can be useful if you
440 are implementing your own buffering on top of a :class:`BufferedIOBase`
441 object.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000442
Georg Brandl014197c2008-04-09 18:40:51 +0000443 .. method:: readinto(b)
444
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300445 Read up to ``len(b)`` bytes into bytearray *b* and return the number of
446 bytes read.
Georg Brandl014197c2008-04-09 18:40:51 +0000447
448 Like :meth:`read`, multiple reads may be issued to the underlying raw
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300449 stream, unless the latter is interactive.
Georg Brandl014197c2008-04-09 18:40:51 +0000450
Antoine Pitrou497a7672009-09-17 17:18:01 +0000451 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
452 non blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000453
Georg Brandl014197c2008-04-09 18:40:51 +0000454 .. method:: write(b)
455
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300456 Write the given :class:`bytes` or :class:`bytearray` object, *b* and
457 return the number of bytes written (never less than ``len(b)``, since if
458 the write fails an :exc:`OSError` will be raised). Depending on the
459 actual implementation, these bytes may be readily written to the
460 underlying stream, or held in a buffer for performance and latency
461 reasons.
Georg Brandl014197c2008-04-09 18:40:51 +0000462
Antoine Pitrou497a7672009-09-17 17:18:01 +0000463 When in non-blocking mode, a :exc:`BlockingIOError` is raised if the
464 data needed to be written to the raw stream but it couldn't accept
465 all the data without blocking.
Georg Brandl014197c2008-04-09 18:40:51 +0000466
467
Benjamin Petersonaa069002009-01-23 03:26:36 +0000468Raw File I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000469^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000470
Ross Lagerwall59142db2011-10-31 20:34:46 +0200471.. class:: FileIO(name, mode='r', closefd=True, opener=None)
Benjamin Petersonaa069002009-01-23 03:26:36 +0000472
Antoine Pitrou497a7672009-09-17 17:18:01 +0000473 :class:`FileIO` represents an OS-level file containing bytes data.
474 It implements the :class:`RawIOBase` interface (and therefore the
475 :class:`IOBase` interface, too).
476
477 The *name* can be one of two things:
478
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300479 * a character string or :class:`bytes` object representing the path to the
480 file which will be opened;
Antoine Pitrou497a7672009-09-17 17:18:01 +0000481 * an integer representing the number of an existing OS-level file descriptor
482 to which the resulting :class:`FileIO` object will give access.
Benjamin Petersonaa069002009-01-23 03:26:36 +0000483
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100484 The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading
Charles-François Natalid612de12012-01-14 11:51:00 +0100485 (default), writing, exclusive creation or appending. The file will be
486 created if it doesn't exist when opened for writing or appending; it will be
487 truncated when opened for writing. :exc:`FileExistsError` will be raised if
488 it already exists when opened for creating. Opening a file for creating
489 implies writing, so this mode behaves in a similar way to ``'w'``. Add a
490 ``'+'`` to the mode to allow simultaneous reading and writing.
Benjamin Petersonaa069002009-01-23 03:26:36 +0000491
Antoine Pitrou497a7672009-09-17 17:18:01 +0000492 The :meth:`read` (when called with a positive argument), :meth:`readinto`
493 and :meth:`write` methods on this class will only make one system call.
494
Ross Lagerwall59142db2011-10-31 20:34:46 +0200495 A custom opener can be used by passing a callable as *opener*. The underlying
496 file descriptor for the file object is then obtained by calling *opener* with
497 (*name*, *flags*). *opener* must return an open file descriptor (passing
498 :mod:`os.open` as *opener* results in functionality similar to passing
499 ``None``).
500
501 .. versionchanged:: 3.3
502 The *opener* parameter was added.
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100503 The ``'x'`` mode was added.
Ross Lagerwall59142db2011-10-31 20:34:46 +0200504
Benjamin Petersonaa069002009-01-23 03:26:36 +0000505 In addition to the attributes and methods from :class:`IOBase` and
506 :class:`RawIOBase`, :class:`FileIO` provides the following data
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300507 attributes:
Benjamin Petersonaa069002009-01-23 03:26:36 +0000508
509 .. attribute:: mode
510
511 The mode as given in the constructor.
512
513 .. attribute:: name
514
515 The file name. This is the file descriptor of the file when no name is
516 given in the constructor.
517
Benjamin Petersonaa069002009-01-23 03:26:36 +0000518
519Buffered Streams
Antoine Pitroub530e142010-08-30 12:41:00 +0000520^^^^^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000521
Antoine Pitroubed81c82010-12-03 19:14:17 +0000522Buffered I/O streams provide a higher-level interface to an I/O device
523than raw I/O does.
Antoine Pitrou497a7672009-09-17 17:18:01 +0000524
Georg Brandl014197c2008-04-09 18:40:51 +0000525.. class:: BytesIO([initial_bytes])
526
527 A stream implementation using an in-memory bytes buffer. It inherits
528 :class:`BufferedIOBase`.
529
Antoine Pitroub530e142010-08-30 12:41:00 +0000530 The argument *initial_bytes* contains optional initial :class:`bytes` data.
Georg Brandl014197c2008-04-09 18:40:51 +0000531
532 :class:`BytesIO` provides or overrides these methods in addition to those
533 from :class:`BufferedIOBase` and :class:`IOBase`:
534
Antoine Pitrou972ee132010-09-06 18:48:21 +0000535 .. method:: getbuffer()
536
537 Return a readable and writable view over the contents of the buffer
538 without copying them. Also, mutating the view will transparently
539 update the contents of the buffer::
540
541 >>> b = io.BytesIO(b"abcdef")
542 >>> view = b.getbuffer()
543 >>> view[2:4] = b"56"
544 >>> b.getvalue()
545 b'ab56ef'
546
547 .. note::
548 As long as the view exists, the :class:`BytesIO` object cannot be
549 resized.
550
551 .. versionadded:: 3.2
552
Georg Brandl014197c2008-04-09 18:40:51 +0000553 .. method:: getvalue()
554
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300555 Return :class:`bytes` containing the entire contents of the buffer.
Georg Brandl014197c2008-04-09 18:40:51 +0000556
557 .. method:: read1()
558
Benjamin Peterson9efcc4b2008-04-14 21:30:21 +0000559 In :class:`BytesIO`, this is the same as :meth:`read`.
Georg Brandl014197c2008-04-09 18:40:51 +0000560
Georg Brandl014197c2008-04-09 18:40:51 +0000561
Georg Brandl3dd33882009-06-01 17:35:27 +0000562.. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000563
Antoine Pitrou497a7672009-09-17 17:18:01 +0000564 A buffer providing higher-level access to a readable, sequential
565 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
566 When reading data from this object, a larger amount of data may be
567 requested from the underlying raw stream, and kept in an internal buffer.
568 The buffered data can then be returned directly on subsequent reads.
Georg Brandl014197c2008-04-09 18:40:51 +0000569
570 The constructor creates a :class:`BufferedReader` for the given readable
571 *raw* stream and *buffer_size*. If *buffer_size* is omitted,
572 :data:`DEFAULT_BUFFER_SIZE` is used.
573
574 :class:`BufferedReader` provides or overrides these methods in addition to
575 those from :class:`BufferedIOBase` and :class:`IOBase`:
576
577 .. method:: peek([n])
578
Benjamin Petersonc43a26d2009-06-16 23:09:24 +0000579 Return bytes from the stream without advancing the position. At most one
Benjamin Peterson2a8b54d2009-06-14 14:37:23 +0000580 single read on the raw stream is done to satisfy the call. The number of
581 bytes returned may be less or more than requested.
Georg Brandl014197c2008-04-09 18:40:51 +0000582
583 .. method:: read([n])
584
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000585 Read and return *n* bytes, or if *n* is not given or negative, until EOF
Georg Brandl014197c2008-04-09 18:40:51 +0000586 or if the read call would block in non-blocking mode.
587
588 .. method:: read1(n)
589
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000590 Read and return up to *n* bytes with only one call on the raw stream. If
Georg Brandl014197c2008-04-09 18:40:51 +0000591 at least one byte is buffered, only buffered bytes are returned.
592 Otherwise, one raw stream read call is made.
593
594
Georg Brandl3dd33882009-06-01 17:35:27 +0000595.. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000596
Antoine Pitrou497a7672009-09-17 17:18:01 +0000597 A buffer providing higher-level access to a writeable, sequential
598 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300599 When writing to this object, data is normally placed into an internal
Antoine Pitrou497a7672009-09-17 17:18:01 +0000600 buffer. The buffer will be written out to the underlying :class:`RawIOBase`
601 object under various conditions, including:
602
603 * when the buffer gets too small for all pending data;
604 * when :meth:`flush()` is called;
605 * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects);
606 * when the :class:`BufferedWriter` object is closed or destroyed.
Georg Brandl014197c2008-04-09 18:40:51 +0000607
608 The constructor creates a :class:`BufferedWriter` for the given writeable
609 *raw* stream. If the *buffer_size* is not given, it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000610 :data:`DEFAULT_BUFFER_SIZE`.
611
Georg Brandl014197c2008-04-09 18:40:51 +0000612 :class:`BufferedWriter` provides or overrides these methods in addition to
613 those from :class:`BufferedIOBase` and :class:`IOBase`:
614
615 .. method:: flush()
616
617 Force bytes held in the buffer into the raw stream. A
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000618 :exc:`BlockingIOError` should be raised if the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000619
620 .. method:: write(b)
621
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300622 Write the :class:`bytes` or :class:`bytearray` object, *b* and return the
623 number of bytes written. When in non-blocking mode, a
624 :exc:`BlockingIOError` is raised if the buffer needs to be written out but
625 the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000626
627
Georg Brandl3dd33882009-06-01 17:35:27 +0000628.. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000629
630 A buffered interface to random access streams. It inherits
Antoine Pitrou497a7672009-09-17 17:18:01 +0000631 :class:`BufferedReader` and :class:`BufferedWriter`, and further supports
632 :meth:`seek` and :meth:`tell` functionality.
Georg Brandl014197c2008-04-09 18:40:51 +0000633
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000634 The constructor creates a reader and writer for a seekable raw stream, given
Georg Brandl014197c2008-04-09 18:40:51 +0000635 in the first argument. If the *buffer_size* is omitted it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000636 :data:`DEFAULT_BUFFER_SIZE`.
637
Georg Brandl014197c2008-04-09 18:40:51 +0000638 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
639 :class:`BufferedWriter` can do.
640
641
Antoine Pitrou13d28952011-08-20 19:48:43 +0200642.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE)
643
644 A buffered I/O object combining two unidirectional :class:`RawIOBase`
645 objects -- one readable, the other writeable -- into a single bidirectional
646 endpoint. It inherits :class:`BufferedIOBase`.
647
648 *reader* and *writer* are :class:`RawIOBase` objects that are readable and
649 writeable respectively. If the *buffer_size* is omitted it defaults to
650 :data:`DEFAULT_BUFFER_SIZE`.
651
Antoine Pitrou13d28952011-08-20 19:48:43 +0200652 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods
653 except for :meth:`~BufferedIOBase.detach`, which raises
654 :exc:`UnsupportedOperation`.
655
656 .. warning::
657 :class:`BufferedRWPair` does not attempt to synchronize accesses to
658 its underlying raw streams. You should not pass it the same object
659 as reader and writer; use :class:`BufferedRandom` instead.
660
661
Georg Brandl014197c2008-04-09 18:40:51 +0000662Text I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000663^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000664
665.. class:: TextIOBase
666
667 Base class for text streams. This class provides a character and line based
668 interface to stream I/O. There is no :meth:`readinto` method because
669 Python's character strings are immutable. It inherits :class:`IOBase`.
670 There is no public constructor.
671
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000672 :class:`TextIOBase` provides or overrides these data attributes and
673 methods in addition to those from :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000674
675 .. attribute:: encoding
676
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000677 The name of the encoding used to decode the stream's bytes into
Georg Brandl014197c2008-04-09 18:40:51 +0000678 strings, and to encode strings into bytes.
679
Benjamin Peterson0926ad12009-06-06 18:02:12 +0000680 .. attribute:: errors
681
682 The error setting of the decoder or encoder.
683
Georg Brandl014197c2008-04-09 18:40:51 +0000684 .. attribute:: newlines
685
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000686 A string, a tuple of strings, or ``None``, indicating the newlines
Antoine Pitrou497a7672009-09-17 17:18:01 +0000687 translated so far. Depending on the implementation and the initial
688 constructor flags, this may not be available.
Georg Brandl014197c2008-04-09 18:40:51 +0000689
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000690 .. attribute:: buffer
691
692 The underlying binary buffer (a :class:`BufferedIOBase` instance) that
693 :class:`TextIOBase` deals with. This is not part of the
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300694 :class:`TextIOBase` API and may not exist in some implementations.
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000695
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000696 .. method:: detach()
697
Antoine Pitrou497a7672009-09-17 17:18:01 +0000698 Separate the underlying binary buffer from the :class:`TextIOBase` and
699 return it.
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000700
701 After the underlying buffer has been detached, the :class:`TextIOBase` is
702 in an unusable state.
703
704 Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not
705 have the concept of an underlying buffer and calling this method will
706 raise :exc:`UnsupportedOperation`.
707
Benjamin Petersonedc36472009-05-01 20:48:14 +0000708 .. versionadded:: 3.1
709
Georg Brandl014197c2008-04-09 18:40:51 +0000710 .. method:: read(n)
711
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000712 Read and return at most *n* characters from the stream as a single
Antoine Pitrou497a7672009-09-17 17:18:01 +0000713 :class:`str`. If *n* is negative or ``None``, reads until EOF.
Georg Brandl014197c2008-04-09 18:40:51 +0000714
Antoine Pitrou707bd4e2012-07-25 22:38:33 +0200715 .. method:: readline(limit=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000716
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000717 Read until newline or EOF and return a single ``str``. If the stream is
718 already at EOF, an empty string is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000719
Antoine Pitrou707bd4e2012-07-25 22:38:33 +0200720 If *limit* is specified, at most *limit* characters will be read.
721
Antoine Pitrouf49d1522012-01-21 20:20:49 +0100722 .. method:: seek(offset, whence=SEEK_SET)
723
724 Change the stream position to the given *offset*. Behaviour depends
725 on the *whence* parameter:
726
727 * :data:`SEEK_SET` or ``0``: seek from the start of the stream
728 (the default); *offset* must either be a number returned by
729 :meth:`TextIOBase.tell`, or zero. Any other *offset* value
730 produces undefined behaviour.
731 * :data:`SEEK_CUR` or ``1``: "seek" to the current position;
732 *offset* must be zero, which is a no-operation (all other values
733 are unsupported).
734 * :data:`SEEK_END` or ``2``: seek to the end of the stream;
735 *offset* must be zero (all other values are unsupported).
736
737 Return the new absolute position as an opaque number.
738
739 .. versionadded:: 3.1
740 The ``SEEK_*`` constants.
741
742 .. method:: tell()
743
744 Return the current stream position as an opaque number. The number
745 does not usually represent a number of bytes in the underlying
746 binary storage.
747
Georg Brandl014197c2008-04-09 18:40:51 +0000748 .. method:: write(s)
749
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000750 Write the string *s* to the stream and return the number of characters
751 written.
Georg Brandl014197c2008-04-09 18:40:51 +0000752
753
Antoine Pitrou664091b2011-07-23 22:00:03 +0200754.. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, \
755 line_buffering=False, write_through=False)
Georg Brandl014197c2008-04-09 18:40:51 +0000756
Antoine Pitrou497a7672009-09-17 17:18:01 +0000757 A buffered text stream over a :class:`BufferedIOBase` binary stream.
Georg Brandl014197c2008-04-09 18:40:51 +0000758 It inherits :class:`TextIOBase`.
759
760 *encoding* gives the name of the encoding that the stream will be decoded or
Andrew Svetlov4805fa82012-08-13 22:11:14 +0300761 encoded with. It defaults to
762 :func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`.
Georg Brandl014197c2008-04-09 18:40:51 +0000763
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000764 *errors* is an optional string that specifies how encoding and decoding
765 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError`
766 exception if there is an encoding error (the default of ``None`` has the same
767 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding
768 errors can lead to data loss.) ``'replace'`` causes a replacement marker
Christian Heimesa342c012008-04-20 21:01:16 +0000769 (such as ``'?'``) to be inserted where there is malformed data. When
770 writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character
771 reference) or ``'backslashreplace'`` (replace with backslashed escape
772 sequences) can be used. Any other error handling name that has been
773 registered with :func:`codecs.register_error` is also valid.
Georg Brandl014197c2008-04-09 18:40:51 +0000774
R David Murray1b00f252012-08-15 10:43:58 -0400775 .. index::
776 single: universal newlines; io.TextIOWrapper class
777
Antoine Pitrou0c1c0d42012-08-04 00:55:38 +0200778 *newline* controls how line endings are handled. It can be ``None``,
779 ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows:
780
R David Murray1b00f252012-08-15 10:43:58 -0400781 * When reading input from the stream, if *newline* is ``None``,
R David Murrayee0a9452012-08-15 11:05:36 -0400782 :term:`universal newlines` mode is enabled. Lines in the input can end in
783 ``'\n'``, ``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'``
784 before being returned to the caller. If it is ``''``, universal newlines
785 mode is enabled, but line endings are returned to the caller untranslated.
786 If it has any of the other legal values, input lines are only terminated
787 by the given string, and the line ending is returned to the caller
788 untranslated.
Antoine Pitrou0c1c0d42012-08-04 00:55:38 +0200789
Georg Brandl296d1be2012-08-14 09:39:07 +0200790 * When writing output to the stream, if *newline* is ``None``, any ``'\n'``
791 characters written are translated to the system default line separator,
792 :data:`os.linesep`. If *newline* is ``''`` or ``'\n'``, no translation
793 takes place. If *newline* is any of the other legal values, any ``'\n'``
794 characters written are translated to the given string.
Georg Brandl014197c2008-04-09 18:40:51 +0000795
796 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
797 write contains a newline character.
798
Antoine Pitrou664091b2011-07-23 22:00:03 +0200799 If *write_through* is ``True``, calls to :meth:`write` are guaranteed
800 not to be buffered: any data written on the :class:`TextIOWrapper`
801 object is immediately handled to its underlying binary *buffer*.
802
803 .. versionchanged:: 3.3
804 The *write_through* argument has been added.
805
Victor Stinnerf86a5e82012-06-05 13:43:22 +0200806 .. versionchanged:: 3.3
807 The default *encoding* is now ``locale.getpreferredencoding(False)``
808 instead of ``locale.getpreferredencoding()``. Don't change temporary the
809 locale encoding using :func:`locale.setlocale`, use the current locale
810 encoding instead of the user preferred encoding.
811
Benjamin Peterson0926ad12009-06-06 18:02:12 +0000812 :class:`TextIOWrapper` provides one attribute in addition to those of
Georg Brandl014197c2008-04-09 18:40:51 +0000813 :class:`TextIOBase` and its parents:
814
Georg Brandl014197c2008-04-09 18:40:51 +0000815 .. attribute:: line_buffering
816
817 Whether line buffering is enabled.
Georg Brandl48310cd2009-01-03 21:18:54 +0000818
Georg Brandl014197c2008-04-09 18:40:51 +0000819
Georg Brandl3dd33882009-06-01 17:35:27 +0000820.. class:: StringIO(initial_value='', newline=None)
Georg Brandl014197c2008-04-09 18:40:51 +0000821
Antoine Pitroub530e142010-08-30 12:41:00 +0000822 An in-memory stream for text I/O.
Georg Brandl014197c2008-04-09 18:40:51 +0000823
Benjamin Petersonaa1c8d82009-03-09 02:02:23 +0000824 The initial value of the buffer (an empty string by default) can be set by
825 providing *initial_value*. The *newline* argument works like that of
826 :class:`TextIOWrapper`. The default is to do no newline translation.
Georg Brandl014197c2008-04-09 18:40:51 +0000827
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000828 :class:`StringIO` provides this method in addition to those from
Antoine Pitroub530e142010-08-30 12:41:00 +0000829 :class:`TextIOBase` and its parents:
Georg Brandl014197c2008-04-09 18:40:51 +0000830
831 .. method:: getvalue()
832
Georg Brandl2932d932008-05-30 06:27:09 +0000833 Return a ``str`` containing the entire contents of the buffer at any
834 time before the :class:`StringIO` object's :meth:`close` method is
835 called.
Georg Brandl014197c2008-04-09 18:40:51 +0000836
Georg Brandl2932d932008-05-30 06:27:09 +0000837 Example usage::
838
839 import io
840
841 output = io.StringIO()
842 output.write('First line.\n')
843 print('Second line.', file=output)
844
845 # Retrieve file contents -- this will be
846 # 'First line.\nSecond line.\n'
847 contents = output.getvalue()
848
Georg Brandl48310cd2009-01-03 21:18:54 +0000849 # Close object and discard memory buffer --
Georg Brandl2932d932008-05-30 06:27:09 +0000850 # .getvalue() will now raise an exception.
851 output.close()
Georg Brandl014197c2008-04-09 18:40:51 +0000852
Antoine Pitroub530e142010-08-30 12:41:00 +0000853
R David Murray1b00f252012-08-15 10:43:58 -0400854.. index::
855 single: universal newlines; io.IncrementalNewlineDecoder class
856
Georg Brandl014197c2008-04-09 18:40:51 +0000857.. class:: IncrementalNewlineDecoder
858
R David Murray1b00f252012-08-15 10:43:58 -0400859 A helper codec that decodes newlines for :term:`universal newlines` mode.
860 It inherits :class:`codecs.IncrementalDecoder`.
Georg Brandl014197c2008-04-09 18:40:51 +0000861
Antoine Pitroubed81c82010-12-03 19:14:17 +0000862
Antoine Pitroubed81c82010-12-03 19:14:17 +0000863Performance
Benjamin Petersonedf51322011-02-24 03:03:46 +0000864-----------
865
866This section discusses the performance of the provided concrete I/O
867implementations.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000868
869Binary I/O
Benjamin Petersonedf51322011-02-24 03:03:46 +0000870^^^^^^^^^^
Antoine Pitroubed81c82010-12-03 19:14:17 +0000871
Benjamin Petersonedf51322011-02-24 03:03:46 +0000872By reading and writing only large chunks of data even when the user asks for a
873single byte, buffered I/O hides any inefficiency in calling and executing the
874operating system's unbuffered I/O routines. The gain depends on the OS and the
875kind of I/O which is performed. For example, on some modern OSes such as Linux,
876unbuffered disk I/O can be as fast as buffered I/O. The bottom line, however,
877is that buffered I/O offers predictable performance regardless of the platform
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300878and the backing device. Therefore, it is almost always preferable to use
879buffered I/O rather than unbuffered I/O for binary data.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000880
881Text I/O
Benjamin Petersonedf51322011-02-24 03:03:46 +0000882^^^^^^^^
Antoine Pitroubed81c82010-12-03 19:14:17 +0000883
884Text I/O over a binary storage (such as a file) is significantly slower than
Benjamin Petersonedf51322011-02-24 03:03:46 +0000885binary I/O over the same storage, because it requires conversions between
886unicode and binary data using a character codec. This can become noticeable
887handling huge amounts of text data like large log files. Also,
888:meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both quite slow
889due to the reconstruction algorithm used.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000890
891:class:`StringIO`, however, is a native in-memory unicode container and will
892exhibit similar speed to :class:`BytesIO`.
893
894Multi-threading
895^^^^^^^^^^^^^^^
896
Benjamin Petersonedf51322011-02-24 03:03:46 +0000897:class:`FileIO` objects are thread-safe to the extent that the operating system
898calls (such as ``read(2)`` under Unix) they wrap are thread-safe too.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000899
900Binary buffered objects (instances of :class:`BufferedReader`,
901:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
902protect their internal structures using a lock; it is therefore safe to call
903them from multiple threads at once.
904
905:class:`TextIOWrapper` objects are not thread-safe.
906
907Reentrancy
908^^^^^^^^^^
909
910Binary buffered objects (instances of :class:`BufferedReader`,
911:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
912are not reentrant. While reentrant calls will not happen in normal situations,
Benjamin Petersonedf51322011-02-24 03:03:46 +0000913they can arise from doing I/O in a :mod:`signal` handler. If a thread tries to
Eli Benderskyf877a7c2012-07-14 21:22:25 +0300914re-enter a buffered object which it is already accessing, a :exc:`RuntimeError`
915is raised. Note this doesn't prohibit a different thread from entering the
Benjamin Petersonedf51322011-02-24 03:03:46 +0000916buffered object.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000917
Benjamin Petersonedf51322011-02-24 03:03:46 +0000918The above implicitly extends to text files, since the :func:`open()` function
919will wrap a buffered object inside a :class:`TextIOWrapper`. This includes
920standard streams and therefore affects the built-in function :func:`print()` as
921well.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000922