17db96d56Sopenharmony_ci:mod:`io` --- Core tools for working with streams 27db96d56Sopenharmony_ci================================================= 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: io 57db96d56Sopenharmony_ci :synopsis: Core tools for working with streams. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. moduleauthor:: Guido van Rossum <guido@python.org> 87db96d56Sopenharmony_ci.. moduleauthor:: Mike Verdone <mike.verdone@gmail.com> 97db96d56Sopenharmony_ci.. moduleauthor:: Mark Russell <mark.russell@zen.co.uk> 107db96d56Sopenharmony_ci.. moduleauthor:: Antoine Pitrou <solipsis@pitrou.net> 117db96d56Sopenharmony_ci.. moduleauthor:: Amaury Forgeot d'Arc <amauryfa@gmail.com> 127db96d56Sopenharmony_ci.. moduleauthor:: Benjamin Peterson <benjamin@python.org> 137db96d56Sopenharmony_ci.. sectionauthor:: Benjamin Peterson <benjamin@python.org> 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ci**Source code:** :source:`Lib/io.py` 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ci-------------- 187db96d56Sopenharmony_ci 197db96d56Sopenharmony_ci.. _io-overview: 207db96d56Sopenharmony_ci 217db96d56Sopenharmony_ciOverview 227db96d56Sopenharmony_ci-------- 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ci.. index:: 257db96d56Sopenharmony_ci single: file object; io module 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ciThe :mod:`io` module provides Python's main facilities for dealing with various 287db96d56Sopenharmony_citypes of I/O. There are three main types of I/O: *text I/O*, *binary I/O* 297db96d56Sopenharmony_ciand *raw I/O*. These are generic categories, and various backing stores can 307db96d56Sopenharmony_cibe used for each of them. A concrete object belonging to any of these 317db96d56Sopenharmony_cicategories is called a :term:`file object`. Other common terms are *stream* 327db96d56Sopenharmony_ciand *file-like object*. 337db96d56Sopenharmony_ci 347db96d56Sopenharmony_ciIndependent of its category, each concrete stream object will also have 357db96d56Sopenharmony_civarious capabilities: it can be read-only, write-only, or read-write. It can 367db96d56Sopenharmony_cialso allow arbitrary random access (seeking forwards or backwards to any 377db96d56Sopenharmony_cilocation), or only sequential access (for example in the case of a socket or 387db96d56Sopenharmony_cipipe). 397db96d56Sopenharmony_ci 407db96d56Sopenharmony_ciAll streams are careful about the type of data you give to them. For example 417db96d56Sopenharmony_cigiving a :class:`str` object to the ``write()`` method of a binary stream 427db96d56Sopenharmony_ciwill raise a :exc:`TypeError`. So will giving a :class:`bytes` object to the 437db96d56Sopenharmony_ci``write()`` method of a text stream. 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ci.. versionchanged:: 3.3 467db96d56Sopenharmony_ci Operations that used to raise :exc:`IOError` now raise :exc:`OSError`, since 477db96d56Sopenharmony_ci :exc:`IOError` is now an alias of :exc:`OSError`. 487db96d56Sopenharmony_ci 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ciText I/O 517db96d56Sopenharmony_ci^^^^^^^^ 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ciText I/O expects and produces :class:`str` objects. This means that whenever 547db96d56Sopenharmony_cithe backing store is natively made of bytes (such as in the case of a file), 557db96d56Sopenharmony_ciencoding and decoding of data is made transparently as well as optional 567db96d56Sopenharmony_citranslation of platform-specific newline characters. 577db96d56Sopenharmony_ci 587db96d56Sopenharmony_ciThe easiest way to create a text stream is with :meth:`open()`, optionally 597db96d56Sopenharmony_cispecifying an encoding:: 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ci f = open("myfile.txt", "r", encoding="utf-8") 627db96d56Sopenharmony_ci 637db96d56Sopenharmony_ciIn-memory text streams are also available as :class:`StringIO` objects:: 647db96d56Sopenharmony_ci 657db96d56Sopenharmony_ci f = io.StringIO("some initial text data") 667db96d56Sopenharmony_ci 677db96d56Sopenharmony_ciThe text stream API is described in detail in the documentation of 687db96d56Sopenharmony_ci:class:`TextIOBase`. 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ciBinary I/O 727db96d56Sopenharmony_ci^^^^^^^^^^ 737db96d56Sopenharmony_ci 747db96d56Sopenharmony_ciBinary I/O (also called *buffered I/O*) expects 757db96d56Sopenharmony_ci:term:`bytes-like objects <bytes-like object>` and produces :class:`bytes` 767db96d56Sopenharmony_ciobjects. No encoding, decoding, or newline translation is performed. This 777db96d56Sopenharmony_cicategory of streams can be used for all kinds of non-text data, and also when 787db96d56Sopenharmony_cimanual control over the handling of text data is desired. 797db96d56Sopenharmony_ci 807db96d56Sopenharmony_ciThe easiest way to create a binary stream is with :meth:`open()` with ``'b'`` in 817db96d56Sopenharmony_cithe mode string:: 827db96d56Sopenharmony_ci 837db96d56Sopenharmony_ci f = open("myfile.jpg", "rb") 847db96d56Sopenharmony_ci 857db96d56Sopenharmony_ciIn-memory binary streams are also available as :class:`BytesIO` objects:: 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ci f = io.BytesIO(b"some initial binary data: \x00\x01") 887db96d56Sopenharmony_ci 897db96d56Sopenharmony_ciThe binary stream API is described in detail in the docs of 907db96d56Sopenharmony_ci:class:`BufferedIOBase`. 917db96d56Sopenharmony_ci 927db96d56Sopenharmony_ciOther library modules may provide additional ways to create text or binary 937db96d56Sopenharmony_cistreams. See :meth:`socket.socket.makefile` for example. 947db96d56Sopenharmony_ci 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ciRaw I/O 977db96d56Sopenharmony_ci^^^^^^^ 987db96d56Sopenharmony_ci 997db96d56Sopenharmony_ciRaw I/O (also called *unbuffered I/O*) is generally used as a low-level 1007db96d56Sopenharmony_cibuilding-block for binary and text streams; it is rarely useful to directly 1017db96d56Sopenharmony_cimanipulate a raw stream from user code. Nevertheless, you can create a raw 1027db96d56Sopenharmony_cistream by opening a file in binary mode with buffering disabled:: 1037db96d56Sopenharmony_ci 1047db96d56Sopenharmony_ci f = open("myfile.jpg", "rb", buffering=0) 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ciThe raw stream API is described in detail in the docs of :class:`RawIOBase`. 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ci 1097db96d56Sopenharmony_ci.. _io-text-encoding: 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ciText Encoding 1127db96d56Sopenharmony_ci------------- 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ciThe default encoding of :class:`TextIOWrapper` and :func:`open` is 1157db96d56Sopenharmony_cilocale-specific (:func:`locale.getencoding`). 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ciHowever, many developers forget to specify the encoding when opening text files 1187db96d56Sopenharmony_ciencoded in UTF-8 (e.g. JSON, TOML, Markdown, etc...) since most Unix 1197db96d56Sopenharmony_ciplatforms use UTF-8 locale by default. This causes bugs because the locale 1207db96d56Sopenharmony_ciencoding is not UTF-8 for most Windows users. For example:: 1217db96d56Sopenharmony_ci 1227db96d56Sopenharmony_ci # May not work on Windows when non-ASCII characters in the file. 1237db96d56Sopenharmony_ci with open("README.md") as f: 1247db96d56Sopenharmony_ci long_description = f.read() 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ciAccordingly, it is highly recommended that you specify the encoding 1277db96d56Sopenharmony_ciexplicitly when opening text files. If you want to use UTF-8, pass 1287db96d56Sopenharmony_ci``encoding="utf-8"``. To use the current locale encoding, 1297db96d56Sopenharmony_ci``encoding="locale"`` is supported since Python 3.10. 1307db96d56Sopenharmony_ci 1317db96d56Sopenharmony_ci.. seealso:: 1327db96d56Sopenharmony_ci 1337db96d56Sopenharmony_ci :ref:`utf8-mode` 1347db96d56Sopenharmony_ci Python UTF-8 Mode can be used to change the default encoding to 1357db96d56Sopenharmony_ci UTF-8 from locale-specific encoding. 1367db96d56Sopenharmony_ci 1377db96d56Sopenharmony_ci :pep:`686` 1387db96d56Sopenharmony_ci Python 3.15 will make :ref:`utf8-mode` default. 1397db96d56Sopenharmony_ci 1407db96d56Sopenharmony_ci.. _io-encoding-warning: 1417db96d56Sopenharmony_ci 1427db96d56Sopenharmony_ciOpt-in EncodingWarning 1437db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^ 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci.. versionadded:: 3.10 1467db96d56Sopenharmony_ci See :pep:`597` for more details. 1477db96d56Sopenharmony_ci 1487db96d56Sopenharmony_ciTo find where the default locale encoding is used, you can enable 1497db96d56Sopenharmony_cithe ``-X warn_default_encoding`` command line option or set the 1507db96d56Sopenharmony_ci:envvar:`PYTHONWARNDEFAULTENCODING` environment variable, which will 1517db96d56Sopenharmony_ciemit an :exc:`EncodingWarning` when the default encoding is used. 1527db96d56Sopenharmony_ci 1537db96d56Sopenharmony_ciIf you are providing an API that uses :func:`open` or 1547db96d56Sopenharmony_ci:class:`TextIOWrapper` and passes ``encoding=None`` as a parameter, you 1557db96d56Sopenharmony_cican use :func:`text_encoding` so that callers of the API will emit an 1567db96d56Sopenharmony_ci:exc:`EncodingWarning` if they don't pass an ``encoding``. However, 1577db96d56Sopenharmony_ciplease consider using UTF-8 by default (i.e. ``encoding="utf-8"``) for 1587db96d56Sopenharmony_cinew APIs. 1597db96d56Sopenharmony_ci 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_ciHigh-level Module Interface 1627db96d56Sopenharmony_ci--------------------------- 1637db96d56Sopenharmony_ci 1647db96d56Sopenharmony_ci.. data:: DEFAULT_BUFFER_SIZE 1657db96d56Sopenharmony_ci 1667db96d56Sopenharmony_ci An int containing the default buffer size used by the module's buffered I/O 1677db96d56Sopenharmony_ci classes. :func:`open` uses the file's blksize (as obtained by 1687db96d56Sopenharmony_ci :func:`os.stat`) if possible. 1697db96d56Sopenharmony_ci 1707db96d56Sopenharmony_ci 1717db96d56Sopenharmony_ci.. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_ci This is an alias for the builtin :func:`open` function. 1747db96d56Sopenharmony_ci 1757db96d56Sopenharmony_ci .. audit-event:: open path,mode,flags io.open 1767db96d56Sopenharmony_ci 1777db96d56Sopenharmony_ci This function raises an :ref:`auditing event <auditing>` ``open`` with 1787db96d56Sopenharmony_ci arguments ``path``, ``mode`` and ``flags``. The ``mode`` and ``flags`` 1797db96d56Sopenharmony_ci arguments may have been modified or inferred from the original call. 1807db96d56Sopenharmony_ci 1817db96d56Sopenharmony_ci 1827db96d56Sopenharmony_ci.. function:: open_code(path) 1837db96d56Sopenharmony_ci 1847db96d56Sopenharmony_ci Opens the provided file with mode ``'rb'``. This function should be used 1857db96d56Sopenharmony_ci when the intent is to treat the contents as executable code. 1867db96d56Sopenharmony_ci 1877db96d56Sopenharmony_ci ``path`` should be a :class:`str` and an absolute path. 1887db96d56Sopenharmony_ci 1897db96d56Sopenharmony_ci The behavior of this function may be overridden by an earlier call to the 1907db96d56Sopenharmony_ci :c:func:`PyFile_SetOpenCodeHook`. However, assuming that ``path`` is a 1917db96d56Sopenharmony_ci :class:`str` and an absolute path, ``open_code(path)`` should always behave 1927db96d56Sopenharmony_ci the same as ``open(path, 'rb')``. Overriding the behavior is intended for 1937db96d56Sopenharmony_ci additional validation or preprocessing of the file. 1947db96d56Sopenharmony_ci 1957db96d56Sopenharmony_ci .. versionadded:: 3.8 1967db96d56Sopenharmony_ci 1977db96d56Sopenharmony_ci 1987db96d56Sopenharmony_ci.. function:: text_encoding(encoding, stacklevel=2, /) 1997db96d56Sopenharmony_ci 2007db96d56Sopenharmony_ci This is a helper function for callables that use :func:`open` or 2017db96d56Sopenharmony_ci :class:`TextIOWrapper` and have an ``encoding=None`` parameter. 2027db96d56Sopenharmony_ci 2037db96d56Sopenharmony_ci This function returns *encoding* if it is not ``None``. 2047db96d56Sopenharmony_ci Otherwise, it returns ``"locale"`` or ``"utf-8"`` depending on 2057db96d56Sopenharmony_ci :ref:`UTF-8 Mode <utf8-mode>`. 2067db96d56Sopenharmony_ci 2077db96d56Sopenharmony_ci This function emits an :class:`EncodingWarning` if 2087db96d56Sopenharmony_ci :data:`sys.flags.warn_default_encoding <sys.flags>` is true and *encoding* 2097db96d56Sopenharmony_ci is ``None``. *stacklevel* specifies where the warning is emitted. 2107db96d56Sopenharmony_ci For example:: 2117db96d56Sopenharmony_ci 2127db96d56Sopenharmony_ci def read_text(path, encoding=None): 2137db96d56Sopenharmony_ci encoding = io.text_encoding(encoding) # stacklevel=2 2147db96d56Sopenharmony_ci with open(path, encoding) as f: 2157db96d56Sopenharmony_ci return f.read() 2167db96d56Sopenharmony_ci 2177db96d56Sopenharmony_ci In this example, an :class:`EncodingWarning` is emitted for the caller of 2187db96d56Sopenharmony_ci ``read_text()``. 2197db96d56Sopenharmony_ci 2207db96d56Sopenharmony_ci See :ref:`io-text-encoding` for more information. 2217db96d56Sopenharmony_ci 2227db96d56Sopenharmony_ci .. versionadded:: 3.10 2237db96d56Sopenharmony_ci 2247db96d56Sopenharmony_ci .. versionchanged:: 3.11 2257db96d56Sopenharmony_ci :func:`text_encoding` returns "utf-8" when UTF-8 mode is enabled and 2267db96d56Sopenharmony_ci *encoding* is ``None``. 2277db96d56Sopenharmony_ci 2287db96d56Sopenharmony_ci 2297db96d56Sopenharmony_ci.. exception:: BlockingIOError 2307db96d56Sopenharmony_ci 2317db96d56Sopenharmony_ci This is a compatibility alias for the builtin :exc:`BlockingIOError` 2327db96d56Sopenharmony_ci exception. 2337db96d56Sopenharmony_ci 2347db96d56Sopenharmony_ci 2357db96d56Sopenharmony_ci.. exception:: UnsupportedOperation 2367db96d56Sopenharmony_ci 2377db96d56Sopenharmony_ci An exception inheriting :exc:`OSError` and :exc:`ValueError` that is raised 2387db96d56Sopenharmony_ci when an unsupported operation is called on a stream. 2397db96d56Sopenharmony_ci 2407db96d56Sopenharmony_ci 2417db96d56Sopenharmony_ci.. seealso:: 2427db96d56Sopenharmony_ci 2437db96d56Sopenharmony_ci :mod:`sys` 2447db96d56Sopenharmony_ci contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`, 2457db96d56Sopenharmony_ci and :data:`sys.stderr`. 2467db96d56Sopenharmony_ci 2477db96d56Sopenharmony_ci 2487db96d56Sopenharmony_ciClass hierarchy 2497db96d56Sopenharmony_ci--------------- 2507db96d56Sopenharmony_ci 2517db96d56Sopenharmony_ciThe implementation of I/O streams is organized as a hierarchy of classes. First 2527db96d56Sopenharmony_ci:term:`abstract base classes <abstract base class>` (ABCs), which are used to 2537db96d56Sopenharmony_cispecify the various categories of streams, then concrete classes providing the 2547db96d56Sopenharmony_cistandard stream implementations. 2557db96d56Sopenharmony_ci 2567db96d56Sopenharmony_ci .. note:: 2577db96d56Sopenharmony_ci 2587db96d56Sopenharmony_ci The abstract base classes also provide default implementations of some 2597db96d56Sopenharmony_ci methods in order to help implementation of concrete stream classes. For 2607db96d56Sopenharmony_ci example, :class:`BufferedIOBase` provides unoptimized implementations of 2617db96d56Sopenharmony_ci :meth:`~IOBase.readinto` and :meth:`~IOBase.readline`. 2627db96d56Sopenharmony_ci 2637db96d56Sopenharmony_ciAt the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It 2647db96d56Sopenharmony_cidefines the basic interface to a stream. Note, however, that there is no 2657db96d56Sopenharmony_ciseparation between reading and writing to streams; implementations are allowed 2667db96d56Sopenharmony_cito raise :exc:`UnsupportedOperation` if they do not support a given operation. 2677db96d56Sopenharmony_ci 2687db96d56Sopenharmony_ciThe :class:`RawIOBase` ABC extends :class:`IOBase`. It deals with the reading 2697db96d56Sopenharmony_ciand writing of bytes to a stream. :class:`FileIO` subclasses :class:`RawIOBase` 2707db96d56Sopenharmony_cito provide an interface to files in the machine's file system. 2717db96d56Sopenharmony_ci 2727db96d56Sopenharmony_ciThe :class:`BufferedIOBase` ABC extends :class:`IOBase`. It deals with 2737db96d56Sopenharmony_cibuffering on a raw binary stream (:class:`RawIOBase`). Its subclasses, 2747db96d56Sopenharmony_ci:class:`BufferedWriter`, :class:`BufferedReader`, and :class:`BufferedRWPair` 2757db96d56Sopenharmony_cibuffer raw binary streams that are writable, readable, and both readable and writable, 2767db96d56Sopenharmony_cirespectively. :class:`BufferedRandom` provides a buffered interface to seekable streams. 2777db96d56Sopenharmony_ciAnother :class:`BufferedIOBase` subclass, :class:`BytesIO`, is a stream of 2787db96d56Sopenharmony_ciin-memory bytes. 2797db96d56Sopenharmony_ci 2807db96d56Sopenharmony_ciThe :class:`TextIOBase` ABC extends :class:`IOBase`. It deals with 2817db96d56Sopenharmony_cistreams whose bytes represent text, and handles encoding and decoding to and 2827db96d56Sopenharmony_cifrom strings. :class:`TextIOWrapper`, which extends :class:`TextIOBase`, is a buffered text 2837db96d56Sopenharmony_ciinterface to a buffered raw stream (:class:`BufferedIOBase`). Finally, 2847db96d56Sopenharmony_ci:class:`StringIO` is an in-memory stream for text. 2857db96d56Sopenharmony_ci 2867db96d56Sopenharmony_ciArgument names are not part of the specification, and only the arguments of 2877db96d56Sopenharmony_ci:func:`open` are intended to be used as keyword arguments. 2887db96d56Sopenharmony_ci 2897db96d56Sopenharmony_ciThe following table summarizes the ABCs provided by the :mod:`io` module: 2907db96d56Sopenharmony_ci 2917db96d56Sopenharmony_ci.. tabularcolumns:: |l|l|L|L| 2927db96d56Sopenharmony_ci 2937db96d56Sopenharmony_ci========================= ================== ======================== ================================================== 2947db96d56Sopenharmony_ciABC Inherits Stub Methods Mixin Methods and Properties 2957db96d56Sopenharmony_ci========================= ================== ======================== ================================================== 2967db96d56Sopenharmony_ci:class:`IOBase` ``fileno``, ``seek``, ``close``, ``closed``, ``__enter__``, 2977db96d56Sopenharmony_ci and ``truncate`` ``__exit__``, ``flush``, ``isatty``, ``__iter__``, 2987db96d56Sopenharmony_ci ``__next__``, ``readable``, ``readline``, 2997db96d56Sopenharmony_ci ``readlines``, ``seekable``, ``tell``, 3007db96d56Sopenharmony_ci ``writable``, and ``writelines`` 3017db96d56Sopenharmony_ci:class:`RawIOBase` :class:`IOBase` ``readinto`` and Inherited :class:`IOBase` methods, ``read``, 3027db96d56Sopenharmony_ci ``write`` and ``readall`` 3037db96d56Sopenharmony_ci:class:`BufferedIOBase` :class:`IOBase` ``detach``, ``read``, Inherited :class:`IOBase` methods, ``readinto``, 3047db96d56Sopenharmony_ci ``read1``, and ``write`` and ``readinto1`` 3057db96d56Sopenharmony_ci:class:`TextIOBase` :class:`IOBase` ``detach``, ``read``, Inherited :class:`IOBase` methods, ``encoding``, 3067db96d56Sopenharmony_ci ``readline``, and ``errors``, and ``newlines`` 3077db96d56Sopenharmony_ci ``write`` 3087db96d56Sopenharmony_ci========================= ================== ======================== ================================================== 3097db96d56Sopenharmony_ci 3107db96d56Sopenharmony_ci 3117db96d56Sopenharmony_ciI/O Base Classes 3127db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^ 3137db96d56Sopenharmony_ci 3147db96d56Sopenharmony_ci.. class:: IOBase 3157db96d56Sopenharmony_ci 3167db96d56Sopenharmony_ci The abstract base class for all I/O classes. 3177db96d56Sopenharmony_ci 3187db96d56Sopenharmony_ci This class provides empty abstract implementations for many methods 3197db96d56Sopenharmony_ci that derived classes can override selectively; the default 3207db96d56Sopenharmony_ci implementations represent a file that cannot be read, written or 3217db96d56Sopenharmony_ci seeked. 3227db96d56Sopenharmony_ci 3237db96d56Sopenharmony_ci Even though :class:`IOBase` does not declare :meth:`read` 3247db96d56Sopenharmony_ci or :meth:`write` because their signatures will vary, implementations and 3257db96d56Sopenharmony_ci clients should consider those methods part of the interface. Also, 3267db96d56Sopenharmony_ci implementations may raise a :exc:`ValueError` (or :exc:`UnsupportedOperation`) 3277db96d56Sopenharmony_ci when operations they do not support are called. 3287db96d56Sopenharmony_ci 3297db96d56Sopenharmony_ci The basic type used for binary data read from or written to a file is 3307db96d56Sopenharmony_ci :class:`bytes`. Other :term:`bytes-like objects <bytes-like object>` are 3317db96d56Sopenharmony_ci accepted as method arguments too. Text I/O classes work with :class:`str` data. 3327db96d56Sopenharmony_ci 3337db96d56Sopenharmony_ci Note that calling any method (even inquiries) on a closed stream is 3347db96d56Sopenharmony_ci undefined. Implementations may raise :exc:`ValueError` in this case. 3357db96d56Sopenharmony_ci 3367db96d56Sopenharmony_ci :class:`IOBase` (and its subclasses) supports the iterator protocol, meaning 3377db96d56Sopenharmony_ci that an :class:`IOBase` object can be iterated over yielding the lines in a 3387db96d56Sopenharmony_ci stream. Lines are defined slightly differently depending on whether the 3397db96d56Sopenharmony_ci stream is a binary stream (yielding bytes), or a text stream (yielding 3407db96d56Sopenharmony_ci character strings). See :meth:`~IOBase.readline` below. 3417db96d56Sopenharmony_ci 3427db96d56Sopenharmony_ci :class:`IOBase` is also a context manager and therefore supports the 3437db96d56Sopenharmony_ci :keyword:`with` statement. In this example, *file* is closed after the 3447db96d56Sopenharmony_ci :keyword:`!with` statement's suite is finished---even if an exception occurs:: 3457db96d56Sopenharmony_ci 3467db96d56Sopenharmony_ci with open('spam.txt', 'w') as file: 3477db96d56Sopenharmony_ci file.write('Spam and eggs!') 3487db96d56Sopenharmony_ci 3497db96d56Sopenharmony_ci :class:`IOBase` provides these data attributes and methods: 3507db96d56Sopenharmony_ci 3517db96d56Sopenharmony_ci .. method:: close() 3527db96d56Sopenharmony_ci 3537db96d56Sopenharmony_ci Flush and close this stream. This method has no effect if the file is 3547db96d56Sopenharmony_ci already closed. Once the file is closed, any operation on the file 3557db96d56Sopenharmony_ci (e.g. reading or writing) will raise a :exc:`ValueError`. 3567db96d56Sopenharmony_ci 3577db96d56Sopenharmony_ci As a convenience, it is allowed to call this method more than once; 3587db96d56Sopenharmony_ci only the first call, however, will have an effect. 3597db96d56Sopenharmony_ci 3607db96d56Sopenharmony_ci .. attribute:: closed 3617db96d56Sopenharmony_ci 3627db96d56Sopenharmony_ci ``True`` if the stream is closed. 3637db96d56Sopenharmony_ci 3647db96d56Sopenharmony_ci .. method:: fileno() 3657db96d56Sopenharmony_ci 3667db96d56Sopenharmony_ci Return the underlying file descriptor (an integer) of the stream if it 3677db96d56Sopenharmony_ci exists. An :exc:`OSError` is raised if the IO object does not use a file 3687db96d56Sopenharmony_ci descriptor. 3697db96d56Sopenharmony_ci 3707db96d56Sopenharmony_ci .. method:: flush() 3717db96d56Sopenharmony_ci 3727db96d56Sopenharmony_ci Flush the write buffers of the stream if applicable. This does nothing 3737db96d56Sopenharmony_ci for read-only and non-blocking streams. 3747db96d56Sopenharmony_ci 3757db96d56Sopenharmony_ci .. method:: isatty() 3767db96d56Sopenharmony_ci 3777db96d56Sopenharmony_ci Return ``True`` if the stream is interactive (i.e., connected to 3787db96d56Sopenharmony_ci a terminal/tty device). 3797db96d56Sopenharmony_ci 3807db96d56Sopenharmony_ci .. method:: readable() 3817db96d56Sopenharmony_ci 3827db96d56Sopenharmony_ci Return ``True`` if the stream can be read from. If ``False``, :meth:`read` 3837db96d56Sopenharmony_ci will raise :exc:`OSError`. 3847db96d56Sopenharmony_ci 3857db96d56Sopenharmony_ci .. method:: readline(size=-1, /) 3867db96d56Sopenharmony_ci 3877db96d56Sopenharmony_ci Read and return one line from the stream. If *size* is specified, at 3887db96d56Sopenharmony_ci most *size* bytes will be read. 3897db96d56Sopenharmony_ci 3907db96d56Sopenharmony_ci The line terminator is always ``b'\n'`` for binary files; for text files, 3917db96d56Sopenharmony_ci the *newline* argument to :func:`open` can be used to select the line 3927db96d56Sopenharmony_ci terminator(s) recognized. 3937db96d56Sopenharmony_ci 3947db96d56Sopenharmony_ci .. method:: readlines(hint=-1, /) 3957db96d56Sopenharmony_ci 3967db96d56Sopenharmony_ci Read and return a list of lines from the stream. *hint* can be specified 3977db96d56Sopenharmony_ci to control the number of lines read: no more lines will be read if the 3987db96d56Sopenharmony_ci total size (in bytes/characters) of all lines so far exceeds *hint*. 3997db96d56Sopenharmony_ci 4007db96d56Sopenharmony_ci *hint* values of ``0`` or less, as well as ``None``, are treated as no 4017db96d56Sopenharmony_ci hint. 4027db96d56Sopenharmony_ci 4037db96d56Sopenharmony_ci Note that it's already possible to iterate on file objects using ``for 4047db96d56Sopenharmony_ci line in file: ...`` without calling ``file.readlines()``. 4057db96d56Sopenharmony_ci 4067db96d56Sopenharmony_ci .. method:: seek(offset, whence=SEEK_SET, /) 4077db96d56Sopenharmony_ci 4087db96d56Sopenharmony_ci Change the stream position to the given byte *offset*. *offset* is 4097db96d56Sopenharmony_ci interpreted relative to the position indicated by *whence*. The default 4107db96d56Sopenharmony_ci value for *whence* is :data:`SEEK_SET`. Values for *whence* are: 4117db96d56Sopenharmony_ci 4127db96d56Sopenharmony_ci * :data:`SEEK_SET` or ``0`` -- start of the stream (the default); 4137db96d56Sopenharmony_ci *offset* should be zero or positive 4147db96d56Sopenharmony_ci * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may 4157db96d56Sopenharmony_ci be negative 4167db96d56Sopenharmony_ci * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually 4177db96d56Sopenharmony_ci negative 4187db96d56Sopenharmony_ci 4197db96d56Sopenharmony_ci Return the new absolute position. 4207db96d56Sopenharmony_ci 4217db96d56Sopenharmony_ci .. versionadded:: 3.1 4227db96d56Sopenharmony_ci The ``SEEK_*`` constants. 4237db96d56Sopenharmony_ci 4247db96d56Sopenharmony_ci .. versionadded:: 3.3 4257db96d56Sopenharmony_ci Some operating systems could support additional values, like 4267db96d56Sopenharmony_ci :data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`. The valid values 4277db96d56Sopenharmony_ci for a file could depend on it being open in text or binary mode. 4287db96d56Sopenharmony_ci 4297db96d56Sopenharmony_ci .. method:: seekable() 4307db96d56Sopenharmony_ci 4317db96d56Sopenharmony_ci Return ``True`` if the stream supports random access. If ``False``, 4327db96d56Sopenharmony_ci :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`OSError`. 4337db96d56Sopenharmony_ci 4347db96d56Sopenharmony_ci .. method:: tell() 4357db96d56Sopenharmony_ci 4367db96d56Sopenharmony_ci Return the current stream position. 4377db96d56Sopenharmony_ci 4387db96d56Sopenharmony_ci .. method:: truncate(size=None, /) 4397db96d56Sopenharmony_ci 4407db96d56Sopenharmony_ci Resize the stream to the given *size* in bytes (or the current position 4417db96d56Sopenharmony_ci if *size* is not specified). The current stream position isn't changed. 4427db96d56Sopenharmony_ci This resizing can extend or reduce the current file size. In case of 4437db96d56Sopenharmony_ci extension, the contents of the new file area depend on the platform 4447db96d56Sopenharmony_ci (on most systems, additional bytes are zero-filled). The new file size 4457db96d56Sopenharmony_ci is returned. 4467db96d56Sopenharmony_ci 4477db96d56Sopenharmony_ci .. versionchanged:: 3.5 4487db96d56Sopenharmony_ci Windows will now zero-fill files when extending. 4497db96d56Sopenharmony_ci 4507db96d56Sopenharmony_ci .. method:: writable() 4517db96d56Sopenharmony_ci 4527db96d56Sopenharmony_ci Return ``True`` if the stream supports writing. If ``False``, 4537db96d56Sopenharmony_ci :meth:`write` and :meth:`truncate` will raise :exc:`OSError`. 4547db96d56Sopenharmony_ci 4557db96d56Sopenharmony_ci .. method:: writelines(lines, /) 4567db96d56Sopenharmony_ci 4577db96d56Sopenharmony_ci Write a list of lines to the stream. Line separators are not added, so it 4587db96d56Sopenharmony_ci is usual for each of the lines provided to have a line separator at the 4597db96d56Sopenharmony_ci end. 4607db96d56Sopenharmony_ci 4617db96d56Sopenharmony_ci .. method:: __del__() 4627db96d56Sopenharmony_ci 4637db96d56Sopenharmony_ci Prepare for object destruction. :class:`IOBase` provides a default 4647db96d56Sopenharmony_ci implementation of this method that calls the instance's 4657db96d56Sopenharmony_ci :meth:`~IOBase.close` method. 4667db96d56Sopenharmony_ci 4677db96d56Sopenharmony_ci 4687db96d56Sopenharmony_ci.. class:: RawIOBase 4697db96d56Sopenharmony_ci 4707db96d56Sopenharmony_ci Base class for raw binary streams. It inherits :class:`IOBase`. 4717db96d56Sopenharmony_ci 4727db96d56Sopenharmony_ci Raw binary streams typically provide low-level access to an underlying OS 4737db96d56Sopenharmony_ci device or API, and do not try to encapsulate it in high-level primitives 4747db96d56Sopenharmony_ci (this functionality is done at a higher-level in buffered binary streams and text streams, described later 4757db96d56Sopenharmony_ci in this page). 4767db96d56Sopenharmony_ci 4777db96d56Sopenharmony_ci :class:`RawIOBase` provides these methods in addition to those from 4787db96d56Sopenharmony_ci :class:`IOBase`: 4797db96d56Sopenharmony_ci 4807db96d56Sopenharmony_ci .. method:: read(size=-1, /) 4817db96d56Sopenharmony_ci 4827db96d56Sopenharmony_ci Read up to *size* bytes from the object and return them. As a convenience, 4837db96d56Sopenharmony_ci if *size* is unspecified or -1, all bytes until EOF are returned. 4847db96d56Sopenharmony_ci Otherwise, only one system call is ever made. Fewer than *size* bytes may 4857db96d56Sopenharmony_ci be returned if the operating system call returns fewer than *size* bytes. 4867db96d56Sopenharmony_ci 4877db96d56Sopenharmony_ci If 0 bytes are returned, and *size* was not 0, this indicates end of file. 4887db96d56Sopenharmony_ci If the object is in non-blocking mode and no bytes are available, 4897db96d56Sopenharmony_ci ``None`` is returned. 4907db96d56Sopenharmony_ci 4917db96d56Sopenharmony_ci The default implementation defers to :meth:`readall` and 4927db96d56Sopenharmony_ci :meth:`readinto`. 4937db96d56Sopenharmony_ci 4947db96d56Sopenharmony_ci .. method:: readall() 4957db96d56Sopenharmony_ci 4967db96d56Sopenharmony_ci Read and return all the bytes from the stream until EOF, using multiple 4977db96d56Sopenharmony_ci calls to the stream if necessary. 4987db96d56Sopenharmony_ci 4997db96d56Sopenharmony_ci .. method:: readinto(b, /) 5007db96d56Sopenharmony_ci 5017db96d56Sopenharmony_ci Read bytes into a pre-allocated, writable 5027db96d56Sopenharmony_ci :term:`bytes-like object` *b*, and return the 5037db96d56Sopenharmony_ci number of bytes read. For example, *b* might be a :class:`bytearray`. 5047db96d56Sopenharmony_ci If the object is in non-blocking mode and no bytes 5057db96d56Sopenharmony_ci are available, ``None`` is returned. 5067db96d56Sopenharmony_ci 5077db96d56Sopenharmony_ci .. method:: write(b, /) 5087db96d56Sopenharmony_ci 5097db96d56Sopenharmony_ci Write the given :term:`bytes-like object`, *b*, to the 5107db96d56Sopenharmony_ci underlying raw stream, and return the number of 5117db96d56Sopenharmony_ci bytes written. This can be less than the length of *b* in 5127db96d56Sopenharmony_ci bytes, depending on specifics of the underlying raw 5137db96d56Sopenharmony_ci stream, and especially if it is in non-blocking mode. ``None`` is 5147db96d56Sopenharmony_ci returned if the raw stream is set not to block and no single byte could 5157db96d56Sopenharmony_ci be readily written to it. The caller may release or mutate *b* after 5167db96d56Sopenharmony_ci this method returns, so the implementation should only access *b* 5177db96d56Sopenharmony_ci during the method call. 5187db96d56Sopenharmony_ci 5197db96d56Sopenharmony_ci 5207db96d56Sopenharmony_ci.. class:: BufferedIOBase 5217db96d56Sopenharmony_ci 5227db96d56Sopenharmony_ci Base class for binary streams that support some kind of buffering. 5237db96d56Sopenharmony_ci It inherits :class:`IOBase`. 5247db96d56Sopenharmony_ci 5257db96d56Sopenharmony_ci The main difference with :class:`RawIOBase` is that methods :meth:`read`, 5267db96d56Sopenharmony_ci :meth:`readinto` and :meth:`write` will try (respectively) to read as much 5277db96d56Sopenharmony_ci input as requested or to consume all given output, at the expense of 5287db96d56Sopenharmony_ci making perhaps more than one system call. 5297db96d56Sopenharmony_ci 5307db96d56Sopenharmony_ci In addition, those methods can raise :exc:`BlockingIOError` if the 5317db96d56Sopenharmony_ci underlying raw stream is in non-blocking mode and cannot take or give 5327db96d56Sopenharmony_ci enough data; unlike their :class:`RawIOBase` counterparts, they will 5337db96d56Sopenharmony_ci never return ``None``. 5347db96d56Sopenharmony_ci 5357db96d56Sopenharmony_ci Besides, the :meth:`read` method does not have a default 5367db96d56Sopenharmony_ci implementation that defers to :meth:`readinto`. 5377db96d56Sopenharmony_ci 5387db96d56Sopenharmony_ci A typical :class:`BufferedIOBase` implementation should not inherit from a 5397db96d56Sopenharmony_ci :class:`RawIOBase` implementation, but wrap one, like 5407db96d56Sopenharmony_ci :class:`BufferedWriter` and :class:`BufferedReader` do. 5417db96d56Sopenharmony_ci 5427db96d56Sopenharmony_ci :class:`BufferedIOBase` provides or overrides these data attributes and 5437db96d56Sopenharmony_ci methods in addition to those from :class:`IOBase`: 5447db96d56Sopenharmony_ci 5457db96d56Sopenharmony_ci .. attribute:: raw 5467db96d56Sopenharmony_ci 5477db96d56Sopenharmony_ci The underlying raw stream (a :class:`RawIOBase` instance) that 5487db96d56Sopenharmony_ci :class:`BufferedIOBase` deals with. This is not part of the 5497db96d56Sopenharmony_ci :class:`BufferedIOBase` API and may not exist on some implementations. 5507db96d56Sopenharmony_ci 5517db96d56Sopenharmony_ci .. method:: detach() 5527db96d56Sopenharmony_ci 5537db96d56Sopenharmony_ci Separate the underlying raw stream from the buffer and return it. 5547db96d56Sopenharmony_ci 5557db96d56Sopenharmony_ci After the raw stream has been detached, the buffer is in an unusable 5567db96d56Sopenharmony_ci state. 5577db96d56Sopenharmony_ci 5587db96d56Sopenharmony_ci Some buffers, like :class:`BytesIO`, do not have the concept of a single 5597db96d56Sopenharmony_ci raw stream to return from this method. They raise 5607db96d56Sopenharmony_ci :exc:`UnsupportedOperation`. 5617db96d56Sopenharmony_ci 5627db96d56Sopenharmony_ci .. versionadded:: 3.1 5637db96d56Sopenharmony_ci 5647db96d56Sopenharmony_ci .. method:: read(size=-1, /) 5657db96d56Sopenharmony_ci 5667db96d56Sopenharmony_ci Read and return up to *size* bytes. If the argument is omitted, ``None``, 5677db96d56Sopenharmony_ci or negative, data is read and returned until EOF is reached. An empty 5687db96d56Sopenharmony_ci :class:`bytes` object is returned if the stream is already at EOF. 5697db96d56Sopenharmony_ci 5707db96d56Sopenharmony_ci If the argument is positive, and the underlying raw stream is not 5717db96d56Sopenharmony_ci interactive, multiple raw reads may be issued to satisfy the byte count 5727db96d56Sopenharmony_ci (unless EOF is reached first). But for interactive raw streams, at most 5737db96d56Sopenharmony_ci one raw read will be issued, and a short result does not imply that EOF is 5747db96d56Sopenharmony_ci imminent. 5757db96d56Sopenharmony_ci 5767db96d56Sopenharmony_ci A :exc:`BlockingIOError` is raised if the underlying raw stream is in 5777db96d56Sopenharmony_ci non blocking-mode, and has no data available at the moment. 5787db96d56Sopenharmony_ci 5797db96d56Sopenharmony_ci .. method:: read1(size=-1, /) 5807db96d56Sopenharmony_ci 5817db96d56Sopenharmony_ci Read and return up to *size* bytes, with at most one call to the 5827db96d56Sopenharmony_ci underlying raw stream's :meth:`~RawIOBase.read` (or 5837db96d56Sopenharmony_ci :meth:`~RawIOBase.readinto`) method. This can be useful if you are 5847db96d56Sopenharmony_ci implementing your own buffering on top of a :class:`BufferedIOBase` 5857db96d56Sopenharmony_ci object. 5867db96d56Sopenharmony_ci 5877db96d56Sopenharmony_ci If *size* is ``-1`` (the default), an arbitrary number of bytes are 5887db96d56Sopenharmony_ci returned (more than zero unless EOF is reached). 5897db96d56Sopenharmony_ci 5907db96d56Sopenharmony_ci .. method:: readinto(b, /) 5917db96d56Sopenharmony_ci 5927db96d56Sopenharmony_ci Read bytes into a pre-allocated, writable 5937db96d56Sopenharmony_ci :term:`bytes-like object` *b* and return the number of bytes read. 5947db96d56Sopenharmony_ci For example, *b* might be a :class:`bytearray`. 5957db96d56Sopenharmony_ci 5967db96d56Sopenharmony_ci Like :meth:`read`, multiple reads may be issued to the underlying raw 5977db96d56Sopenharmony_ci stream, unless the latter is interactive. 5987db96d56Sopenharmony_ci 5997db96d56Sopenharmony_ci A :exc:`BlockingIOError` is raised if the underlying raw stream is in non 6007db96d56Sopenharmony_ci blocking-mode, and has no data available at the moment. 6017db96d56Sopenharmony_ci 6027db96d56Sopenharmony_ci .. method:: readinto1(b, /) 6037db96d56Sopenharmony_ci 6047db96d56Sopenharmony_ci Read bytes into a pre-allocated, writable 6057db96d56Sopenharmony_ci :term:`bytes-like object` *b*, using at most one call to 6067db96d56Sopenharmony_ci the underlying raw stream's :meth:`~RawIOBase.read` (or 6077db96d56Sopenharmony_ci :meth:`~RawIOBase.readinto`) method. Return the number of bytes read. 6087db96d56Sopenharmony_ci 6097db96d56Sopenharmony_ci A :exc:`BlockingIOError` is raised if the underlying raw stream is in non 6107db96d56Sopenharmony_ci blocking-mode, and has no data available at the moment. 6117db96d56Sopenharmony_ci 6127db96d56Sopenharmony_ci .. versionadded:: 3.5 6137db96d56Sopenharmony_ci 6147db96d56Sopenharmony_ci .. method:: write(b, /) 6157db96d56Sopenharmony_ci 6167db96d56Sopenharmony_ci Write the given :term:`bytes-like object`, *b*, and return the number 6177db96d56Sopenharmony_ci of bytes written (always equal to the length of *b* in bytes, since if 6187db96d56Sopenharmony_ci the write fails an :exc:`OSError` will be raised). Depending on the 6197db96d56Sopenharmony_ci actual implementation, these bytes may be readily written to the 6207db96d56Sopenharmony_ci underlying stream, or held in a buffer for performance and latency 6217db96d56Sopenharmony_ci reasons. 6227db96d56Sopenharmony_ci 6237db96d56Sopenharmony_ci When in non-blocking mode, a :exc:`BlockingIOError` is raised if the 6247db96d56Sopenharmony_ci data needed to be written to the raw stream but it couldn't accept 6257db96d56Sopenharmony_ci all the data without blocking. 6267db96d56Sopenharmony_ci 6277db96d56Sopenharmony_ci The caller may release or mutate *b* after this method returns, 6287db96d56Sopenharmony_ci so the implementation should only access *b* during the method call. 6297db96d56Sopenharmony_ci 6307db96d56Sopenharmony_ci 6317db96d56Sopenharmony_ciRaw File I/O 6327db96d56Sopenharmony_ci^^^^^^^^^^^^ 6337db96d56Sopenharmony_ci 6347db96d56Sopenharmony_ci.. class:: FileIO(name, mode='r', closefd=True, opener=None) 6357db96d56Sopenharmony_ci 6367db96d56Sopenharmony_ci A raw binary stream representing an OS-level file containing bytes data. It 6377db96d56Sopenharmony_ci inherits :class:`RawIOBase`. 6387db96d56Sopenharmony_ci 6397db96d56Sopenharmony_ci The *name* can be one of two things: 6407db96d56Sopenharmony_ci 6417db96d56Sopenharmony_ci * a character string or :class:`bytes` object representing the path to the 6427db96d56Sopenharmony_ci file which will be opened. In this case closefd must be ``True`` (the default) 6437db96d56Sopenharmony_ci otherwise an error will be raised. 6447db96d56Sopenharmony_ci * an integer representing the number of an existing OS-level file descriptor 6457db96d56Sopenharmony_ci to which the resulting :class:`FileIO` object will give access. When the 6467db96d56Sopenharmony_ci FileIO object is closed this fd will be closed as well, unless *closefd* 6477db96d56Sopenharmony_ci is set to ``False``. 6487db96d56Sopenharmony_ci 6497db96d56Sopenharmony_ci The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading 6507db96d56Sopenharmony_ci (default), writing, exclusive creation or appending. The file will be 6517db96d56Sopenharmony_ci created if it doesn't exist when opened for writing or appending; it will be 6527db96d56Sopenharmony_ci truncated when opened for writing. :exc:`FileExistsError` will be raised if 6537db96d56Sopenharmony_ci it already exists when opened for creating. Opening a file for creating 6547db96d56Sopenharmony_ci implies writing, so this mode behaves in a similar way to ``'w'``. Add a 6557db96d56Sopenharmony_ci ``'+'`` to the mode to allow simultaneous reading and writing. 6567db96d56Sopenharmony_ci 6577db96d56Sopenharmony_ci The :meth:`read` (when called with a positive argument), :meth:`readinto` 6587db96d56Sopenharmony_ci and :meth:`write` methods on this class will only make one system call. 6597db96d56Sopenharmony_ci 6607db96d56Sopenharmony_ci A custom opener can be used by passing a callable as *opener*. The underlying 6617db96d56Sopenharmony_ci file descriptor for the file object is then obtained by calling *opener* with 6627db96d56Sopenharmony_ci (*name*, *flags*). *opener* must return an open file descriptor (passing 6637db96d56Sopenharmony_ci :mod:`os.open` as *opener* results in functionality similar to passing 6647db96d56Sopenharmony_ci ``None``). 6657db96d56Sopenharmony_ci 6667db96d56Sopenharmony_ci The newly created file is :ref:`non-inheritable <fd_inheritance>`. 6677db96d56Sopenharmony_ci 6687db96d56Sopenharmony_ci See the :func:`open` built-in function for examples on using the *opener* 6697db96d56Sopenharmony_ci parameter. 6707db96d56Sopenharmony_ci 6717db96d56Sopenharmony_ci .. versionchanged:: 3.3 6727db96d56Sopenharmony_ci The *opener* parameter was added. 6737db96d56Sopenharmony_ci The ``'x'`` mode was added. 6747db96d56Sopenharmony_ci 6757db96d56Sopenharmony_ci .. versionchanged:: 3.4 6767db96d56Sopenharmony_ci The file is now non-inheritable. 6777db96d56Sopenharmony_ci 6787db96d56Sopenharmony_ci :class:`FileIO` provides these data attributes in addition to those from 6797db96d56Sopenharmony_ci :class:`RawIOBase` and :class:`IOBase`: 6807db96d56Sopenharmony_ci 6817db96d56Sopenharmony_ci .. attribute:: mode 6827db96d56Sopenharmony_ci 6837db96d56Sopenharmony_ci The mode as given in the constructor. 6847db96d56Sopenharmony_ci 6857db96d56Sopenharmony_ci .. attribute:: name 6867db96d56Sopenharmony_ci 6877db96d56Sopenharmony_ci The file name. This is the file descriptor of the file when no name is 6887db96d56Sopenharmony_ci given in the constructor. 6897db96d56Sopenharmony_ci 6907db96d56Sopenharmony_ci 6917db96d56Sopenharmony_ciBuffered Streams 6927db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^ 6937db96d56Sopenharmony_ci 6947db96d56Sopenharmony_ciBuffered I/O streams provide a higher-level interface to an I/O device 6957db96d56Sopenharmony_cithan raw I/O does. 6967db96d56Sopenharmony_ci 6977db96d56Sopenharmony_ci.. class:: BytesIO(initial_bytes=b'') 6987db96d56Sopenharmony_ci 6997db96d56Sopenharmony_ci A binary stream using an in-memory bytes buffer. It inherits 7007db96d56Sopenharmony_ci :class:`BufferedIOBase`. The buffer is discarded when the 7017db96d56Sopenharmony_ci :meth:`~IOBase.close` method is called. 7027db96d56Sopenharmony_ci 7037db96d56Sopenharmony_ci The optional argument *initial_bytes* is a :term:`bytes-like object` that 7047db96d56Sopenharmony_ci contains initial data. 7057db96d56Sopenharmony_ci 7067db96d56Sopenharmony_ci :class:`BytesIO` provides or overrides these methods in addition to those 7077db96d56Sopenharmony_ci from :class:`BufferedIOBase` and :class:`IOBase`: 7087db96d56Sopenharmony_ci 7097db96d56Sopenharmony_ci .. method:: getbuffer() 7107db96d56Sopenharmony_ci 7117db96d56Sopenharmony_ci Return a readable and writable view over the contents of the buffer 7127db96d56Sopenharmony_ci without copying them. Also, mutating the view will transparently 7137db96d56Sopenharmony_ci update the contents of the buffer:: 7147db96d56Sopenharmony_ci 7157db96d56Sopenharmony_ci >>> b = io.BytesIO(b"abcdef") 7167db96d56Sopenharmony_ci >>> view = b.getbuffer() 7177db96d56Sopenharmony_ci >>> view[2:4] = b"56" 7187db96d56Sopenharmony_ci >>> b.getvalue() 7197db96d56Sopenharmony_ci b'ab56ef' 7207db96d56Sopenharmony_ci 7217db96d56Sopenharmony_ci .. note:: 7227db96d56Sopenharmony_ci As long as the view exists, the :class:`BytesIO` object cannot be 7237db96d56Sopenharmony_ci resized or closed. 7247db96d56Sopenharmony_ci 7257db96d56Sopenharmony_ci .. versionadded:: 3.2 7267db96d56Sopenharmony_ci 7277db96d56Sopenharmony_ci .. method:: getvalue() 7287db96d56Sopenharmony_ci 7297db96d56Sopenharmony_ci Return :class:`bytes` containing the entire contents of the buffer. 7307db96d56Sopenharmony_ci 7317db96d56Sopenharmony_ci 7327db96d56Sopenharmony_ci .. method:: read1(size=-1, /) 7337db96d56Sopenharmony_ci 7347db96d56Sopenharmony_ci In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.read`. 7357db96d56Sopenharmony_ci 7367db96d56Sopenharmony_ci .. versionchanged:: 3.7 7377db96d56Sopenharmony_ci The *size* argument is now optional. 7387db96d56Sopenharmony_ci 7397db96d56Sopenharmony_ci .. method:: readinto1(b, /) 7407db96d56Sopenharmony_ci 7417db96d56Sopenharmony_ci In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.readinto`. 7427db96d56Sopenharmony_ci 7437db96d56Sopenharmony_ci .. versionadded:: 3.5 7447db96d56Sopenharmony_ci 7457db96d56Sopenharmony_ci.. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE) 7467db96d56Sopenharmony_ci 7477db96d56Sopenharmony_ci A buffered binary stream providing higher-level access to a readable, non 7487db96d56Sopenharmony_ci seekable :class:`RawIOBase` raw binary stream. It inherits 7497db96d56Sopenharmony_ci :class:`BufferedIOBase`. 7507db96d56Sopenharmony_ci 7517db96d56Sopenharmony_ci When reading data from this object, a larger amount of data may be 7527db96d56Sopenharmony_ci requested from the underlying raw stream, and kept in an internal buffer. 7537db96d56Sopenharmony_ci The buffered data can then be returned directly on subsequent reads. 7547db96d56Sopenharmony_ci 7557db96d56Sopenharmony_ci The constructor creates a :class:`BufferedReader` for the given readable 7567db96d56Sopenharmony_ci *raw* stream and *buffer_size*. If *buffer_size* is omitted, 7577db96d56Sopenharmony_ci :data:`DEFAULT_BUFFER_SIZE` is used. 7587db96d56Sopenharmony_ci 7597db96d56Sopenharmony_ci :class:`BufferedReader` provides or overrides these methods in addition to 7607db96d56Sopenharmony_ci those from :class:`BufferedIOBase` and :class:`IOBase`: 7617db96d56Sopenharmony_ci 7627db96d56Sopenharmony_ci .. method:: peek(size=0, /) 7637db96d56Sopenharmony_ci 7647db96d56Sopenharmony_ci Return bytes from the stream without advancing the position. At most one 7657db96d56Sopenharmony_ci single read on the raw stream is done to satisfy the call. The number of 7667db96d56Sopenharmony_ci bytes returned may be less or more than requested. 7677db96d56Sopenharmony_ci 7687db96d56Sopenharmony_ci .. method:: read(size=-1, /) 7697db96d56Sopenharmony_ci 7707db96d56Sopenharmony_ci Read and return *size* bytes, or if *size* is not given or negative, until 7717db96d56Sopenharmony_ci EOF or if the read call would block in non-blocking mode. 7727db96d56Sopenharmony_ci 7737db96d56Sopenharmony_ci .. method:: read1(size=-1, /) 7747db96d56Sopenharmony_ci 7757db96d56Sopenharmony_ci Read and return up to *size* bytes with only one call on the raw stream. 7767db96d56Sopenharmony_ci If at least one byte is buffered, only buffered bytes are returned. 7777db96d56Sopenharmony_ci Otherwise, one raw stream read call is made. 7787db96d56Sopenharmony_ci 7797db96d56Sopenharmony_ci .. versionchanged:: 3.7 7807db96d56Sopenharmony_ci The *size* argument is now optional. 7817db96d56Sopenharmony_ci 7827db96d56Sopenharmony_ci 7837db96d56Sopenharmony_ci.. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE) 7847db96d56Sopenharmony_ci 7857db96d56Sopenharmony_ci A buffered binary stream providing higher-level access to a writeable, non 7867db96d56Sopenharmony_ci seekable :class:`RawIOBase` raw binary stream. It inherits 7877db96d56Sopenharmony_ci :class:`BufferedIOBase`. 7887db96d56Sopenharmony_ci 7897db96d56Sopenharmony_ci When writing to this object, data is normally placed into an internal 7907db96d56Sopenharmony_ci buffer. The buffer will be written out to the underlying :class:`RawIOBase` 7917db96d56Sopenharmony_ci object under various conditions, including: 7927db96d56Sopenharmony_ci 7937db96d56Sopenharmony_ci * when the buffer gets too small for all pending data; 7947db96d56Sopenharmony_ci * when :meth:`flush()` is called; 7957db96d56Sopenharmony_ci * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects); 7967db96d56Sopenharmony_ci * when the :class:`BufferedWriter` object is closed or destroyed. 7977db96d56Sopenharmony_ci 7987db96d56Sopenharmony_ci The constructor creates a :class:`BufferedWriter` for the given writeable 7997db96d56Sopenharmony_ci *raw* stream. If the *buffer_size* is not given, it defaults to 8007db96d56Sopenharmony_ci :data:`DEFAULT_BUFFER_SIZE`. 8017db96d56Sopenharmony_ci 8027db96d56Sopenharmony_ci :class:`BufferedWriter` provides or overrides these methods in addition to 8037db96d56Sopenharmony_ci those from :class:`BufferedIOBase` and :class:`IOBase`: 8047db96d56Sopenharmony_ci 8057db96d56Sopenharmony_ci .. method:: flush() 8067db96d56Sopenharmony_ci 8077db96d56Sopenharmony_ci Force bytes held in the buffer into the raw stream. A 8087db96d56Sopenharmony_ci :exc:`BlockingIOError` should be raised if the raw stream blocks. 8097db96d56Sopenharmony_ci 8107db96d56Sopenharmony_ci .. method:: write(b, /) 8117db96d56Sopenharmony_ci 8127db96d56Sopenharmony_ci Write the :term:`bytes-like object`, *b*, and return the 8137db96d56Sopenharmony_ci number of bytes written. When in non-blocking mode, a 8147db96d56Sopenharmony_ci :exc:`BlockingIOError` is raised if the buffer needs to be written out but 8157db96d56Sopenharmony_ci the raw stream blocks. 8167db96d56Sopenharmony_ci 8177db96d56Sopenharmony_ci 8187db96d56Sopenharmony_ci.. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE) 8197db96d56Sopenharmony_ci 8207db96d56Sopenharmony_ci A buffered binary stream providing higher-level access to a seekable 8217db96d56Sopenharmony_ci :class:`RawIOBase` raw binary stream. It inherits :class:`BufferedReader` 8227db96d56Sopenharmony_ci and :class:`BufferedWriter`. 8237db96d56Sopenharmony_ci 8247db96d56Sopenharmony_ci The constructor creates a reader and writer for a seekable raw stream, given 8257db96d56Sopenharmony_ci in the first argument. If the *buffer_size* is omitted it defaults to 8267db96d56Sopenharmony_ci :data:`DEFAULT_BUFFER_SIZE`. 8277db96d56Sopenharmony_ci 8287db96d56Sopenharmony_ci :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or 8297db96d56Sopenharmony_ci :class:`BufferedWriter` can do. In addition, :meth:`seek` and :meth:`tell` 8307db96d56Sopenharmony_ci are guaranteed to be implemented. 8317db96d56Sopenharmony_ci 8327db96d56Sopenharmony_ci 8337db96d56Sopenharmony_ci.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE, /) 8347db96d56Sopenharmony_ci 8357db96d56Sopenharmony_ci A buffered binary stream providing higher-level access to two non seekable 8367db96d56Sopenharmony_ci :class:`RawIOBase` raw binary streams---one readable, the other writeable. 8377db96d56Sopenharmony_ci It inherits :class:`BufferedIOBase`. 8387db96d56Sopenharmony_ci 8397db96d56Sopenharmony_ci *reader* and *writer* are :class:`RawIOBase` objects that are readable and 8407db96d56Sopenharmony_ci writeable respectively. If the *buffer_size* is omitted it defaults to 8417db96d56Sopenharmony_ci :data:`DEFAULT_BUFFER_SIZE`. 8427db96d56Sopenharmony_ci 8437db96d56Sopenharmony_ci :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods 8447db96d56Sopenharmony_ci except for :meth:`~BufferedIOBase.detach`, which raises 8457db96d56Sopenharmony_ci :exc:`UnsupportedOperation`. 8467db96d56Sopenharmony_ci 8477db96d56Sopenharmony_ci .. warning:: 8487db96d56Sopenharmony_ci 8497db96d56Sopenharmony_ci :class:`BufferedRWPair` does not attempt to synchronize accesses to 8507db96d56Sopenharmony_ci its underlying raw streams. You should not pass it the same object 8517db96d56Sopenharmony_ci as reader and writer; use :class:`BufferedRandom` instead. 8527db96d56Sopenharmony_ci 8537db96d56Sopenharmony_ci 8547db96d56Sopenharmony_ciText I/O 8557db96d56Sopenharmony_ci^^^^^^^^ 8567db96d56Sopenharmony_ci 8577db96d56Sopenharmony_ci.. class:: TextIOBase 8587db96d56Sopenharmony_ci 8597db96d56Sopenharmony_ci Base class for text streams. This class provides a character and line based 8607db96d56Sopenharmony_ci interface to stream I/O. It inherits :class:`IOBase`. 8617db96d56Sopenharmony_ci 8627db96d56Sopenharmony_ci :class:`TextIOBase` provides or overrides these data attributes and 8637db96d56Sopenharmony_ci methods in addition to those from :class:`IOBase`: 8647db96d56Sopenharmony_ci 8657db96d56Sopenharmony_ci .. attribute:: encoding 8667db96d56Sopenharmony_ci 8677db96d56Sopenharmony_ci The name of the encoding used to decode the stream's bytes into 8687db96d56Sopenharmony_ci strings, and to encode strings into bytes. 8697db96d56Sopenharmony_ci 8707db96d56Sopenharmony_ci .. attribute:: errors 8717db96d56Sopenharmony_ci 8727db96d56Sopenharmony_ci The error setting of the decoder or encoder. 8737db96d56Sopenharmony_ci 8747db96d56Sopenharmony_ci .. attribute:: newlines 8757db96d56Sopenharmony_ci 8767db96d56Sopenharmony_ci A string, a tuple of strings, or ``None``, indicating the newlines 8777db96d56Sopenharmony_ci translated so far. Depending on the implementation and the initial 8787db96d56Sopenharmony_ci constructor flags, this may not be available. 8797db96d56Sopenharmony_ci 8807db96d56Sopenharmony_ci .. attribute:: buffer 8817db96d56Sopenharmony_ci 8827db96d56Sopenharmony_ci The underlying binary buffer (a :class:`BufferedIOBase` instance) that 8837db96d56Sopenharmony_ci :class:`TextIOBase` deals with. This is not part of the 8847db96d56Sopenharmony_ci :class:`TextIOBase` API and may not exist in some implementations. 8857db96d56Sopenharmony_ci 8867db96d56Sopenharmony_ci .. method:: detach() 8877db96d56Sopenharmony_ci 8887db96d56Sopenharmony_ci Separate the underlying binary buffer from the :class:`TextIOBase` and 8897db96d56Sopenharmony_ci return it. 8907db96d56Sopenharmony_ci 8917db96d56Sopenharmony_ci After the underlying buffer has been detached, the :class:`TextIOBase` is 8927db96d56Sopenharmony_ci in an unusable state. 8937db96d56Sopenharmony_ci 8947db96d56Sopenharmony_ci Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not 8957db96d56Sopenharmony_ci have the concept of an underlying buffer and calling this method will 8967db96d56Sopenharmony_ci raise :exc:`UnsupportedOperation`. 8977db96d56Sopenharmony_ci 8987db96d56Sopenharmony_ci .. versionadded:: 3.1 8997db96d56Sopenharmony_ci 9007db96d56Sopenharmony_ci .. method:: read(size=-1, /) 9017db96d56Sopenharmony_ci 9027db96d56Sopenharmony_ci Read and return at most *size* characters from the stream as a single 9037db96d56Sopenharmony_ci :class:`str`. If *size* is negative or ``None``, reads until EOF. 9047db96d56Sopenharmony_ci 9057db96d56Sopenharmony_ci .. method:: readline(size=-1, /) 9067db96d56Sopenharmony_ci 9077db96d56Sopenharmony_ci Read until newline or EOF and return a single ``str``. If the stream is 9087db96d56Sopenharmony_ci already at EOF, an empty string is returned. 9097db96d56Sopenharmony_ci 9107db96d56Sopenharmony_ci If *size* is specified, at most *size* characters will be read. 9117db96d56Sopenharmony_ci 9127db96d56Sopenharmony_ci .. method:: seek(offset, whence=SEEK_SET, /) 9137db96d56Sopenharmony_ci 9147db96d56Sopenharmony_ci Change the stream position to the given *offset*. Behaviour depends on 9157db96d56Sopenharmony_ci the *whence* parameter. The default value for *whence* is 9167db96d56Sopenharmony_ci :data:`SEEK_SET`. 9177db96d56Sopenharmony_ci 9187db96d56Sopenharmony_ci * :data:`SEEK_SET` or ``0``: seek from the start of the stream 9197db96d56Sopenharmony_ci (the default); *offset* must either be a number returned by 9207db96d56Sopenharmony_ci :meth:`TextIOBase.tell`, or zero. Any other *offset* value 9217db96d56Sopenharmony_ci produces undefined behaviour. 9227db96d56Sopenharmony_ci * :data:`SEEK_CUR` or ``1``: "seek" to the current position; 9237db96d56Sopenharmony_ci *offset* must be zero, which is a no-operation (all other values 9247db96d56Sopenharmony_ci are unsupported). 9257db96d56Sopenharmony_ci * :data:`SEEK_END` or ``2``: seek to the end of the stream; 9267db96d56Sopenharmony_ci *offset* must be zero (all other values are unsupported). 9277db96d56Sopenharmony_ci 9287db96d56Sopenharmony_ci Return the new absolute position as an opaque number. 9297db96d56Sopenharmony_ci 9307db96d56Sopenharmony_ci .. versionadded:: 3.1 9317db96d56Sopenharmony_ci The ``SEEK_*`` constants. 9327db96d56Sopenharmony_ci 9337db96d56Sopenharmony_ci .. method:: tell() 9347db96d56Sopenharmony_ci 9357db96d56Sopenharmony_ci Return the current stream position as an opaque number. The number 9367db96d56Sopenharmony_ci does not usually represent a number of bytes in the underlying 9377db96d56Sopenharmony_ci binary storage. 9387db96d56Sopenharmony_ci 9397db96d56Sopenharmony_ci .. method:: write(s, /) 9407db96d56Sopenharmony_ci 9417db96d56Sopenharmony_ci Write the string *s* to the stream and return the number of characters 9427db96d56Sopenharmony_ci written. 9437db96d56Sopenharmony_ci 9447db96d56Sopenharmony_ci 9457db96d56Sopenharmony_ci.. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, \ 9467db96d56Sopenharmony_ci line_buffering=False, write_through=False) 9477db96d56Sopenharmony_ci 9487db96d56Sopenharmony_ci A buffered text stream providing higher-level access to a 9497db96d56Sopenharmony_ci :class:`BufferedIOBase` buffered binary stream. It inherits 9507db96d56Sopenharmony_ci :class:`TextIOBase`. 9517db96d56Sopenharmony_ci 9527db96d56Sopenharmony_ci *encoding* gives the name of the encoding that the stream will be decoded or 9537db96d56Sopenharmony_ci encoded with. It defaults to :func:`locale.getencoding()`. 9547db96d56Sopenharmony_ci ``encoding="locale"`` can be used to specify the current locale's encoding 9557db96d56Sopenharmony_ci explicitly. See :ref:`io-text-encoding` for more information. 9567db96d56Sopenharmony_ci 9577db96d56Sopenharmony_ci *errors* is an optional string that specifies how encoding and decoding 9587db96d56Sopenharmony_ci errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError` 9597db96d56Sopenharmony_ci exception if there is an encoding error (the default of ``None`` has the same 9607db96d56Sopenharmony_ci effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding 9617db96d56Sopenharmony_ci errors can lead to data loss.) ``'replace'`` causes a replacement marker 9627db96d56Sopenharmony_ci (such as ``'?'``) to be inserted where there is malformed data. 9637db96d56Sopenharmony_ci ``'backslashreplace'`` causes malformed data to be replaced by a 9647db96d56Sopenharmony_ci backslashed escape sequence. When writing, ``'xmlcharrefreplace'`` 9657db96d56Sopenharmony_ci (replace with the appropriate XML character reference) or ``'namereplace'`` 9667db96d56Sopenharmony_ci (replace with ``\N{...}`` escape sequences) can be used. Any other error 9677db96d56Sopenharmony_ci handling name that has been registered with 9687db96d56Sopenharmony_ci :func:`codecs.register_error` is also valid. 9697db96d56Sopenharmony_ci 9707db96d56Sopenharmony_ci .. index:: 9717db96d56Sopenharmony_ci single: universal newlines; io.TextIOWrapper class 9727db96d56Sopenharmony_ci 9737db96d56Sopenharmony_ci *newline* controls how line endings are handled. It can be ``None``, 9747db96d56Sopenharmony_ci ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows: 9757db96d56Sopenharmony_ci 9767db96d56Sopenharmony_ci * When reading input from the stream, if *newline* is ``None``, 9777db96d56Sopenharmony_ci :term:`universal newlines` mode is enabled. Lines in the input can end in 9787db96d56Sopenharmony_ci ``'\n'``, ``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'`` 9797db96d56Sopenharmony_ci before being returned to the caller. If *newline* is ``''``, universal 9807db96d56Sopenharmony_ci newlines mode is enabled, but line endings are returned to the caller 9817db96d56Sopenharmony_ci untranslated. If *newline* has any of the other legal values, input lines 9827db96d56Sopenharmony_ci are only terminated by the given string, and the line ending is returned to 9837db96d56Sopenharmony_ci the caller untranslated. 9847db96d56Sopenharmony_ci 9857db96d56Sopenharmony_ci * When writing output to the stream, if *newline* is ``None``, any ``'\n'`` 9867db96d56Sopenharmony_ci characters written are translated to the system default line separator, 9877db96d56Sopenharmony_ci :data:`os.linesep`. If *newline* is ``''`` or ``'\n'``, no translation 9887db96d56Sopenharmony_ci takes place. If *newline* is any of the other legal values, any ``'\n'`` 9897db96d56Sopenharmony_ci characters written are translated to the given string. 9907db96d56Sopenharmony_ci 9917db96d56Sopenharmony_ci If *line_buffering* is ``True``, :meth:`flush` is implied when a call to 9927db96d56Sopenharmony_ci write contains a newline character or a carriage return. 9937db96d56Sopenharmony_ci 9947db96d56Sopenharmony_ci If *write_through* is ``True``, calls to :meth:`write` are guaranteed 9957db96d56Sopenharmony_ci not to be buffered: any data written on the :class:`TextIOWrapper` 9967db96d56Sopenharmony_ci object is immediately handled to its underlying binary *buffer*. 9977db96d56Sopenharmony_ci 9987db96d56Sopenharmony_ci .. versionchanged:: 3.3 9997db96d56Sopenharmony_ci The *write_through* argument has been added. 10007db96d56Sopenharmony_ci 10017db96d56Sopenharmony_ci .. versionchanged:: 3.3 10027db96d56Sopenharmony_ci The default *encoding* is now ``locale.getpreferredencoding(False)`` 10037db96d56Sopenharmony_ci instead of ``locale.getpreferredencoding()``. Don't change temporary the 10047db96d56Sopenharmony_ci locale encoding using :func:`locale.setlocale`, use the current locale 10057db96d56Sopenharmony_ci encoding instead of the user preferred encoding. 10067db96d56Sopenharmony_ci 10077db96d56Sopenharmony_ci .. versionchanged:: 3.10 10087db96d56Sopenharmony_ci The *encoding* argument now supports the ``"locale"`` dummy encoding name. 10097db96d56Sopenharmony_ci 10107db96d56Sopenharmony_ci :class:`TextIOWrapper` provides these data attributes and methods in 10117db96d56Sopenharmony_ci addition to those from :class:`TextIOBase` and :class:`IOBase`: 10127db96d56Sopenharmony_ci 10137db96d56Sopenharmony_ci .. attribute:: line_buffering 10147db96d56Sopenharmony_ci 10157db96d56Sopenharmony_ci Whether line buffering is enabled. 10167db96d56Sopenharmony_ci 10177db96d56Sopenharmony_ci .. attribute:: write_through 10187db96d56Sopenharmony_ci 10197db96d56Sopenharmony_ci Whether writes are passed immediately to the underlying binary 10207db96d56Sopenharmony_ci buffer. 10217db96d56Sopenharmony_ci 10227db96d56Sopenharmony_ci .. versionadded:: 3.7 10237db96d56Sopenharmony_ci 10247db96d56Sopenharmony_ci .. method:: reconfigure(*, encoding=None, errors=None, newline=None, \ 10257db96d56Sopenharmony_ci line_buffering=None, write_through=None) 10267db96d56Sopenharmony_ci 10277db96d56Sopenharmony_ci Reconfigure this text stream using new settings for *encoding*, 10287db96d56Sopenharmony_ci *errors*, *newline*, *line_buffering* and *write_through*. 10297db96d56Sopenharmony_ci 10307db96d56Sopenharmony_ci Parameters not specified keep current settings, except 10317db96d56Sopenharmony_ci ``errors='strict'`` is used when *encoding* is specified but 10327db96d56Sopenharmony_ci *errors* is not specified. 10337db96d56Sopenharmony_ci 10347db96d56Sopenharmony_ci It is not possible to change the encoding or newline if some data 10357db96d56Sopenharmony_ci has already been read from the stream. On the other hand, changing 10367db96d56Sopenharmony_ci encoding after write is possible. 10377db96d56Sopenharmony_ci 10387db96d56Sopenharmony_ci This method does an implicit stream flush before setting the 10397db96d56Sopenharmony_ci new parameters. 10407db96d56Sopenharmony_ci 10417db96d56Sopenharmony_ci .. versionadded:: 3.7 10427db96d56Sopenharmony_ci 10437db96d56Sopenharmony_ci .. versionchanged:: 3.11 10447db96d56Sopenharmony_ci The method supports ``encoding="locale"`` option. 10457db96d56Sopenharmony_ci 10467db96d56Sopenharmony_ci 10477db96d56Sopenharmony_ci.. class:: StringIO(initial_value='', newline='\n') 10487db96d56Sopenharmony_ci 10497db96d56Sopenharmony_ci A text stream using an in-memory text buffer. It inherits 10507db96d56Sopenharmony_ci :class:`TextIOBase`. 10517db96d56Sopenharmony_ci 10527db96d56Sopenharmony_ci The text buffer is discarded when the :meth:`~IOBase.close` method is 10537db96d56Sopenharmony_ci called. 10547db96d56Sopenharmony_ci 10557db96d56Sopenharmony_ci The initial value of the buffer can be set by providing *initial_value*. 10567db96d56Sopenharmony_ci If newline translation is enabled, newlines will be encoded as if by 10577db96d56Sopenharmony_ci :meth:`~TextIOBase.write`. The stream is positioned at the start of the 10587db96d56Sopenharmony_ci buffer which emulates opening an existing file in a ``w+`` mode, making it 10597db96d56Sopenharmony_ci ready for an immediate write from the beginning or for a write that 10607db96d56Sopenharmony_ci would overwrite the initial value. To emulate opening a file in an ``a+`` 10617db96d56Sopenharmony_ci mode ready for appending, use ``f.seek(0, io.SEEK_END)`` to reposition the 10627db96d56Sopenharmony_ci stream at the end of the buffer. 10637db96d56Sopenharmony_ci 10647db96d56Sopenharmony_ci The *newline* argument works like that of :class:`TextIOWrapper`, 10657db96d56Sopenharmony_ci except that when writing output to the stream, if *newline* is ``None``, 10667db96d56Sopenharmony_ci newlines are written as ``\n`` on all platforms. 10677db96d56Sopenharmony_ci 10687db96d56Sopenharmony_ci :class:`StringIO` provides this method in addition to those from 10697db96d56Sopenharmony_ci :class:`TextIOBase` and :class:`IOBase`: 10707db96d56Sopenharmony_ci 10717db96d56Sopenharmony_ci .. method:: getvalue() 10727db96d56Sopenharmony_ci 10737db96d56Sopenharmony_ci Return a ``str`` containing the entire contents of the buffer. 10747db96d56Sopenharmony_ci Newlines are decoded as if by :meth:`~TextIOBase.read`, although 10757db96d56Sopenharmony_ci the stream position is not changed. 10767db96d56Sopenharmony_ci 10777db96d56Sopenharmony_ci Example usage:: 10787db96d56Sopenharmony_ci 10797db96d56Sopenharmony_ci import io 10807db96d56Sopenharmony_ci 10817db96d56Sopenharmony_ci output = io.StringIO() 10827db96d56Sopenharmony_ci output.write('First line.\n') 10837db96d56Sopenharmony_ci print('Second line.', file=output) 10847db96d56Sopenharmony_ci 10857db96d56Sopenharmony_ci # Retrieve file contents -- this will be 10867db96d56Sopenharmony_ci # 'First line.\nSecond line.\n' 10877db96d56Sopenharmony_ci contents = output.getvalue() 10887db96d56Sopenharmony_ci 10897db96d56Sopenharmony_ci # Close object and discard memory buffer -- 10907db96d56Sopenharmony_ci # .getvalue() will now raise an exception. 10917db96d56Sopenharmony_ci output.close() 10927db96d56Sopenharmony_ci 10937db96d56Sopenharmony_ci 10947db96d56Sopenharmony_ci.. index:: 10957db96d56Sopenharmony_ci single: universal newlines; io.IncrementalNewlineDecoder class 10967db96d56Sopenharmony_ci 10977db96d56Sopenharmony_ci.. class:: IncrementalNewlineDecoder 10987db96d56Sopenharmony_ci 10997db96d56Sopenharmony_ci A helper codec that decodes newlines for :term:`universal newlines` mode. 11007db96d56Sopenharmony_ci It inherits :class:`codecs.IncrementalDecoder`. 11017db96d56Sopenharmony_ci 11027db96d56Sopenharmony_ci 11037db96d56Sopenharmony_ciPerformance 11047db96d56Sopenharmony_ci----------- 11057db96d56Sopenharmony_ci 11067db96d56Sopenharmony_ciThis section discusses the performance of the provided concrete I/O 11077db96d56Sopenharmony_ciimplementations. 11087db96d56Sopenharmony_ci 11097db96d56Sopenharmony_ciBinary I/O 11107db96d56Sopenharmony_ci^^^^^^^^^^ 11117db96d56Sopenharmony_ci 11127db96d56Sopenharmony_ciBy reading and writing only large chunks of data even when the user asks for a 11137db96d56Sopenharmony_cisingle byte, buffered I/O hides any inefficiency in calling and executing the 11147db96d56Sopenharmony_cioperating system's unbuffered I/O routines. The gain depends on the OS and the 11157db96d56Sopenharmony_cikind of I/O which is performed. For example, on some modern OSes such as Linux, 11167db96d56Sopenharmony_ciunbuffered disk I/O can be as fast as buffered I/O. The bottom line, however, 11177db96d56Sopenharmony_ciis that buffered I/O offers predictable performance regardless of the platform 11187db96d56Sopenharmony_ciand the backing device. Therefore, it is almost always preferable to use 11197db96d56Sopenharmony_cibuffered I/O rather than unbuffered I/O for binary data. 11207db96d56Sopenharmony_ci 11217db96d56Sopenharmony_ciText I/O 11227db96d56Sopenharmony_ci^^^^^^^^ 11237db96d56Sopenharmony_ci 11247db96d56Sopenharmony_ciText I/O over a binary storage (such as a file) is significantly slower than 11257db96d56Sopenharmony_cibinary I/O over the same storage, because it requires conversions between 11267db96d56Sopenharmony_ciunicode and binary data using a character codec. This can become noticeable 11277db96d56Sopenharmony_cihandling huge amounts of text data like large log files. Also, 11287db96d56Sopenharmony_ci:meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both quite slow 11297db96d56Sopenharmony_cidue to the reconstruction algorithm used. 11307db96d56Sopenharmony_ci 11317db96d56Sopenharmony_ci:class:`StringIO`, however, is a native in-memory unicode container and will 11327db96d56Sopenharmony_ciexhibit similar speed to :class:`BytesIO`. 11337db96d56Sopenharmony_ci 11347db96d56Sopenharmony_ciMulti-threading 11357db96d56Sopenharmony_ci^^^^^^^^^^^^^^^ 11367db96d56Sopenharmony_ci 11377db96d56Sopenharmony_ci:class:`FileIO` objects are thread-safe to the extent that the operating system 11387db96d56Sopenharmony_cicalls (such as ``read(2)`` under Unix) they wrap are thread-safe too. 11397db96d56Sopenharmony_ci 11407db96d56Sopenharmony_ciBinary buffered objects (instances of :class:`BufferedReader`, 11417db96d56Sopenharmony_ci:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`) 11427db96d56Sopenharmony_ciprotect their internal structures using a lock; it is therefore safe to call 11437db96d56Sopenharmony_cithem from multiple threads at once. 11447db96d56Sopenharmony_ci 11457db96d56Sopenharmony_ci:class:`TextIOWrapper` objects are not thread-safe. 11467db96d56Sopenharmony_ci 11477db96d56Sopenharmony_ciReentrancy 11487db96d56Sopenharmony_ci^^^^^^^^^^ 11497db96d56Sopenharmony_ci 11507db96d56Sopenharmony_ciBinary buffered objects (instances of :class:`BufferedReader`, 11517db96d56Sopenharmony_ci:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`) 11527db96d56Sopenharmony_ciare not reentrant. While reentrant calls will not happen in normal situations, 11537db96d56Sopenharmony_cithey can arise from doing I/O in a :mod:`signal` handler. If a thread tries to 11547db96d56Sopenharmony_cire-enter a buffered object which it is already accessing, a :exc:`RuntimeError` 11557db96d56Sopenharmony_ciis raised. Note this doesn't prohibit a different thread from entering the 11567db96d56Sopenharmony_cibuffered object. 11577db96d56Sopenharmony_ci 11587db96d56Sopenharmony_ciThe above implicitly extends to text files, since the :func:`open()` function 11597db96d56Sopenharmony_ciwill wrap a buffered object inside a :class:`TextIOWrapper`. This includes 11607db96d56Sopenharmony_cistandard streams and therefore affects the built-in :func:`print()` function as 11617db96d56Sopenharmony_ciwell. 1162