1 :mod:`io` --- Core tools for working with streams 2 ================================================= 3 4 .. module:: io 5 :synopsis: Core tools for working with streams. 6 7 .. moduleauthor:: Guido van Rossum <guido (a] python.org> 8 .. moduleauthor:: Mike Verdone <mike.verdone (a] gmail.com> 9 .. moduleauthor:: Mark Russell <mark.russell (a] zen.co.uk> 10 .. moduleauthor:: Antoine Pitrou <solipsis (a] pitrou.net> 11 .. moduleauthor:: Amaury Forgeot d'Arc <amauryfa (a] gmail.com> 12 .. moduleauthor:: Benjamin Peterson <benjamin (a] python.org> 13 .. sectionauthor:: Benjamin Peterson <benjamin (a] python.org> 14 15 **Source code:** :source:`Lib/io.py` 16 17 -------------- 18 19 .. _io-overview: 20 21 Overview 22 -------- 23 24 .. index:: 25 single: file object; io module 26 27 The :mod:`io` module provides Python's main facilities for dealing with various 28 types of I/O. There are three main types of I/O: *text I/O*, *binary I/O* 29 and *raw I/O*. These are generic categories, and various backing stores can 30 be used for each of them. A concrete object belonging to any of these 31 categories is called a :term:`file object`. Other common terms are *stream* 32 and *file-like object*. 33 34 Independently of its category, each concrete stream object will also have 35 various capabilities: it can be read-only, write-only, or read-write. It can 36 also allow arbitrary random access (seeking forwards or backwards to any 37 location), or only sequential access (for example in the case of a socket or 38 pipe). 39 40 All streams are careful about the type of data you give to them. For example 41 giving a :class:`str` object to the ``write()`` method of a binary stream 42 will raise a ``TypeError``. So will giving a :class:`bytes` object to the 43 ``write()`` method of a text stream. 44 45 .. versionchanged:: 3.3 46 Operations that used to raise :exc:`IOError` now raise :exc:`OSError`, since 47 :exc:`IOError` is now an alias of :exc:`OSError`. 48 49 50 Text I/O 51 ^^^^^^^^ 52 53 Text I/O expects and produces :class:`str` objects. This means that whenever 54 the backing store is natively made of bytes (such as in the case of a file), 55 encoding and decoding of data is made transparently as well as optional 56 translation of platform-specific newline characters. 57 58 The easiest way to create a text stream is with :meth:`open()`, optionally 59 specifying an encoding:: 60 61 f = open("myfile.txt", "r", encoding="utf-8") 62 63 In-memory text streams are also available as :class:`StringIO` objects:: 64 65 f = io.StringIO("some initial text data") 66 67 The text stream API is described in detail in the documentation of 68 :class:`TextIOBase`. 69 70 71 Binary I/O 72 ^^^^^^^^^^ 73 74 Binary I/O (also called *buffered I/O*) expects 75 :term:`bytes-like objects <bytes-like object>` and produces :class:`bytes` 76 objects. No encoding, decoding, or newline translation is performed. This 77 category of streams can be used for all kinds of non-text data, and also when 78 manual control over the handling of text data is desired. 79 80 The easiest way to create a binary stream is with :meth:`open()` with ``'b'`` in 81 the mode string:: 82 83 f = open("myfile.jpg", "rb") 84 85 In-memory binary streams are also available as :class:`BytesIO` objects:: 86 87 f = io.BytesIO(b"some initial binary data: \x00\x01") 88 89 The binary stream API is described in detail in the docs of 90 :class:`BufferedIOBase`. 91 92 Other library modules may provide additional ways to create text or binary 93 streams. See :meth:`socket.socket.makefile` for example. 94 95 96 Raw I/O 97 ^^^^^^^ 98 99 Raw I/O (also called *unbuffered I/O*) is generally used as a low-level 100 building-block for binary and text streams; it is rarely useful to directly 101 manipulate a raw stream from user code. Nevertheless, you can create a raw 102 stream by opening a file in binary mode with buffering disabled:: 103 104 f = open("myfile.jpg", "rb", buffering=0) 105 106 The raw stream API is described in detail in the docs of :class:`RawIOBase`. 107 108 109 High-level Module Interface 110 --------------------------- 111 112 .. data:: DEFAULT_BUFFER_SIZE 113 114 An int containing the default buffer size used by the module's buffered I/O 115 classes. :func:`open` uses the file's blksize (as obtained by 116 :func:`os.stat`) if possible. 117 118 119 .. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) 120 121 This is an alias for the builtin :func:`open` function. 122 123 124 .. exception:: BlockingIOError 125 126 This is a compatibility alias for the builtin :exc:`BlockingIOError` 127 exception. 128 129 130 .. exception:: UnsupportedOperation 131 132 An exception inheriting :exc:`OSError` and :exc:`ValueError` that is raised 133 when an unsupported operation is called on a stream. 134 135 136 In-memory streams 137 ^^^^^^^^^^^^^^^^^ 138 139 It is also possible to use a :class:`str` or :term:`bytes-like object` as a 140 file for both reading and writing. For strings :class:`StringIO` can be used 141 like a file opened in text mode. :class:`BytesIO` can be used like a file 142 opened in binary mode. Both provide full read-write capabilities with random 143 access. 144 145 146 .. seealso:: 147 148 :mod:`sys` 149 contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`, 150 and :data:`sys.stderr`. 151 152 153 Class hierarchy 154 --------------- 155 156 The implementation of I/O streams is organized as a hierarchy of classes. First 157 :term:`abstract base classes <abstract base class>` (ABCs), which are used to 158 specify the various categories of streams, then concrete classes providing the 159 standard stream implementations. 160 161 .. note:: 162 163 The abstract base classes also provide default implementations of some 164 methods in order to help implementation of concrete stream classes. For 165 example, :class:`BufferedIOBase` provides unoptimized implementations of 166 :meth:`~IOBase.readinto` and :meth:`~IOBase.readline`. 167 168 At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It 169 defines the basic interface to a stream. Note, however, that there is no 170 separation between reading and writing to streams; implementations are allowed 171 to raise :exc:`UnsupportedOperation` if they do not support a given operation. 172 173 The :class:`RawIOBase` ABC extends :class:`IOBase`. It deals with the reading 174 and writing of bytes to a stream. :class:`FileIO` subclasses :class:`RawIOBase` 175 to provide an interface to files in the machine's file system. 176 177 The :class:`BufferedIOBase` ABC deals with buffering on a raw byte stream 178 (:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`, 179 :class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are 180 readable, writable, and both readable and writable. :class:`BufferedRandom` 181 provides a buffered interface to random access streams. Another 182 :class:`BufferedIOBase` subclass, :class:`BytesIO`, is a stream of in-memory 183 bytes. 184 185 The :class:`TextIOBase` ABC, another subclass of :class:`IOBase`, deals with 186 streams whose bytes represent text, and handles encoding and decoding to and 187 from strings. :class:`TextIOWrapper`, which extends it, is a buffered text 188 interface to a buffered raw stream (:class:`BufferedIOBase`). Finally, 189 :class:`StringIO` is an in-memory stream for text. 190 191 Argument names are not part of the specification, and only the arguments of 192 :func:`open` are intended to be used as keyword arguments. 193 194 The following table summarizes the ABCs provided by the :mod:`io` module: 195 196 .. tabularcolumns:: |l|l|L|L| 197 198 ========================= ================== ======================== ================================================== 199 ABC Inherits Stub Methods Mixin Methods and Properties 200 ========================= ================== ======================== ================================================== 201 :class:`IOBase` ``fileno``, ``seek``, ``close``, ``closed``, ``__enter__``, 202 and ``truncate`` ``__exit__``, ``flush``, ``isatty``, ``__iter__``, 203 ``__next__``, ``readable``, ``readline``, 204 ``readlines``, ``seekable``, ``tell``, 205 ``writable``, and ``writelines`` 206 :class:`RawIOBase` :class:`IOBase` ``readinto`` and Inherited :class:`IOBase` methods, ``read``, 207 ``write`` and ``readall`` 208 :class:`BufferedIOBase` :class:`IOBase` ``detach``, ``read``, Inherited :class:`IOBase` methods, ``readinto`` 209 ``read1``, and ``write`` 210 :class:`TextIOBase` :class:`IOBase` ``detach``, ``read``, Inherited :class:`IOBase` methods, ``encoding``, 211 ``readline``, and ``errors``, and ``newlines`` 212 ``write`` 213 ========================= ================== ======================== ================================================== 214 215 216 I/O Base Classes 217 ^^^^^^^^^^^^^^^^ 218 219 .. class:: IOBase 220 221 The abstract base class for all I/O classes, acting on streams of bytes. 222 There is no public constructor. 223 224 This class provides empty abstract implementations for many methods 225 that derived classes can override selectively; the default 226 implementations represent a file that cannot be read, written or 227 seeked. 228 229 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`, 230 or :meth:`write` because their signatures will vary, implementations and 231 clients should consider those methods part of the interface. Also, 232 implementations may raise a :exc:`ValueError` (or :exc:`UnsupportedOperation`) 233 when operations they do not support are called. 234 235 The basic type used for binary data read from or written to a file is 236 :class:`bytes`. Other :term:`bytes-like objects <bytes-like object>` are 237 accepted as method arguments too. In some cases, such as 238 :meth:`~RawIOBase.readinto`, a writable object such as :class:`bytearray` 239 is required. Text I/O classes work with :class:`str` data. 240 241 Note that calling any method (even inquiries) on a closed stream is 242 undefined. Implementations may raise :exc:`ValueError` in this case. 243 244 :class:`IOBase` (and its subclasses) supports the iterator protocol, meaning 245 that an :class:`IOBase` object can be iterated over yielding the lines in a 246 stream. Lines are defined slightly differently depending on whether the 247 stream is a binary stream (yielding bytes), or a text stream (yielding 248 character strings). See :meth:`~IOBase.readline` below. 249 250 :class:`IOBase` is also a context manager and therefore supports the 251 :keyword:`with` statement. In this example, *file* is closed after the 252 :keyword:`with` statement's suite is finished---even if an exception occurs:: 253 254 with open('spam.txt', 'w') as file: 255 file.write('Spam and eggs!') 256 257 :class:`IOBase` provides these data attributes and methods: 258 259 .. method:: close() 260 261 Flush and close this stream. This method has no effect if the file is 262 already closed. Once the file is closed, any operation on the file 263 (e.g. reading or writing) will raise a :exc:`ValueError`. 264 265 As a convenience, it is allowed to call this method more than once; 266 only the first call, however, will have an effect. 267 268 .. attribute:: closed 269 270 ``True`` if the stream is closed. 271 272 .. method:: fileno() 273 274 Return the underlying file descriptor (an integer) of the stream if it 275 exists. An :exc:`OSError` is raised if the IO object does not use a file 276 descriptor. 277 278 .. method:: flush() 279 280 Flush the write buffers of the stream if applicable. This does nothing 281 for read-only and non-blocking streams. 282 283 .. method:: isatty() 284 285 Return ``True`` if the stream is interactive (i.e., connected to 286 a terminal/tty device). 287 288 .. method:: readable() 289 290 Return ``True`` if the stream can be read from. If ``False``, :meth:`read` 291 will raise :exc:`OSError`. 292 293 .. method:: readline(size=-1) 294 295 Read and return one line from the stream. If *size* is specified, at 296 most *size* bytes will be read. 297 298 The line terminator is always ``b'\n'`` for binary files; for text files, 299 the *newline* argument to :func:`open` can be used to select the line 300 terminator(s) recognized. 301 302 .. method:: readlines(hint=-1) 303 304 Read and return a list of lines from the stream. *hint* can be specified 305 to control the number of lines read: no more lines will be read if the 306 total size (in bytes/characters) of all lines so far exceeds *hint*. 307 308 Note that it's already possible to iterate on file objects using ``for 309 line in file: ...`` without calling ``file.readlines()``. 310 311 .. method:: seek(offset[, whence]) 312 313 Change the stream position to the given byte *offset*. *offset* is 314 interpreted relative to the position indicated by *whence*. The default 315 value for *whence* is :data:`SEEK_SET`. Values for *whence* are: 316 317 * :data:`SEEK_SET` or ``0`` -- start of the stream (the default); 318 *offset* should be zero or positive 319 * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may 320 be negative 321 * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually 322 negative 323 324 Return the new absolute position. 325 326 .. versionadded:: 3.1 327 The ``SEEK_*`` constants. 328 329 .. versionadded:: 3.3 330 Some operating systems could support additional values, like 331 :data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`. The valid values 332 for a file could depend on it being open in text or binary mode. 333 334 .. method:: seekable() 335 336 Return ``True`` if the stream supports random access. If ``False``, 337 :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`OSError`. 338 339 .. method:: tell() 340 341 Return the current stream position. 342 343 .. method:: truncate(size=None) 344 345 Resize the stream to the given *size* in bytes (or the current position 346 if *size* is not specified). The current stream position isn't changed. 347 This resizing can extend or reduce the current file size. In case of 348 extension, the contents of the new file area depend on the platform 349 (on most systems, additional bytes are zero-filled). The new file size 350 is returned. 351 352 .. versionchanged:: 3.5 353 Windows will now zero-fill files when extending. 354 355 .. method:: writable() 356 357 Return ``True`` if the stream supports writing. If ``False``, 358 :meth:`write` and :meth:`truncate` will raise :exc:`OSError`. 359 360 .. method:: writelines(lines) 361 362 Write a list of lines to the stream. Line separators are not added, so it 363 is usual for each of the lines provided to have a line separator at the 364 end. 365 366 .. method:: __del__() 367 368 Prepare for object destruction. :class:`IOBase` provides a default 369 implementation of this method that calls the instance's 370 :meth:`~IOBase.close` method. 371 372 373 .. class:: RawIOBase 374 375 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no 376 public constructor. 377 378 Raw binary I/O typically provides low-level access to an underlying OS 379 device or API, and does not try to encapsulate it in high-level primitives 380 (this is left to Buffered I/O and Text I/O, described later in this page). 381 382 In addition to the attributes and methods from :class:`IOBase`, 383 :class:`RawIOBase` provides the following methods: 384 385 .. method:: read(size=-1) 386 387 Read up to *size* bytes from the object and return them. As a convenience, 388 if *size* is unspecified or -1, :meth:`readall` is called. Otherwise, 389 only one system call is ever made. Fewer than *size* bytes may be 390 returned if the operating system call returns fewer than *size* bytes. 391 392 If 0 bytes are returned, and *size* was not 0, this indicates end of file. 393 If the object is in non-blocking mode and no bytes are available, 394 ``None`` is returned. 395 396 .. method:: readall() 397 398 Read and return all the bytes from the stream until EOF, using multiple 399 calls to the stream if necessary. 400 401 .. method:: readinto(b) 402 403 Read bytes into a pre-allocated, writable 404 :term:`bytes-like object` *b*, and return the 405 number of bytes read. If the object is in non-blocking mode and no bytes 406 are available, ``None`` is returned. 407 408 .. method:: write(b) 409 410 Write the given :term:`bytes-like object`, *b*, to the 411 underlying raw stream, and return the number of 412 bytes written. This can be less than the length of *b* in 413 bytes, depending on specifics of the underlying raw 414 stream, and especially if it is in non-blocking mode. ``None`` is 415 returned if the raw stream is set not to block and no single byte could 416 be readily written to it. The caller may release or mutate *b* after 417 this method returns, so the implementation should only access *b* 418 during the method call. 419 420 421 .. class:: BufferedIOBase 422 423 Base class for binary streams that support some kind of buffering. 424 It inherits :class:`IOBase`. There is no public constructor. 425 426 The main difference with :class:`RawIOBase` is that methods :meth:`read`, 427 :meth:`readinto` and :meth:`write` will try (respectively) to read as much 428 input as requested or to consume all given output, at the expense of 429 making perhaps more than one system call. 430 431 In addition, those methods can raise :exc:`BlockingIOError` if the 432 underlying raw stream is in non-blocking mode and cannot take or give 433 enough data; unlike their :class:`RawIOBase` counterparts, they will 434 never return ``None``. 435 436 Besides, the :meth:`read` method does not have a default 437 implementation that defers to :meth:`readinto`. 438 439 A typical :class:`BufferedIOBase` implementation should not inherit from a 440 :class:`RawIOBase` implementation, but wrap one, like 441 :class:`BufferedWriter` and :class:`BufferedReader` do. 442 443 :class:`BufferedIOBase` provides or overrides these methods and attribute in 444 addition to those from :class:`IOBase`: 445 446 .. attribute:: raw 447 448 The underlying raw stream (a :class:`RawIOBase` instance) that 449 :class:`BufferedIOBase` deals with. This is not part of the 450 :class:`BufferedIOBase` API and may not exist on some implementations. 451 452 .. method:: detach() 453 454 Separate the underlying raw stream from the buffer and return it. 455 456 After the raw stream has been detached, the buffer is in an unusable 457 state. 458 459 Some buffers, like :class:`BytesIO`, do not have the concept of a single 460 raw stream to return from this method. They raise 461 :exc:`UnsupportedOperation`. 462 463 .. versionadded:: 3.1 464 465 .. method:: read(size=-1) 466 467 Read and return up to *size* bytes. If the argument is omitted, ``None``, 468 or negative, data is read and returned until EOF is reached. An empty 469 :class:`bytes` object is returned if the stream is already at EOF. 470 471 If the argument is positive, and the underlying raw stream is not 472 interactive, multiple raw reads may be issued to satisfy the byte count 473 (unless EOF is reached first). But for interactive raw streams, at most 474 one raw read will be issued, and a short result does not imply that EOF is 475 imminent. 476 477 A :exc:`BlockingIOError` is raised if the underlying raw stream is in 478 non blocking-mode, and has no data available at the moment. 479 480 .. method:: read1(size=-1) 481 482 Read and return up to *size* bytes, with at most one call to the 483 underlying raw stream's :meth:`~RawIOBase.read` (or 484 :meth:`~RawIOBase.readinto`) method. This can be useful if you are 485 implementing your own buffering on top of a :class:`BufferedIOBase` 486 object. 487 488 .. method:: readinto(b) 489 490 Read bytes into a pre-allocated, writable 491 :term:`bytes-like object` *b* and return the number of bytes read. 492 493 Like :meth:`read`, multiple reads may be issued to the underlying raw 494 stream, unless the latter is interactive. 495 496 A :exc:`BlockingIOError` is raised if the underlying raw stream is in non 497 blocking-mode, and has no data available at the moment. 498 499 .. method:: readinto1(b) 500 501 Read bytes into a pre-allocated, writable 502 :term:`bytes-like object` *b*, using at most one call to 503 the underlying raw stream's :meth:`~RawIOBase.read` (or 504 :meth:`~RawIOBase.readinto`) method. Return the number of bytes read. 505 506 A :exc:`BlockingIOError` is raised if the underlying raw stream is in non 507 blocking-mode, and has no data available at the moment. 508 509 .. versionadded:: 3.5 510 511 .. method:: write(b) 512 513 Write the given :term:`bytes-like object`, *b*, and return the number 514 of bytes written (always equal to the length of *b* in bytes, since if 515 the write fails an :exc:`OSError` will be raised). Depending on the 516 actual implementation, these bytes may be readily written to the 517 underlying stream, or held in a buffer for performance and latency 518 reasons. 519 520 When in non-blocking mode, a :exc:`BlockingIOError` is raised if the 521 data needed to be written to the raw stream but it couldn't accept 522 all the data without blocking. 523 524 The caller may release or mutate *b* after this method returns, 525 so the implementation should only access *b* during the method call. 526 527 528 Raw File I/O 529 ^^^^^^^^^^^^ 530 531 .. class:: FileIO(name, mode='r', closefd=True, opener=None) 532 533 :class:`FileIO` represents an OS-level file containing bytes data. 534 It implements the :class:`RawIOBase` interface (and therefore the 535 :class:`IOBase` interface, too). 536 537 The *name* can be one of two things: 538 539 * a character string or :class:`bytes` object representing the path to the 540 file which will be opened. In this case closefd must be ``True`` (the default) 541 otherwise an error will be raised. 542 * an integer representing the number of an existing OS-level file descriptor 543 to which the resulting :class:`FileIO` object will give access. When the 544 FileIO object is closed this fd will be closed as well, unless *closefd* 545 is set to ``False``. 546 547 The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading 548 (default), writing, exclusive creation or appending. The file will be 549 created if it doesn't exist when opened for writing or appending; it will be 550 truncated when opened for writing. :exc:`FileExistsError` will be raised if 551 it already exists when opened for creating. Opening a file for creating 552 implies writing, so this mode behaves in a similar way to ``'w'``. Add a 553 ``'+'`` to the mode to allow simultaneous reading and writing. 554 555 The :meth:`read` (when called with a positive argument), :meth:`readinto` 556 and :meth:`write` methods on this class will only make one system call. 557 558 A custom opener can be used by passing a callable as *opener*. The underlying 559 file descriptor for the file object is then obtained by calling *opener* with 560 (*name*, *flags*). *opener* must return an open file descriptor (passing 561 :mod:`os.open` as *opener* results in functionality similar to passing 562 ``None``). 563 564 The newly created file is :ref:`non-inheritable <fd_inheritance>`. 565 566 See the :func:`open` built-in function for examples on using the *opener* 567 parameter. 568 569 .. versionchanged:: 3.3 570 The *opener* parameter was added. 571 The ``'x'`` mode was added. 572 573 .. versionchanged:: 3.4 574 The file is now non-inheritable. 575 576 In addition to the attributes and methods from :class:`IOBase` and 577 :class:`RawIOBase`, :class:`FileIO` provides the following data 578 attributes: 579 580 .. attribute:: mode 581 582 The mode as given in the constructor. 583 584 .. attribute:: name 585 586 The file name. This is the file descriptor of the file when no name is 587 given in the constructor. 588 589 590 Buffered Streams 591 ^^^^^^^^^^^^^^^^ 592 593 Buffered I/O streams provide a higher-level interface to an I/O device 594 than raw I/O does. 595 596 .. class:: BytesIO([initial_bytes]) 597 598 A stream implementation using an in-memory bytes buffer. It inherits 599 :class:`BufferedIOBase`. The buffer is discarded when the 600 :meth:`~IOBase.close` method is called. 601 602 The optional argument *initial_bytes* is a :term:`bytes-like object` that 603 contains initial data. 604 605 :class:`BytesIO` provides or overrides these methods in addition to those 606 from :class:`BufferedIOBase` and :class:`IOBase`: 607 608 .. method:: getbuffer() 609 610 Return a readable and writable view over the contents of the buffer 611 without copying them. Also, mutating the view will transparently 612 update the contents of the buffer:: 613 614 >>> b = io.BytesIO(b"abcdef") 615 >>> view = b.getbuffer() 616 >>> view[2:4] = b"56" 617 >>> b.getvalue() 618 b'ab56ef' 619 620 .. note:: 621 As long as the view exists, the :class:`BytesIO` object cannot be 622 resized or closed. 623 624 .. versionadded:: 3.2 625 626 .. method:: getvalue() 627 628 Return :class:`bytes` containing the entire contents of the buffer. 629 630 631 .. method:: read1() 632 633 In :class:`BytesIO`, this is the same as :meth:`read`. 634 635 .. method:: readinto1() 636 637 In :class:`BytesIO`, this is the same as :meth:`readinto`. 638 639 .. versionadded:: 3.5 640 641 .. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE) 642 643 A buffer providing higher-level access to a readable, sequential 644 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`. 645 When reading data from this object, a larger amount of data may be 646 requested from the underlying raw stream, and kept in an internal buffer. 647 The buffered data can then be returned directly on subsequent reads. 648 649 The constructor creates a :class:`BufferedReader` for the given readable 650 *raw* stream and *buffer_size*. If *buffer_size* is omitted, 651 :data:`DEFAULT_BUFFER_SIZE` is used. 652 653 :class:`BufferedReader` provides or overrides these methods in addition to 654 those from :class:`BufferedIOBase` and :class:`IOBase`: 655 656 .. method:: peek([size]) 657 658 Return bytes from the stream without advancing the position. At most one 659 single read on the raw stream is done to satisfy the call. The number of 660 bytes returned may be less or more than requested. 661 662 .. method:: read([size]) 663 664 Read and return *size* bytes, or if *size* is not given or negative, until 665 EOF or if the read call would block in non-blocking mode. 666 667 .. method:: read1(size) 668 669 Read and return up to *size* bytes with only one call on the raw stream. 670 If at least one byte is buffered, only buffered bytes are returned. 671 Otherwise, one raw stream read call is made. 672 673 674 .. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE) 675 676 A buffer providing higher-level access to a writeable, sequential 677 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`. 678 When writing to this object, data is normally placed into an internal 679 buffer. The buffer will be written out to the underlying :class:`RawIOBase` 680 object under various conditions, including: 681 682 * when the buffer gets too small for all pending data; 683 * when :meth:`flush()` is called; 684 * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects); 685 * when the :class:`BufferedWriter` object is closed or destroyed. 686 687 The constructor creates a :class:`BufferedWriter` for the given writeable 688 *raw* stream. If the *buffer_size* is not given, it defaults to 689 :data:`DEFAULT_BUFFER_SIZE`. 690 691 :class:`BufferedWriter` provides or overrides these methods in addition to 692 those from :class:`BufferedIOBase` and :class:`IOBase`: 693 694 .. method:: flush() 695 696 Force bytes held in the buffer into the raw stream. A 697 :exc:`BlockingIOError` should be raised if the raw stream blocks. 698 699 .. method:: write(b) 700 701 Write the :term:`bytes-like object`, *b*, and return the 702 number of bytes written. When in non-blocking mode, a 703 :exc:`BlockingIOError` is raised if the buffer needs to be written out but 704 the raw stream blocks. 705 706 707 .. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE) 708 709 A buffered interface to random access streams. It inherits 710 :class:`BufferedReader` and :class:`BufferedWriter`, and further supports 711 :meth:`seek` and :meth:`tell` functionality. 712 713 The constructor creates a reader and writer for a seekable raw stream, given 714 in the first argument. If the *buffer_size* is omitted it defaults to 715 :data:`DEFAULT_BUFFER_SIZE`. 716 717 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or 718 :class:`BufferedWriter` can do. 719 720 721 .. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE) 722 723 A buffered I/O object combining two unidirectional :class:`RawIOBase` 724 objects -- one readable, the other writeable -- into a single bidirectional 725 endpoint. It inherits :class:`BufferedIOBase`. 726 727 *reader* and *writer* are :class:`RawIOBase` objects that are readable and 728 writeable respectively. If the *buffer_size* is omitted it defaults to 729 :data:`DEFAULT_BUFFER_SIZE`. 730 731 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods 732 except for :meth:`~BufferedIOBase.detach`, which raises 733 :exc:`UnsupportedOperation`. 734 735 .. warning:: 736 737 :class:`BufferedRWPair` does not attempt to synchronize accesses to 738 its underlying raw streams. You should not pass it the same object 739 as reader and writer; use :class:`BufferedRandom` instead. 740 741 742 Text I/O 743 ^^^^^^^^ 744 745 .. class:: TextIOBase 746 747 Base class for text streams. This class provides a character and line based 748 interface to stream I/O. There is no :meth:`readinto` method because 749 Python's character strings are immutable. It inherits :class:`IOBase`. 750 There is no public constructor. 751 752 :class:`TextIOBase` provides or overrides these data attributes and 753 methods in addition to those from :class:`IOBase`: 754 755 .. attribute:: encoding 756 757 The name of the encoding used to decode the stream's bytes into 758 strings, and to encode strings into bytes. 759 760 .. attribute:: errors 761 762 The error setting of the decoder or encoder. 763 764 .. attribute:: newlines 765 766 A string, a tuple of strings, or ``None``, indicating the newlines 767 translated so far. Depending on the implementation and the initial 768 constructor flags, this may not be available. 769 770 .. attribute:: buffer 771 772 The underlying binary buffer (a :class:`BufferedIOBase` instance) that 773 :class:`TextIOBase` deals with. This is not part of the 774 :class:`TextIOBase` API and may not exist in some implementations. 775 776 .. method:: detach() 777 778 Separate the underlying binary buffer from the :class:`TextIOBase` and 779 return it. 780 781 After the underlying buffer has been detached, the :class:`TextIOBase` is 782 in an unusable state. 783 784 Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not 785 have the concept of an underlying buffer and calling this method will 786 raise :exc:`UnsupportedOperation`. 787 788 .. versionadded:: 3.1 789 790 .. method:: read(size) 791 792 Read and return at most *size* characters from the stream as a single 793 :class:`str`. If *size* is negative or ``None``, reads until EOF. 794 795 .. method:: readline(size=-1) 796 797 Read until newline or EOF and return a single ``str``. If the stream is 798 already at EOF, an empty string is returned. 799 800 If *size* is specified, at most *size* characters will be read. 801 802 .. method:: seek(offset[, whence]) 803 804 Change the stream position to the given *offset*. Behaviour depends on 805 the *whence* parameter. The default value for *whence* is 806 :data:`SEEK_SET`. 807 808 * :data:`SEEK_SET` or ``0``: seek from the start of the stream 809 (the default); *offset* must either be a number returned by 810 :meth:`TextIOBase.tell`, or zero. Any other *offset* value 811 produces undefined behaviour. 812 * :data:`SEEK_CUR` or ``1``: "seek" to the current position; 813 *offset* must be zero, which is a no-operation (all other values 814 are unsupported). 815 * :data:`SEEK_END` or ``2``: seek to the end of the stream; 816 *offset* must be zero (all other values are unsupported). 817 818 Return the new absolute position as an opaque number. 819 820 .. versionadded:: 3.1 821 The ``SEEK_*`` constants. 822 823 .. method:: tell() 824 825 Return the current stream position as an opaque number. The number 826 does not usually represent a number of bytes in the underlying 827 binary storage. 828 829 .. method:: write(s) 830 831 Write the string *s* to the stream and return the number of characters 832 written. 833 834 835 .. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, \ 836 line_buffering=False, write_through=False) 837 838 A buffered text stream over a :class:`BufferedIOBase` binary stream. 839 It inherits :class:`TextIOBase`. 840 841 *encoding* gives the name of the encoding that the stream will be decoded or 842 encoded with. It defaults to 843 :func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`. 844 845 *errors* is an optional string that specifies how encoding and decoding 846 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError` 847 exception if there is an encoding error (the default of ``None`` has the same 848 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding 849 errors can lead to data loss.) ``'replace'`` causes a replacement marker 850 (such as ``'?'``) to be inserted where there is malformed data. 851 ``'backslashreplace'`` causes malformed data to be replaced by a 852 backslashed escape sequence. When writing, ``'xmlcharrefreplace'`` 853 (replace with the appropriate XML character reference) or ``'namereplace'`` 854 (replace with ``\N{...}`` escape sequences) can be used. Any other error 855 handling name that has been registered with 856 :func:`codecs.register_error` is also valid. 857 858 .. index:: 859 single: universal newlines; io.TextIOWrapper class 860 861 *newline* controls how line endings are handled. It can be ``None``, 862 ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows: 863 864 * When reading input from the stream, if *newline* is ``None``, 865 :term:`universal newlines` mode is enabled. Lines in the input can end in 866 ``'\n'``, ``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'`` 867 before being returned to the caller. If it is ``''``, universal newlines 868 mode is enabled, but line endings are returned to the caller untranslated. 869 If it has any of the other legal values, input lines are only terminated 870 by the given string, and the line ending is returned to the caller 871 untranslated. 872 873 * When writing output to the stream, if *newline* is ``None``, any ``'\n'`` 874 characters written are translated to the system default line separator, 875 :data:`os.linesep`. If *newline* is ``''`` or ``'\n'``, no translation 876 takes place. If *newline* is any of the other legal values, any ``'\n'`` 877 characters written are translated to the given string. 878 879 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to 880 write contains a newline character. 881 882 If *write_through* is ``True``, calls to :meth:`write` are guaranteed 883 not to be buffered: any data written on the :class:`TextIOWrapper` 884 object is immediately handled to its underlying binary *buffer*. 885 886 .. versionchanged:: 3.3 887 The *write_through* argument has been added. 888 889 .. versionchanged:: 3.3 890 The default *encoding* is now ``locale.getpreferredencoding(False)`` 891 instead of ``locale.getpreferredencoding()``. Don't change temporary the 892 locale encoding using :func:`locale.setlocale`, use the current locale 893 encoding instead of the user preferred encoding. 894 895 :class:`TextIOWrapper` provides one attribute in addition to those of 896 :class:`TextIOBase` and its parents: 897 898 .. attribute:: line_buffering 899 900 Whether line buffering is enabled. 901 902 903 .. class:: StringIO(initial_value='', newline='\\n') 904 905 An in-memory stream for text I/O. The text buffer is discarded when the 906 :meth:`~IOBase.close` method is called. 907 908 The initial value of the buffer can be set by providing *initial_value*. 909 If newline translation is enabled, newlines will be encoded as if by 910 :meth:`~TextIOBase.write`. The stream is positioned at the start of 911 the buffer. 912 913 The *newline* argument works like that of :class:`TextIOWrapper`. 914 The default is to consider only ``\n`` characters as ends of lines and 915 to do no newline translation. If *newline* is set to ``None``, 916 newlines are written as ``\n`` on all platforms, but universal 917 newline decoding is still performed when reading. 918 919 :class:`StringIO` provides this method in addition to those from 920 :class:`TextIOBase` and its parents: 921 922 .. method:: getvalue() 923 924 Return a ``str`` containing the entire contents of the buffer. 925 Newlines are decoded as if by :meth:`~TextIOBase.read`, although 926 the stream position is not changed. 927 928 Example usage:: 929 930 import io 931 932 output = io.StringIO() 933 output.write('First line.\n') 934 print('Second line.', file=output) 935 936 # Retrieve file contents -- this will be 937 # 'First line.\nSecond line.\n' 938 contents = output.getvalue() 939 940 # Close object and discard memory buffer -- 941 # .getvalue() will now raise an exception. 942 output.close() 943 944 945 .. index:: 946 single: universal newlines; io.IncrementalNewlineDecoder class 947 948 .. class:: IncrementalNewlineDecoder 949 950 A helper codec that decodes newlines for :term:`universal newlines` mode. 951 It inherits :class:`codecs.IncrementalDecoder`. 952 953 954 Performance 955 ----------- 956 957 This section discusses the performance of the provided concrete I/O 958 implementations. 959 960 Binary I/O 961 ^^^^^^^^^^ 962 963 By reading and writing only large chunks of data even when the user asks for a 964 single byte, buffered I/O hides any inefficiency in calling and executing the 965 operating system's unbuffered I/O routines. The gain depends on the OS and the 966 kind of I/O which is performed. For example, on some modern OSes such as Linux, 967 unbuffered disk I/O can be as fast as buffered I/O. The bottom line, however, 968 is that buffered I/O offers predictable performance regardless of the platform 969 and the backing device. Therefore, it is almost always preferable to use 970 buffered I/O rather than unbuffered I/O for binary data. 971 972 Text I/O 973 ^^^^^^^^ 974 975 Text I/O over a binary storage (such as a file) is significantly slower than 976 binary I/O over the same storage, because it requires conversions between 977 unicode and binary data using a character codec. This can become noticeable 978 handling huge amounts of text data like large log files. Also, 979 :meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both quite slow 980 due to the reconstruction algorithm used. 981 982 :class:`StringIO`, however, is a native in-memory unicode container and will 983 exhibit similar speed to :class:`BytesIO`. 984 985 Multi-threading 986 ^^^^^^^^^^^^^^^ 987 988 :class:`FileIO` objects are thread-safe to the extent that the operating system 989 calls (such as ``read(2)`` under Unix) they wrap are thread-safe too. 990 991 Binary buffered objects (instances of :class:`BufferedReader`, 992 :class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`) 993 protect their internal structures using a lock; it is therefore safe to call 994 them from multiple threads at once. 995 996 :class:`TextIOWrapper` objects are not thread-safe. 997 998 Reentrancy 999 ^^^^^^^^^^ 1000 1001 Binary buffered objects (instances of :class:`BufferedReader`, 1002 :class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`) 1003 are not reentrant. While reentrant calls will not happen in normal situations, 1004 they can arise from doing I/O in a :mod:`signal` handler. If a thread tries to 1005 re-enter a buffered object which it is already accessing, a :exc:`RuntimeError` 1006 is raised. Note this doesn't prohibit a different thread from entering the 1007 buffered object. 1008 1009 The above implicitly extends to text files, since the :func:`open()` function 1010 will wrap a buffered object inside a :class:`TextIOWrapper`. This includes 1011 standard streams and therefore affects the built-in function :func:`print()` as 1012 well. 1013 1014