blob: 62eaf6d7be3c610cc447050a0239d89bf6d1b418 [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
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000040
Antoine Pitroub530e142010-08-30 12:41:00 +000041Text I/O
42^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +000043
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000044Text I/O expects and produces :class:`str` objects. This means that whenever
45the backing store is natively made of bytes (such as in the case of a file),
46encoding and decoding of data is made transparently as well as optional
47translation of platform-specific newline characters.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000048
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000049The easiest way to create a text stream is with :meth:`open()`, optionally
50specifying an encoding::
Antoine Pitroub530e142010-08-30 12:41:00 +000051
52 f = open("myfile.txt", "r", encoding="utf-8")
53
54In-memory text streams are also available as :class:`StringIO` objects::
55
56 f = io.StringIO("some initial text data")
57
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000058The text stream API is described in detail in the documentation for the
59:class:`TextIOBase`.
Antoine Pitroub530e142010-08-30 12:41:00 +000060
Antoine Pitroub530e142010-08-30 12:41:00 +000061
62Binary I/O
63^^^^^^^^^^
64
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000065Binary I/O (also called *buffered I/O*) expects and produces :class:`bytes`
66objects. No encoding, decoding, or newline translation is performed. This
67category of streams can be used for all kinds of non-text data, and also when
68manual control over the handling of text data is desired.
Antoine Pitroub530e142010-08-30 12:41:00 +000069
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000070The easiest way to create a binary stream is with :meth:`open()` with ``'b'`` in
71the mode string::
Antoine Pitroub530e142010-08-30 12:41:00 +000072
73 f = open("myfile.jpg", "rb")
74
75In-memory binary streams are also available as :class:`BytesIO` objects::
76
77 f = io.BytesIO(b"some initial binary data: \x00\x01")
78
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000079The binary stream API is described in detail in the docs of
80:class:`BufferedIOBase`.
Antoine Pitroub530e142010-08-30 12:41:00 +000081
82Other library modules may provide additional ways to create text or binary
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000083streams. See :meth:`socket.socket.makefile` for example.
84
Antoine Pitroub530e142010-08-30 12:41:00 +000085
86Raw I/O
87^^^^^^^
88
89Raw I/O (also called *unbuffered I/O*) is generally used as a low-level
90building-block for binary and text streams; it is rarely useful to directly
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000091manipulate a raw stream from user code. Nevertheless, you can create a raw
92stream by opening a file in binary mode with buffering disabled::
Antoine Pitroub530e142010-08-30 12:41:00 +000093
94 f = open("myfile.jpg", "rb", buffering=0)
95
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000096The raw stream API is described in detail in the docs of :class:`RawIOBase`.
Benjamin Petersoncc12e1b2010-02-19 00:58:13 +000097
Georg Brandl014197c2008-04-09 18:40:51 +000098
Antoine Pitroub530e142010-08-30 12:41:00 +000099High-level Module Interface
100---------------------------
Georg Brandl014197c2008-04-09 18:40:51 +0000101
102.. data:: DEFAULT_BUFFER_SIZE
103
104 An int containing the default buffer size used by the module's buffered I/O
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000105 classes. :func:`open` uses the file's blksize (as obtained by
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000106 :func:`os.stat`) if possible.
Georg Brandl014197c2008-04-09 18:40:51 +0000107
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000108
Benjamin Peterson95e392c2010-04-27 21:07:21 +0000109.. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
Georg Brandl014197c2008-04-09 18:40:51 +0000110
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000111 This is an alias for the builtin :func:`open` function.
Georg Brandl014197c2008-04-09 18:40:51 +0000112
Georg Brandl014197c2008-04-09 18:40:51 +0000113
114.. exception:: BlockingIOError
115
116 Error raised when blocking would occur on a non-blocking stream. It inherits
117 :exc:`IOError`.
118
119 In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one
120 attribute:
121
122 .. attribute:: characters_written
123
124 An integer containing the number of characters written to the stream
125 before it blocked.
126
127
128.. exception:: UnsupportedOperation
129
130 An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised
131 when an unsupported operation is called on a stream.
132
133
Antoine Pitroub530e142010-08-30 12:41:00 +0000134In-memory streams
135^^^^^^^^^^^^^^^^^
136
137It is also possible to use a :class:`str` or :class:`bytes`-like object as a
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000138file for both reading and writing. For strings :class:`StringIO` can be used
139like a file opened in text mode. :class:`BytesIO` can be used like a file
140opened in binary mode. Both provide full read-write capabilities with random
141access.
Antoine Pitroub530e142010-08-30 12:41:00 +0000142
143
144.. seealso::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000145
Antoine Pitroub530e142010-08-30 12:41:00 +0000146 :mod:`sys`
147 contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`,
148 and :data:`sys.stderr`.
149
150
151Class hierarchy
152---------------
153
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000154The implementation of I/O streams is organized as a hierarchy of classes. First
155:term:`abstract base classes <abstract base class>` (ABCs), which are used to
156specify the various categories of streams, then concrete classes providing the
157standard stream implementations.
Antoine Pitroub530e142010-08-30 12:41:00 +0000158
159 .. note::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000160
161 The abstract base classes also provide default implementations of some
162 methods in order to help implementation of concrete stream classes. For
163 example, :class:`BufferedIOBase` provides unoptimized implementations of
164 ``readinto()`` and ``readline()``.
Antoine Pitroub530e142010-08-30 12:41:00 +0000165
166At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
167defines the basic interface to a stream. Note, however, that there is no
168separation between reading and writing to streams; implementations are allowed
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000169to raise :exc:`UnsupportedOperation` if they do not support a given operation.
Antoine Pitroub530e142010-08-30 12:41:00 +0000170
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000171The :class:`RawIOBase` ABC extends :class:`IOBase`. It deals with the reading
172and writing of bytes to a stream. :class:`FileIO` subclasses :class:`RawIOBase`
173to provide an interface to files in the machine's file system.
Antoine Pitroub530e142010-08-30 12:41:00 +0000174
175The :class:`BufferedIOBase` ABC deals with buffering on a raw byte stream
176(:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`,
177:class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000178readable, writable, and both readable and writable. :class:`BufferedRandom`
179provides a buffered interface to random access streams. Another
Georg Brandl682d7e02010-10-06 10:26:05 +0000180:class:`BufferedIOBase` subclass, :class:`BytesIO`, is a stream of in-memory
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000181bytes.
Antoine Pitroub530e142010-08-30 12:41:00 +0000182
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000183The :class:`TextIOBase` ABC, another subclass of :class:`IOBase`, deals with
184streams whose bytes represent text, and handles encoding and decoding to and
185from strings. :class:`TextIOWrapper`, which extends it, is a buffered text
186interface to a buffered raw stream (:class:`BufferedIOBase`). Finally,
187:class:`StringIO` is an in-memory stream for text.
Antoine Pitroub530e142010-08-30 12:41:00 +0000188
189Argument names are not part of the specification, and only the arguments of
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000190:func:`open` are intended to be used as keyword arguments.
Antoine Pitroub530e142010-08-30 12:41:00 +0000191
192
Georg Brandl014197c2008-04-09 18:40:51 +0000193I/O Base Classes
Antoine Pitroub530e142010-08-30 12:41:00 +0000194^^^^^^^^^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000195
196.. class:: IOBase
197
198 The abstract base class for all I/O classes, acting on streams of bytes.
199 There is no public constructor.
200
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000201 This class provides empty abstract implementations for many methods
202 that derived classes can override selectively; the default
203 implementations represent a file that cannot be read, written or
204 seeked.
Georg Brandl014197c2008-04-09 18:40:51 +0000205
206 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000207 or :meth:`write` because their signatures will vary, implementations and
208 clients should consider those methods part of the interface. Also,
209 implementations may raise a :exc:`IOError` when operations they do not
210 support are called.
Georg Brandl014197c2008-04-09 18:40:51 +0000211
212 The basic type used for binary data read from or written to a file is
213 :class:`bytes`. :class:`bytearray`\s are accepted too, and in some cases
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000214 (such as :class:`readinto`) required. Text I/O classes work with
215 :class:`str` data.
Georg Brandl014197c2008-04-09 18:40:51 +0000216
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000217 Note that calling any method (even inquiries) on a closed stream is
218 undefined. Implementations may raise :exc:`IOError` in this case.
Georg Brandl014197c2008-04-09 18:40:51 +0000219
220 IOBase (and its subclasses) support the iterator protocol, meaning that an
221 :class:`IOBase` object can be iterated over yielding the lines in a stream.
Antoine Pitrou497a7672009-09-17 17:18:01 +0000222 Lines are defined slightly differently depending on whether the stream is
223 a binary stream (yielding bytes), or a text stream (yielding character
Meador Inge777bebb2011-12-03 12:29:54 -0600224 strings). See :meth:`~IOBase.readline` below.
Georg Brandl014197c2008-04-09 18:40:51 +0000225
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000226 IOBase is also a context manager and therefore supports the
227 :keyword:`with` statement. In this example, *file* is closed after the
228 :keyword:`with` statement's suite is finished---even if an exception occurs::
Georg Brandl014197c2008-04-09 18:40:51 +0000229
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000230 with open('spam.txt', 'w') as file:
231 file.write('Spam and eggs!')
Georg Brandl014197c2008-04-09 18:40:51 +0000232
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000233 :class:`IOBase` provides these data attributes and methods:
Georg Brandl014197c2008-04-09 18:40:51 +0000234
235 .. method:: close()
236
Christian Heimesecc42a22008-11-05 19:30:32 +0000237 Flush and close this stream. This method has no effect if the file is
Georg Brandl48310cd2009-01-03 21:18:54 +0000238 already closed. Once the file is closed, any operation on the file
Georg Brandl8569e582010-05-19 20:57:08 +0000239 (e.g. reading or writing) will raise a :exc:`ValueError`.
Antoine Pitrouf9fc08f2010-04-28 19:59:32 +0000240
241 As a convenience, it is allowed to call this method more than once;
242 only the first call, however, will have an effect.
Georg Brandl014197c2008-04-09 18:40:51 +0000243
244 .. attribute:: closed
245
246 True if the stream is closed.
247
248 .. method:: fileno()
249
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000250 Return the underlying file descriptor (an integer) of the stream if it
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000251 exists. An :exc:`IOError` is raised if the IO object does not use a file
Georg Brandl014197c2008-04-09 18:40:51 +0000252 descriptor.
253
254 .. method:: flush()
255
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000256 Flush the write buffers of the stream if applicable. This does nothing
257 for read-only and non-blocking streams.
Georg Brandl014197c2008-04-09 18:40:51 +0000258
259 .. method:: isatty()
260
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000261 Return ``True`` if the stream is interactive (i.e., connected to
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000262 a terminal/tty device).
Georg Brandl014197c2008-04-09 18:40:51 +0000263
264 .. method:: readable()
265
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000266 Return ``True`` if the stream can be read from. If False, :meth:`read`
267 will raise :exc:`IOError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000268
Georg Brandl3dd33882009-06-01 17:35:27 +0000269 .. method:: readline(limit=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000270
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000271 Read and return one line from the stream. If *limit* is specified, at
272 most *limit* bytes will be read.
Georg Brandl014197c2008-04-09 18:40:51 +0000273
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000274 The line terminator is always ``b'\n'`` for binary files; for text files,
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000275 the *newlines* argument to :func:`open` can be used to select the line
Georg Brandl014197c2008-04-09 18:40:51 +0000276 terminator(s) recognized.
277
Georg Brandl3dd33882009-06-01 17:35:27 +0000278 .. method:: readlines(hint=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000279
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000280 Read and return a list of lines from the stream. *hint* can be specified
281 to control the number of lines read: no more lines will be read if the
282 total size (in bytes/characters) of all lines so far exceeds *hint*.
Georg Brandl014197c2008-04-09 18:40:51 +0000283
Georg Brandl3dd33882009-06-01 17:35:27 +0000284 .. method:: seek(offset, whence=SEEK_SET)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000285
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000286 Change the stream position to the given byte *offset*. *offset* is
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000287 interpreted relative to the position indicated by *whence*. Values for
288 *whence* are:
289
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000290 * :data:`SEEK_SET` or ``0`` -- start of the stream (the default);
291 *offset* should be zero or positive
292 * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may
293 be negative
294 * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually
295 negative
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000296
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000297 Return the new absolute position.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000298
Raymond Hettinger35a88362009-04-09 00:08:24 +0000299 .. versionadded:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000300 The ``SEEK_*`` constants.
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000301
Georg Brandl014197c2008-04-09 18:40:51 +0000302 .. method:: seekable()
303
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000304 Return ``True`` if the stream supports random access. If ``False``,
305 :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000306
307 .. method:: tell()
308
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000309 Return the current stream position.
Georg Brandl014197c2008-04-09 18:40:51 +0000310
Georg Brandl3dd33882009-06-01 17:35:27 +0000311 .. method:: truncate(size=None)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000312
Antoine Pitrou2016dc92010-05-29 12:08:25 +0000313 Resize the stream to the given *size* in bytes (or the current position
314 if *size* is not specified). The current stream position isn't changed.
315 This resizing can extend or reduce the current file size. In case of
316 extension, the contents of the new file area depend on the platform
317 (on most systems, additional bytes are zero-filled, on Windows they're
318 undetermined). The new file size is returned.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000319
Georg Brandl014197c2008-04-09 18:40:51 +0000320 .. method:: writable()
321
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000322 Return ``True`` if the stream supports writing. If ``False``,
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000323 :meth:`write` and :meth:`truncate` will raise :exc:`IOError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000324
325 .. method:: writelines(lines)
326
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000327 Write a list of lines to the stream. Line separators are not added, so it
328 is usual for each of the lines provided to have a line separator at the
329 end.
Georg Brandl014197c2008-04-09 18:40:51 +0000330
331
332.. class:: RawIOBase
333
334 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no
335 public constructor.
336
Antoine Pitrou497a7672009-09-17 17:18:01 +0000337 Raw binary I/O typically provides low-level access to an underlying OS
338 device or API, and does not try to encapsulate it in high-level primitives
339 (this is left to Buffered I/O and Text I/O, described later in this page).
340
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000341 In addition to the attributes and methods from :class:`IOBase`,
342 RawIOBase provides the following methods:
Georg Brandl014197c2008-04-09 18:40:51 +0000343
Georg Brandl3dd33882009-06-01 17:35:27 +0000344 .. method:: read(n=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000345
Antoine Pitrou78ddbe62009-10-01 16:24:45 +0000346 Read up to *n* bytes from the object and return them. As a convenience,
347 if *n* is unspecified or -1, :meth:`readall` is called. Otherwise,
348 only one system call is ever made. Fewer than *n* bytes may be
349 returned if the operating system call returns fewer than *n* bytes.
350
351 If 0 bytes are returned, and *n* was not 0, this indicates end of file.
352 If the object is in non-blocking mode and no bytes are available,
353 ``None`` is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000354
Benjamin Petersonb47aace2008-04-09 21:38:38 +0000355 .. method:: readall()
Georg Brandl014197c2008-04-09 18:40:51 +0000356
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000357 Read and return all the bytes from the stream until EOF, using multiple
358 calls to the stream if necessary.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000359
360 .. method:: readinto(b)
361
Daniel Stutzbachd01df462010-11-30 17:49:53 +0000362 Read up to len(b) bytes into bytearray *b* and return the number
363 of bytes read. If the object is in non-blocking mode and no
364 bytes are available, ``None`` is returned.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000365
366 .. method:: write(b)
367
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000368 Write the given bytes or bytearray object, *b*, to the underlying raw
Antoine Pitrou497a7672009-09-17 17:18:01 +0000369 stream and return the number of bytes written. This can be less than
370 ``len(b)``, depending on specifics of the underlying raw stream, and
371 especially if it is in non-blocking mode. ``None`` is returned if the
372 raw stream is set not to block and no single byte could be readily
373 written to it.
Georg Brandl014197c2008-04-09 18:40:51 +0000374
375
Georg Brandl014197c2008-04-09 18:40:51 +0000376.. class:: BufferedIOBase
377
Antoine Pitrou497a7672009-09-17 17:18:01 +0000378 Base class for binary streams that support some kind of buffering.
379 It inherits :class:`IOBase`. There is no public constructor.
Georg Brandl014197c2008-04-09 18:40:51 +0000380
Antoine Pitrou497a7672009-09-17 17:18:01 +0000381 The main difference with :class:`RawIOBase` is that methods :meth:`read`,
382 :meth:`readinto` and :meth:`write` will try (respectively) to read as much
383 input as requested or to consume all given output, at the expense of
384 making perhaps more than one system call.
385
386 In addition, those methods can raise :exc:`BlockingIOError` if the
387 underlying raw stream is in non-blocking mode and cannot take or give
388 enough data; unlike their :class:`RawIOBase` counterparts, they will
389 never return ``None``.
390
391 Besides, the :meth:`read` method does not have a default
Georg Brandl014197c2008-04-09 18:40:51 +0000392 implementation that defers to :meth:`readinto`.
393
Antoine Pitrou497a7672009-09-17 17:18:01 +0000394 A typical :class:`BufferedIOBase` implementation should not inherit from a
395 :class:`RawIOBase` implementation, but wrap one, like
396 :class:`BufferedWriter` and :class:`BufferedReader` do.
Georg Brandl014197c2008-04-09 18:40:51 +0000397
Senthil Kumarana6bac952011-07-04 11:28:30 -0700398 :class:`BufferedIOBase` provides or overrides these methods and attribute in
399 addition to those from :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000400
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000401 .. attribute:: raw
402
403 The underlying raw stream (a :class:`RawIOBase` instance) that
404 :class:`BufferedIOBase` deals with. This is not part of the
405 :class:`BufferedIOBase` API and may not exist on some implementations.
406
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000407 .. method:: detach()
408
409 Separate the underlying raw stream from the buffer and return it.
410
411 After the raw stream has been detached, the buffer is in an unusable
412 state.
413
414 Some buffers, like :class:`BytesIO`, do not have the concept of a single
415 raw stream to return from this method. They raise
416 :exc:`UnsupportedOperation`.
417
Benjamin Petersonedc36472009-05-01 20:48:14 +0000418 .. versionadded:: 3.1
419
Georg Brandl3dd33882009-06-01 17:35:27 +0000420 .. method:: read(n=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000421
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000422 Read and return up to *n* bytes. If the argument is omitted, ``None``, or
Georg Brandl014197c2008-04-09 18:40:51 +0000423 negative, data is read and returned until EOF is reached. An empty bytes
424 object is returned if the stream is already at EOF.
425
426 If the argument is positive, and the underlying raw stream is not
427 interactive, multiple raw reads may be issued to satisfy the byte count
428 (unless EOF is reached first). But for interactive raw streams, at most
429 one raw read will be issued, and a short result does not imply that EOF is
430 imminent.
431
Antoine Pitrou497a7672009-09-17 17:18:01 +0000432 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
433 non blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000434
Georg Brandl3dd33882009-06-01 17:35:27 +0000435 .. method:: read1(n=-1)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000436
437 Read and return up to *n* bytes, with at most one call to the underlying
Antoine Pitrou497a7672009-09-17 17:18:01 +0000438 raw stream's :meth:`~RawIOBase.read` method. This can be useful if you
439 are implementing your own buffering on top of a :class:`BufferedIOBase`
440 object.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000441
Georg Brandl014197c2008-04-09 18:40:51 +0000442 .. method:: readinto(b)
443
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000444 Read up to len(b) bytes into bytearray *b* and return the number of bytes
Georg Brandl014197c2008-04-09 18:40:51 +0000445 read.
446
447 Like :meth:`read`, multiple reads may be issued to the underlying raw
Antoine Pitrou497a7672009-09-17 17:18:01 +0000448 stream, unless the latter is 'interactive'.
Georg Brandl014197c2008-04-09 18:40:51 +0000449
Antoine Pitrou497a7672009-09-17 17:18:01 +0000450 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
451 non blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000452
Georg Brandl014197c2008-04-09 18:40:51 +0000453 .. method:: write(b)
454
Antoine Pitrou497a7672009-09-17 17:18:01 +0000455 Write the given bytes or bytearray object, *b* and return the number
456 of bytes written (never less than ``len(b)``, since if the write fails
457 an :exc:`IOError` will be raised). Depending on the actual
458 implementation, these bytes may be readily written to the underlying
459 stream, or held in a buffer for performance and latency reasons.
Georg Brandl014197c2008-04-09 18:40:51 +0000460
Antoine Pitrou497a7672009-09-17 17:18:01 +0000461 When in non-blocking mode, a :exc:`BlockingIOError` is raised if the
462 data needed to be written to the raw stream but it couldn't accept
463 all the data without blocking.
Georg Brandl014197c2008-04-09 18:40:51 +0000464
465
Benjamin Petersonaa069002009-01-23 03:26:36 +0000466Raw File I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000467^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000468
Georg Brandl3dd33882009-06-01 17:35:27 +0000469.. class:: FileIO(name, mode='r', closefd=True)
Benjamin Petersonaa069002009-01-23 03:26:36 +0000470
Antoine Pitrou497a7672009-09-17 17:18:01 +0000471 :class:`FileIO` represents an OS-level file containing bytes data.
472 It implements the :class:`RawIOBase` interface (and therefore the
473 :class:`IOBase` interface, too).
474
475 The *name* can be one of two things:
476
477 * a character string or bytes object representing the path to the file
478 which will be opened;
479 * an integer representing the number of an existing OS-level file descriptor
480 to which the resulting :class:`FileIO` object will give access.
Benjamin Petersonaa069002009-01-23 03:26:36 +0000481
482 The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing,
483 or appending. The file will be created if it doesn't exist when opened for
484 writing or appending; it will be truncated when opened for writing. Add a
485 ``'+'`` to the mode to allow simultaneous reading and writing.
486
Antoine Pitrou497a7672009-09-17 17:18:01 +0000487 The :meth:`read` (when called with a positive argument), :meth:`readinto`
488 and :meth:`write` methods on this class will only make one system call.
489
Benjamin Petersonaa069002009-01-23 03:26:36 +0000490 In addition to the attributes and methods from :class:`IOBase` and
491 :class:`RawIOBase`, :class:`FileIO` provides the following data
492 attributes and methods:
493
494 .. attribute:: mode
495
496 The mode as given in the constructor.
497
498 .. attribute:: name
499
500 The file name. This is the file descriptor of the file when no name is
501 given in the constructor.
502
Benjamin Petersonaa069002009-01-23 03:26:36 +0000503
504Buffered Streams
Antoine Pitroub530e142010-08-30 12:41:00 +0000505^^^^^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000506
Antoine Pitroubed81c82010-12-03 19:14:17 +0000507Buffered I/O streams provide a higher-level interface to an I/O device
508than raw I/O does.
Antoine Pitrou497a7672009-09-17 17:18:01 +0000509
Georg Brandl014197c2008-04-09 18:40:51 +0000510.. class:: BytesIO([initial_bytes])
511
512 A stream implementation using an in-memory bytes buffer. It inherits
513 :class:`BufferedIOBase`.
514
Antoine Pitroub530e142010-08-30 12:41:00 +0000515 The argument *initial_bytes* contains optional initial :class:`bytes` data.
Georg Brandl014197c2008-04-09 18:40:51 +0000516
517 :class:`BytesIO` provides or overrides these methods in addition to those
518 from :class:`BufferedIOBase` and :class:`IOBase`:
519
Antoine Pitrou972ee132010-09-06 18:48:21 +0000520 .. method:: getbuffer()
521
522 Return a readable and writable view over the contents of the buffer
523 without copying them. Also, mutating the view will transparently
524 update the contents of the buffer::
525
526 >>> b = io.BytesIO(b"abcdef")
527 >>> view = b.getbuffer()
528 >>> view[2:4] = b"56"
529 >>> b.getvalue()
530 b'ab56ef'
531
532 .. note::
533 As long as the view exists, the :class:`BytesIO` object cannot be
534 resized.
535
536 .. versionadded:: 3.2
537
Georg Brandl014197c2008-04-09 18:40:51 +0000538 .. method:: getvalue()
539
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000540 Return ``bytes`` containing the entire contents of the buffer.
Georg Brandl014197c2008-04-09 18:40:51 +0000541
542 .. method:: read1()
543
Benjamin Peterson9efcc4b2008-04-14 21:30:21 +0000544 In :class:`BytesIO`, this is the same as :meth:`read`.
Georg Brandl014197c2008-04-09 18:40:51 +0000545
Georg Brandl014197c2008-04-09 18:40:51 +0000546
Georg Brandl3dd33882009-06-01 17:35:27 +0000547.. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000548
Antoine Pitrou497a7672009-09-17 17:18:01 +0000549 A buffer providing higher-level access to a readable, sequential
550 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
551 When reading data from this object, a larger amount of data may be
552 requested from the underlying raw stream, and kept in an internal buffer.
553 The buffered data can then be returned directly on subsequent reads.
Georg Brandl014197c2008-04-09 18:40:51 +0000554
555 The constructor creates a :class:`BufferedReader` for the given readable
556 *raw* stream and *buffer_size*. If *buffer_size* is omitted,
557 :data:`DEFAULT_BUFFER_SIZE` is used.
558
559 :class:`BufferedReader` provides or overrides these methods in addition to
560 those from :class:`BufferedIOBase` and :class:`IOBase`:
561
562 .. method:: peek([n])
563
Benjamin Petersonc43a26d2009-06-16 23:09:24 +0000564 Return bytes from the stream without advancing the position. At most one
Benjamin Peterson2a8b54d2009-06-14 14:37:23 +0000565 single read on the raw stream is done to satisfy the call. The number of
566 bytes returned may be less or more than requested.
Georg Brandl014197c2008-04-09 18:40:51 +0000567
568 .. method:: read([n])
569
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000570 Read and return *n* bytes, or if *n* is not given or negative, until EOF
Georg Brandl014197c2008-04-09 18:40:51 +0000571 or if the read call would block in non-blocking mode.
572
573 .. method:: read1(n)
574
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000575 Read and return up to *n* bytes with only one call on the raw stream. If
Georg Brandl014197c2008-04-09 18:40:51 +0000576 at least one byte is buffered, only buffered bytes are returned.
577 Otherwise, one raw stream read call is made.
578
579
Georg Brandl3dd33882009-06-01 17:35:27 +0000580.. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000581
Antoine Pitrou497a7672009-09-17 17:18:01 +0000582 A buffer providing higher-level access to a writeable, sequential
583 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
584 When writing to this object, data is normally held into an internal
585 buffer. The buffer will be written out to the underlying :class:`RawIOBase`
586 object under various conditions, including:
587
588 * when the buffer gets too small for all pending data;
589 * when :meth:`flush()` is called;
590 * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects);
591 * when the :class:`BufferedWriter` object is closed or destroyed.
Georg Brandl014197c2008-04-09 18:40:51 +0000592
593 The constructor creates a :class:`BufferedWriter` for the given writeable
594 *raw* stream. If the *buffer_size* is not given, it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000595 :data:`DEFAULT_BUFFER_SIZE`.
596
Georg Brandl3dd33882009-06-01 17:35:27 +0000597 A third argument, *max_buffer_size*, is supported, but unused and deprecated.
Georg Brandl014197c2008-04-09 18:40:51 +0000598
599 :class:`BufferedWriter` provides or overrides these methods in addition to
600 those from :class:`BufferedIOBase` and :class:`IOBase`:
601
602 .. method:: flush()
603
604 Force bytes held in the buffer into the raw stream. A
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000605 :exc:`BlockingIOError` should be raised if the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000606
607 .. method:: write(b)
608
Antoine Pitrou497a7672009-09-17 17:18:01 +0000609 Write the bytes or bytearray object, *b* and return the number of bytes
610 written. When in non-blocking mode, a :exc:`BlockingIOError` is raised
611 if the buffer needs to be written out but the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000612
613
Georg Brandl3dd33882009-06-01 17:35:27 +0000614.. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000615
616 A buffered interface to random access streams. It inherits
Antoine Pitrou497a7672009-09-17 17:18:01 +0000617 :class:`BufferedReader` and :class:`BufferedWriter`, and further supports
618 :meth:`seek` and :meth:`tell` functionality.
Georg Brandl014197c2008-04-09 18:40:51 +0000619
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000620 The constructor creates a reader and writer for a seekable raw stream, given
Georg Brandl014197c2008-04-09 18:40:51 +0000621 in the first argument. If the *buffer_size* is omitted it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000622 :data:`DEFAULT_BUFFER_SIZE`.
623
Georg Brandl3dd33882009-06-01 17:35:27 +0000624 A third argument, *max_buffer_size*, is supported, but unused and deprecated.
Georg Brandl014197c2008-04-09 18:40:51 +0000625
626 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
627 :class:`BufferedWriter` can do.
628
629
Antoine Pitrou13d28952011-08-20 19:48:43 +0200630.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE)
631
632 A buffered I/O object combining two unidirectional :class:`RawIOBase`
633 objects -- one readable, the other writeable -- into a single bidirectional
634 endpoint. It inherits :class:`BufferedIOBase`.
635
636 *reader* and *writer* are :class:`RawIOBase` objects that are readable and
637 writeable respectively. If the *buffer_size* is omitted it defaults to
638 :data:`DEFAULT_BUFFER_SIZE`.
639
640 A fourth argument, *max_buffer_size*, is supported, but unused and
641 deprecated.
642
643 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods
644 except for :meth:`~BufferedIOBase.detach`, which raises
645 :exc:`UnsupportedOperation`.
646
647 .. warning::
648 :class:`BufferedRWPair` does not attempt to synchronize accesses to
649 its underlying raw streams. You should not pass it the same object
650 as reader and writer; use :class:`BufferedRandom` instead.
651
652
Georg Brandl014197c2008-04-09 18:40:51 +0000653Text I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000654^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000655
656.. class:: TextIOBase
657
658 Base class for text streams. This class provides a character and line based
659 interface to stream I/O. There is no :meth:`readinto` method because
660 Python's character strings are immutable. It inherits :class:`IOBase`.
661 There is no public constructor.
662
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000663 :class:`TextIOBase` provides or overrides these data attributes and
664 methods in addition to those from :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000665
666 .. attribute:: encoding
667
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000668 The name of the encoding used to decode the stream's bytes into
Georg Brandl014197c2008-04-09 18:40:51 +0000669 strings, and to encode strings into bytes.
670
Benjamin Peterson0926ad12009-06-06 18:02:12 +0000671 .. attribute:: errors
672
673 The error setting of the decoder or encoder.
674
Georg Brandl014197c2008-04-09 18:40:51 +0000675 .. attribute:: newlines
676
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000677 A string, a tuple of strings, or ``None``, indicating the newlines
Antoine Pitrou497a7672009-09-17 17:18:01 +0000678 translated so far. Depending on the implementation and the initial
679 constructor flags, this may not be available.
Georg Brandl014197c2008-04-09 18:40:51 +0000680
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000681 .. attribute:: buffer
682
683 The underlying binary buffer (a :class:`BufferedIOBase` instance) that
684 :class:`TextIOBase` deals with. This is not part of the
685 :class:`TextIOBase` API and may not exist on some implementations.
686
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000687 .. method:: detach()
688
Antoine Pitrou497a7672009-09-17 17:18:01 +0000689 Separate the underlying binary buffer from the :class:`TextIOBase` and
690 return it.
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000691
692 After the underlying buffer has been detached, the :class:`TextIOBase` is
693 in an unusable state.
694
695 Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not
696 have the concept of an underlying buffer and calling this method will
697 raise :exc:`UnsupportedOperation`.
698
Benjamin Petersonedc36472009-05-01 20:48:14 +0000699 .. versionadded:: 3.1
700
Georg Brandl014197c2008-04-09 18:40:51 +0000701 .. method:: read(n)
702
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000703 Read and return at most *n* characters from the stream as a single
Antoine Pitrou497a7672009-09-17 17:18:01 +0000704 :class:`str`. If *n* is negative or ``None``, reads until EOF.
Georg Brandl014197c2008-04-09 18:40:51 +0000705
Antoine Pitrou707bd4e2012-07-25 22:38:33 +0200706 .. method:: readline(limit=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000707
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000708 Read until newline or EOF and return a single ``str``. If the stream is
709 already at EOF, an empty string is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000710
Antoine Pitrou707bd4e2012-07-25 22:38:33 +0200711 If *limit* is specified, at most *limit* characters will be read.
712
Antoine Pitrouf49d1522012-01-21 20:20:49 +0100713 .. method:: seek(offset, whence=SEEK_SET)
714
715 Change the stream position to the given *offset*. Behaviour depends
716 on the *whence* parameter:
717
718 * :data:`SEEK_SET` or ``0``: seek from the start of the stream
719 (the default); *offset* must either be a number returned by
720 :meth:`TextIOBase.tell`, or zero. Any other *offset* value
721 produces undefined behaviour.
722 * :data:`SEEK_CUR` or ``1``: "seek" to the current position;
723 *offset* must be zero, which is a no-operation (all other values
724 are unsupported).
725 * :data:`SEEK_END` or ``2``: seek to the end of the stream;
726 *offset* must be zero (all other values are unsupported).
727
728 Return the new absolute position as an opaque number.
729
730 .. versionadded:: 3.1
731 The ``SEEK_*`` constants.
732
733 .. method:: tell()
734
735 Return the current stream position as an opaque number. The number
736 does not usually represent a number of bytes in the underlying
737 binary storage.
738
Georg Brandl014197c2008-04-09 18:40:51 +0000739 .. method:: write(s)
740
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000741 Write the string *s* to the stream and return the number of characters
742 written.
Georg Brandl014197c2008-04-09 18:40:51 +0000743
744
Georg Brandl3dd33882009-06-01 17:35:27 +0000745.. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False)
Georg Brandl014197c2008-04-09 18:40:51 +0000746
Antoine Pitrou497a7672009-09-17 17:18:01 +0000747 A buffered text stream over a :class:`BufferedIOBase` binary stream.
Georg Brandl014197c2008-04-09 18:40:51 +0000748 It inherits :class:`TextIOBase`.
749
750 *encoding* gives the name of the encoding that the stream will be decoded or
751 encoded with. It defaults to :func:`locale.getpreferredencoding`.
752
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000753 *errors* is an optional string that specifies how encoding and decoding
754 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError`
755 exception if there is an encoding error (the default of ``None`` has the same
756 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding
757 errors can lead to data loss.) ``'replace'`` causes a replacement marker
Christian Heimesa342c012008-04-20 21:01:16 +0000758 (such as ``'?'``) to be inserted where there is malformed data. When
759 writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character
760 reference) or ``'backslashreplace'`` (replace with backslashed escape
761 sequences) can be used. Any other error handling name that has been
762 registered with :func:`codecs.register_error` is also valid.
Georg Brandl014197c2008-04-09 18:40:51 +0000763
R David Murray1b00f252012-08-15 10:43:58 -0400764 .. index::
765 single: universal newlines; io.TextIOWrapper class
766
Antoine Pitrou0c1c0d42012-08-04 00:55:38 +0200767 *newline* controls how line endings are handled. It can be ``None``,
768 ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows:
769
R David Murray1b00f252012-08-15 10:43:58 -0400770 * When reading input from the stream, if *newline* is ``None``,
R David Murrayee0a9452012-08-15 11:05:36 -0400771 :term:`universal newlines` mode is enabled. Lines in the input can end in
772 ``'\n'``, ``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'``
773 before being returned to the caller. If it is ``''``, universal newlines
774 mode is enabled, but line endings are returned to the caller untranslated.
775 If it has any of the other legal values, input lines are only terminated
776 by the given string, and the line ending is returned to the caller
777 untranslated.
Antoine Pitrou0c1c0d42012-08-04 00:55:38 +0200778
Georg Brandl296d1be2012-08-14 09:39:07 +0200779 * When writing output to the stream, if *newline* is ``None``, any ``'\n'``
780 characters written are translated to the system default line separator,
781 :data:`os.linesep`. If *newline* is ``''`` or ``'\n'``, no translation
782 takes place. If *newline* is any of the other legal values, any ``'\n'``
783 characters written are translated to the given string.
Georg Brandl014197c2008-04-09 18:40:51 +0000784
785 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
786 write contains a newline character.
787
Benjamin Peterson0926ad12009-06-06 18:02:12 +0000788 :class:`TextIOWrapper` provides one attribute in addition to those of
Georg Brandl014197c2008-04-09 18:40:51 +0000789 :class:`TextIOBase` and its parents:
790
Georg Brandl014197c2008-04-09 18:40:51 +0000791 .. attribute:: line_buffering
792
793 Whether line buffering is enabled.
Georg Brandl48310cd2009-01-03 21:18:54 +0000794
Georg Brandl014197c2008-04-09 18:40:51 +0000795
Georg Brandl3dd33882009-06-01 17:35:27 +0000796.. class:: StringIO(initial_value='', newline=None)
Georg Brandl014197c2008-04-09 18:40:51 +0000797
Antoine Pitroub530e142010-08-30 12:41:00 +0000798 An in-memory stream for text I/O.
Georg Brandl014197c2008-04-09 18:40:51 +0000799
Benjamin Petersonaa1c8d82009-03-09 02:02:23 +0000800 The initial value of the buffer (an empty string by default) can be set by
801 providing *initial_value*. The *newline* argument works like that of
802 :class:`TextIOWrapper`. The default is to do no newline translation.
Georg Brandl014197c2008-04-09 18:40:51 +0000803
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000804 :class:`StringIO` provides this method in addition to those from
Antoine Pitroub530e142010-08-30 12:41:00 +0000805 :class:`TextIOBase` and its parents:
Georg Brandl014197c2008-04-09 18:40:51 +0000806
807 .. method:: getvalue()
808
Georg Brandl2932d932008-05-30 06:27:09 +0000809 Return a ``str`` containing the entire contents of the buffer at any
810 time before the :class:`StringIO` object's :meth:`close` method is
811 called.
Georg Brandl014197c2008-04-09 18:40:51 +0000812
Georg Brandl2932d932008-05-30 06:27:09 +0000813 Example usage::
814
815 import io
816
817 output = io.StringIO()
818 output.write('First line.\n')
819 print('Second line.', file=output)
820
821 # Retrieve file contents -- this will be
822 # 'First line.\nSecond line.\n'
823 contents = output.getvalue()
824
Georg Brandl48310cd2009-01-03 21:18:54 +0000825 # Close object and discard memory buffer --
Georg Brandl2932d932008-05-30 06:27:09 +0000826 # .getvalue() will now raise an exception.
827 output.close()
Georg Brandl014197c2008-04-09 18:40:51 +0000828
Antoine Pitroub530e142010-08-30 12:41:00 +0000829
R David Murray1b00f252012-08-15 10:43:58 -0400830.. index::
831 single: universal newlines; io.IncrementalNewlineDecoder class
832
Georg Brandl014197c2008-04-09 18:40:51 +0000833.. class:: IncrementalNewlineDecoder
834
R David Murray1b00f252012-08-15 10:43:58 -0400835 A helper codec that decodes newlines for :term:`universal newlines` mode.
836 It inherits :class:`codecs.IncrementalDecoder`.
Georg Brandl014197c2008-04-09 18:40:51 +0000837
Antoine Pitroubed81c82010-12-03 19:14:17 +0000838
Antoine Pitroubed81c82010-12-03 19:14:17 +0000839Performance
Benjamin Petersonedf51322011-02-24 03:03:46 +0000840-----------
841
842This section discusses the performance of the provided concrete I/O
843implementations.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000844
845Binary I/O
Benjamin Petersonedf51322011-02-24 03:03:46 +0000846^^^^^^^^^^
Antoine Pitroubed81c82010-12-03 19:14:17 +0000847
Benjamin Petersonedf51322011-02-24 03:03:46 +0000848By reading and writing only large chunks of data even when the user asks for a
849single byte, buffered I/O hides any inefficiency in calling and executing the
850operating system's unbuffered I/O routines. The gain depends on the OS and the
851kind of I/O which is performed. For example, on some modern OSes such as Linux,
852unbuffered disk I/O can be as fast as buffered I/O. The bottom line, however,
853is that buffered I/O offers predictable performance regardless of the platform
854and the backing device. Therefore, it is most always preferable to use buffered
855I/O rather than unbuffered I/O for binary datal
Antoine Pitroubed81c82010-12-03 19:14:17 +0000856
857Text I/O
Benjamin Petersonedf51322011-02-24 03:03:46 +0000858^^^^^^^^
Antoine Pitroubed81c82010-12-03 19:14:17 +0000859
860Text I/O over a binary storage (such as a file) is significantly slower than
Benjamin Petersonedf51322011-02-24 03:03:46 +0000861binary I/O over the same storage, because it requires conversions between
862unicode and binary data using a character codec. This can become noticeable
863handling huge amounts of text data like large log files. Also,
864:meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both quite slow
865due to the reconstruction algorithm used.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000866
867:class:`StringIO`, however, is a native in-memory unicode container and will
868exhibit similar speed to :class:`BytesIO`.
869
870Multi-threading
871^^^^^^^^^^^^^^^
872
Benjamin Petersonedf51322011-02-24 03:03:46 +0000873:class:`FileIO` objects are thread-safe to the extent that the operating system
874calls (such as ``read(2)`` under Unix) they wrap are thread-safe too.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000875
876Binary buffered objects (instances of :class:`BufferedReader`,
877:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
878protect their internal structures using a lock; it is therefore safe to call
879them from multiple threads at once.
880
881:class:`TextIOWrapper` objects are not thread-safe.
882
883Reentrancy
884^^^^^^^^^^
885
886Binary buffered objects (instances of :class:`BufferedReader`,
887:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
888are not reentrant. While reentrant calls will not happen in normal situations,
Benjamin Petersonedf51322011-02-24 03:03:46 +0000889they can arise from doing I/O in a :mod:`signal` handler. If a thread tries to
890renter a buffered object which it is already accessing, a :exc:`RuntimeError` is
891raised. Note this doesn't prohibit a different thread from entering the
892buffered object.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000893
Benjamin Petersonedf51322011-02-24 03:03:46 +0000894The above implicitly extends to text files, since the :func:`open()` function
895will wrap a buffered object inside a :class:`TextIOWrapper`. This includes
896standard streams and therefore affects the built-in function :func:`print()` as
897well.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000898