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