Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b530e14

Browse files
committed
Issue #9715: improve documentation of the io module
1 parent 4a656eb commit b530e14

1 file changed

Lines changed: 162 additions & 52 deletions

File tree

Doc/library/io.rst

Lines changed: 162 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,90 @@
1111
.. moduleauthor:: Benjamin Peterson <[email protected]>
1212
.. sectionauthor:: Benjamin Peterson <[email protected]>
1313

14-
The :mod:`io` module provides the Python interfaces to stream handling. The
15-
built-in :func:`open` function is defined in this module.
14+
Overview
15+
--------
1616

17-
At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
18-
defines the basic interface to a stream. Note, however, that there is no
19-
separation between reading and writing to streams; implementations are allowed
20-
to raise an :exc:`IOError` if they do not support a given operation.
17+
The :mod:`io` module provides Python 3's main facilities for dealing for
18+
various types of I/O. Three main types of I/O are defined: *text I/O*,
19+
*binary I/O*, *raw I/O*. It should be noted that these are generic categories,
20+
and various backing stores can be used for each of them. Concrete objects
21+
belonging to any of these categories will often be called *streams*; another
22+
common term is *file-like objects*.
2123

22-
Extending :class:`IOBase` is :class:`RawIOBase` which deals simply with the
23-
reading and writing of raw bytes to a stream. :class:`FileIO` subclasses
24-
:class:`RawIOBase` to provide an interface to files in the machine's
25-
file system.
24+
Independently of its category, each concrete stream object will also have
25+
various capabilities: it can be read-only, write-only, or read-write; it
26+
can also allow arbitrary random access (seeking forwards or backwards to
27+
any location), or only sequential access (for example in the case of a
28+
socket or pipe).
2629

27-
:class:`BufferedIOBase` deals with buffering on a raw byte stream
28-
(:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`,
29-
:class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are
30-
readable, writable, and both readable and writable.
31-
:class:`BufferedRandom` provides a buffered interface to random access
32-
streams. :class:`BytesIO` is a simple stream of in-memory bytes.
30+
All streams are careful about the type of data you give to them. For example
31+
giving a :class:`str` object to the ``write()`` method of a binary stream
32+
will raise a ``TypeError``. So will giving a :class:`bytes` object to the
33+
``write()`` method of a text stream.
3334

34-
Another :class:`IOBase` subclass, :class:`TextIOBase`, deals with
35-
streams whose bytes represent text, and handles encoding and decoding
36-
from and to strings. :class:`TextIOWrapper`, which extends it, is a
37-
buffered text interface to a buffered raw stream
38-
(:class:`BufferedIOBase`). Finally, :class:`StringIO` is an in-memory
39-
stream for text.
35+
Text I/O
36+
^^^^^^^^
4037

41-
Argument names are not part of the specification, and only the arguments of
42-
:func:`.open` are intended to be used as keyword arguments.
38+
Text I/O expects and produces :class:`str` objects. This means that,
39+
whenever the backing store is natively made of bytes (such as in the case
40+
of a file), encoding and decoding of data is made transparently, as well as,
41+
optionally, translation of platform-specific newline characters.
4342

44-
.. seealso::
45-
:mod:`sys`
46-
contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`,
47-
and :data:`sys.stderr`.
43+
A way to create a text stream is to :meth:`open()` a file in text mode,
44+
optionally specifying an encoding::
45+
46+
f = open("myfile.txt", "r", encoding="utf-8")
47+
48+
In-memory text streams are also available as :class:`StringIO` objects::
49+
50+
f = io.StringIO("some initial text data")
51+
52+
The detailed API of text streams is described by the :class:`TextIOBase`
53+
class.
54+
55+
.. note::
56+
Text I/O over a binary storage (such as a file) is significantly
57+
slower than binary I/O over the same storage. This can become noticeable
58+
if you handle huge amounts of text data (for example very large log files).
59+
60+
Binary I/O
61+
^^^^^^^^^^
62+
63+
Binary I/O (also called *buffered I/O*) expects and produces
64+
:class:`bytes` objects. No encoding, decoding or character translation
65+
is performed. This is the category of streams used for all kinds of non-text
66+
data, and also when manual control over the handling of text data is desired.
67+
68+
A way to create a binary stream is to :meth:`open()` a file in binary mode::
69+
70+
f = open("myfile.jpg", "rb")
71+
72+
In-memory binary streams are also available as :class:`BytesIO` objects::
73+
74+
f = io.BytesIO(b"some initial binary data: \x00\x01")
75+
76+
The detailed API of binary streams is described by the :class:`BufferedIOBase`
77+
class.
78+
79+
Other library modules may provide additional ways to create text or binary
80+
streams. See for example :meth:`socket.socket.makefile`.
81+
82+
Raw I/O
83+
^^^^^^^
84+
85+
Raw I/O (also called *unbuffered I/O*) is generally used as a low-level
86+
building-block for binary and text streams; it is rarely useful to directly
87+
manipulate a raw stream from user code. Nevertheless, you can for example
88+
create a raw stream by opening a file in binary mode with buffering disabled::
89+
90+
f = open("myfile.jpg", "rb", buffering=0)
91+
92+
The detailed API of raw streams is described by the :class:`RawIOBase`
93+
class.
4894

4995

50-
Module Interface
51-
----------------
96+
High-level Module Interface
97+
---------------------------
5298

5399
.. data:: DEFAULT_BUFFER_SIZE
54100

@@ -89,17 +135,22 @@ Module Interface
89135
not be used in new code)
90136
========= ===============================================================
91137

92-
The default mode is ``'rt'`` (open for reading text). For binary random
93-
access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while
94-
``'r+b'`` opens the file without truncation.
138+
The default mode is ``'r'`` (open for reading text, synonym of ``'rt'``).
139+
For binary read-write access, the mode ``'w+b'`` opens and truncates the
140+
file to 0 bytes, while ``'r+b'`` opens the file without truncation.
95141

96-
Python distinguishes between files opened in binary and text modes, even when
97-
the underlying operating system doesn't. Files opened in binary mode
98-
(including ``'b'`` in the *mode* argument) return contents as ``bytes``
99-
objects without any decoding. In text mode (the default, or when ``'t'`` is
100-
included in the *mode* argument), the contents of the file are returned as
101-
strings, the bytes having been first decoded using a platform-dependent
102-
encoding or using the specified *encoding* if given.
142+
As mentioned in the `overview`_, Python distinguishes between binary
143+
and text I/O. Files opened in binary mode (including ``'b'`` in the
144+
*mode* argument) return contents as :class:`bytes` objects without
145+
any decoding. In text mode (the default, or when ``'t'``
146+
is included in the *mode* argument), the contents of the file are
147+
returned as strings, the bytes having been first decoded using a
148+
platform-dependent encoding or using the specified *encoding* if given.
149+
150+
.. note::
151+
Python doesn't depend on the underlying operating system's notion
152+
of text files; all the the processing is done by Python itself, and
153+
is therefore platform-independent.
103154

104155
*buffering* is an optional integer used to set the buffering policy.
105156
Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
@@ -168,11 +219,6 @@ Module Interface
168219
:class:`BufferedRandom`. When buffering is disabled, the raw stream, a
169220
subclass of :class:`RawIOBase`, :class:`FileIO`, is returned.
170221

171-
It is also possible to use a string or bytearray as a file for both reading
172-
and writing. For strings :class:`StringIO` can be used like a file opened in
173-
a text mode, and for bytearrays a :class:`BytesIO` can be used like a
174-
file opened in a binary mode.
175-
176222

177223
.. exception:: BlockingIOError
178224

@@ -194,8 +240,67 @@ Module Interface
194240
when an unsupported operation is called on a stream.
195241

196242

243+
In-memory streams
244+
^^^^^^^^^^^^^^^^^
245+
246+
It is also possible to use a :class:`str` or :class:`bytes`-like object as a
247+
file for both reading and writing. For strings :class:`StringIO` can be
248+
used like a file opened in text mode, and :class:`BytesIO` can be used like
249+
a file opened in binary mode. Both provide full read-write capabilities
250+
with random access.
251+
252+
253+
.. seealso::
254+
:mod:`sys`
255+
contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`,
256+
and :data:`sys.stderr`.
257+
258+
259+
Class hierarchy
260+
---------------
261+
262+
The implementation of I/O streams is organized as a hierarchy of classes.
263+
First :term:`abstract base classes <abstract base class>` (ABCs), which are used to specify the
264+
various categories of streams, then concrete classes providing the standard
265+
stream implementations.
266+
267+
.. note::
268+
The abstract base classes also provide default implementations of
269+
some methods in order to help implementation of concrete stream
270+
classes. For example, :class:`BufferedIOBase` provides
271+
unoptimized implementations of ``readinto()`` and ``readline()``.
272+
273+
At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
274+
defines the basic interface to a stream. Note, however, that there is no
275+
separation between reading and writing to streams; implementations are allowed
276+
to raise an :exc:`UnsupportedOperation` if they do not support a given
277+
operation.
278+
279+
Extending :class:`IOBase` is the :class:`RawIOBase` ABC which deals simply
280+
with the reading and writing of raw bytes to a stream. :class:`FileIO`
281+
subclasses :class:`RawIOBase` to provide an interface to files in the
282+
machine's file system.
283+
284+
The :class:`BufferedIOBase` ABC deals with buffering on a raw byte stream
285+
(:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`,
286+
:class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are
287+
readable, writable, and both readable and writable.
288+
:class:`BufferedRandom` provides a buffered interface to random access
289+
streams. :class:`BytesIO` is a simple stream of in-memory bytes.
290+
291+
Another :class:`IOBase` subclass, the :class:`TextIOBase` ABC, deals with
292+
streams whose bytes represent text, and handles encoding and decoding
293+
from and to strings. :class:`TextIOWrapper`, which extends it, is a
294+
buffered text interface to a buffered raw stream
295+
(:class:`BufferedIOBase`). Finally, :class:`StringIO` is an in-memory
296+
stream for text.
297+
298+
Argument names are not part of the specification, and only the arguments of
299+
:func:`.open` are intended to be used as keyword arguments.
300+
301+
197302
I/O Base Classes
198-
----------------
303+
^^^^^^^^^^^^^^^^
199304

200305
.. class:: IOBase
201306

@@ -467,7 +572,7 @@ I/O Base Classes
467572

468573

469574
Raw File I/O
470-
------------
575+
^^^^^^^^^^^^
471576

472577
.. class:: FileIO(name, mode='r', closefd=True)
473578

@@ -505,7 +610,7 @@ Raw File I/O
505610

506611

507612
Buffered Streams
508-
----------------
613+
^^^^^^^^^^^^^^^^
509614

510615
In many situations, buffered I/O streams will provide higher performance
511616
(bandwidth and latency) than raw I/O streams. Their API is also more usable.
@@ -515,7 +620,7 @@ In many situations, buffered I/O streams will provide higher performance
515620
A stream implementation using an in-memory bytes buffer. It inherits
516621
:class:`BufferedIOBase`.
517622

518-
The argument *initial_bytes* is an optional initial bytearray.
623+
The argument *initial_bytes* contains optional initial :class:`bytes` data.
519624

520625
:class:`BytesIO` provides or overrides these methods in addition to those
521626
from :class:`BufferedIOBase` and :class:`IOBase`:
@@ -632,7 +737,7 @@ In many situations, buffered I/O streams will provide higher performance
632737

633738

634739
Text I/O
635-
--------
740+
^^^^^^^^
636741

637742
.. class:: TextIOBase
638743

@@ -736,14 +841,14 @@ Text I/O
736841

737842
.. class:: StringIO(initial_value='', newline=None)
738843

739-
An in-memory stream for text. It inherits :class:`TextIOWrapper`.
844+
An in-memory stream for text I/O.
740845

741846
The initial value of the buffer (an empty string by default) can be set by
742847
providing *initial_value*. The *newline* argument works like that of
743848
:class:`TextIOWrapper`. The default is to do no newline translation.
744849

745850
:class:`StringIO` provides this method in addition to those from
746-
:class:`TextIOWrapper` and its parents:
851+
:class:`TextIOBase` and its parents:
747852

748853
.. method:: getvalue()
749854

@@ -767,6 +872,11 @@ Text I/O
767872
# .getvalue() will now raise an exception.
768873
output.close()
769874

875+
.. note::
876+
:class:`StringIO` uses a native text storage and doesn't suffer from
877+
the performance issues of other text streams, such as those based on
878+
:class:`TextIOWrapper`.
879+
770880
.. class:: IncrementalNewlineDecoder
771881

772882
A helper codec that decodes newlines for universal newlines mode. It

0 commit comments

Comments
 (0)