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