1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>16.2. io — Core tools for working with streams — Python 3.5.2 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.2 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.2 documentation" href="../contents.html" />
33
<link rel="up" title="16. Generic Operating System Services" href="allos.html" />
34
<link rel="next" title="16.3. time — Time access and conversions" href="time.html" />
35
<link rel="prev" title="16.1. os — Miscellaneous operating system interfaces" href="os.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
38
<script type="text/javascript" src="../_static/copybutton.js"></script>
39
<script type="text/javascript" src="../_static/version_switch.js"></script>
45
<body role="document">
46
<div class="related" role="navigation" aria-label="related navigation">
49
<li class="right" style="margin-right: 10px">
50
<a href="../genindex.html" title="General Index"
51
accesskey="I">index</a></li>
53
<a href="../py-modindex.html" title="Python Module Index"
56
<a href="time.html" title="16.3. time — Time access and conversions"
57
accesskey="N">next</a> |</li>
59
<a href="os.html" title="16.1. os — Miscellaneous operating system interfaces"
60
accesskey="P">previous</a> |</li>
61
<li><img src="../_static/py.png" alt=""
62
style="vertical-align: middle; margin-top: -1px"/></li>
63
<li><a href="https://www.python.org/">Python</a> »</li>
65
<span class="version_switcher_placeholder">3.5.2</span>
66
<a href="../index.html">Documentation </a> »
69
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
70
<li class="nav-item nav-item-2"><a href="allos.html" accesskey="U">16. Generic Operating System Services</a> »</li>
74
<div class="inline-search" style="display: none" role="search">
75
<form class="inline-search" action="../search.html" method="get">
76
<input placeholder="Quick search" type="text" name="q" />
77
<input type="submit" value="Go" />
78
<input type="hidden" name="check_keywords" value="yes" />
79
<input type="hidden" name="area" value="default" />
82
<script type="text/javascript">$('.inline-search').show(0);</script>
89
<div class="document">
90
<div class="documentwrapper">
91
<div class="bodywrapper">
92
<div class="body" role="main">
94
<div class="section" id="module-io">
95
<span id="io-core-tools-for-working-with-streams"></span><h1>16.2. <a class="reference internal" href="#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal"><span class="pre">io</span></code></a> — Core tools for working with streams<a class="headerlink" href="#module-io" title="Permalink to this headline">¶</a></h1>
96
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/io.py">Lib/io.py</a></p>
97
<hr class="docutils" />
98
<div class="section" id="overview">
99
<span id="io-overview"></span><h2>16.2.1. Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
100
<p id="index-0">The <a class="reference internal" href="#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal"><span class="pre">io</span></code></a> module provides Python’s main facilities for dealing with various
101
types of I/O. There are three main types of I/O: <em>text I/O</em>, <em>binary I/O</em>
102
and <em>raw I/O</em>. These are generic categories, and various backing stores can
103
be used for each of them. A concrete object belonging to any of these
104
categories is called a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>. Other common terms are <em>stream</em>
105
and <em>file-like object</em>.</p>
106
<p>Independently of its category, each concrete stream object will also have
107
various capabilities: it can be read-only, write-only, or read-write. It can
108
also allow arbitrary random access (seeking forwards or backwards to any
109
location), or only sequential access (for example in the case of a socket or
111
<p>All streams are careful about the type of data you give to them. For example
112
giving a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> object to the <code class="docutils literal"><span class="pre">write()</span></code> method of a binary stream
113
will raise a <code class="docutils literal"><span class="pre">TypeError</span></code>. So will giving a <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> object to the
114
<code class="docutils literal"><span class="pre">write()</span></code> method of a text stream.</p>
115
<div class="versionchanged">
116
<p><span class="versionmodified">Changed in version 3.3: </span>Operations that used to raise <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal"><span class="pre">IOError</span></code></a> now raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a>, since
117
<a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal"><span class="pre">IOError</span></code></a> is now an alias of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a>.</p>
119
<div class="section" id="text-i-o">
120
<h3>16.2.1.1. Text I/O<a class="headerlink" href="#text-i-o" title="Permalink to this headline">¶</a></h3>
121
<p>Text I/O expects and produces <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> objects. This means that whenever
122
the backing store is natively made of bytes (such as in the case of a file),
123
encoding and decoding of data is made transparently as well as optional
124
translation of platform-specific newline characters.</p>
125
<p>The easiest way to create a text stream is with <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-meth docutils literal"><span class="pre">open()</span></code></a>, optionally
126
specifying an encoding:</p>
127
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">,</span> <span class="s2">"r"</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">"utf-8"</span><span class="p">)</span>
130
<p>In-memory text streams are also available as <a class="reference internal" href="#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal"><span class="pre">StringIO</span></code></a> objects:</p>
131
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">StringIO</span><span class="p">(</span><span class="s2">"some initial text data"</span><span class="p">)</span>
134
<p>The text stream API is described in detail in the documentation of
135
<a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a>.</p>
137
<div class="section" id="binary-i-o">
138
<h3>16.2.1.2. Binary I/O<a class="headerlink" href="#binary-i-o" title="Permalink to this headline">¶</a></h3>
139
<p>Binary I/O (also called <em>buffered I/O</em>) expects
140
<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> and produces <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>
141
objects. No encoding, decoding, or newline translation is performed. This
142
category of streams can be used for all kinds of non-text data, and also when
143
manual control over the handling of text data is desired.</p>
144
<p>The easiest way to create a binary stream is with <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-meth docutils literal"><span class="pre">open()</span></code></a> with <code class="docutils literal"><span class="pre">'b'</span></code> in
146
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"myfile.jpg"</span><span class="p">,</span> <span class="s2">"rb"</span><span class="p">)</span>
149
<p>In-memory binary streams are also available as <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">BytesIO</span></code></a> objects:</p>
150
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">BytesIO</span><span class="p">(</span><span class="n">b</span><span class="s2">"some initial binary data: </span><span class="se">\x00\x01</span><span class="s2">"</span><span class="p">)</span>
153
<p>The binary stream API is described in detail in the docs of
154
<a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a>.</p>
155
<p>Other library modules may provide additional ways to create text or binary
156
streams. See <a class="reference internal" href="socket.html#socket.socket.makefile" title="socket.socket.makefile"><code class="xref py py-meth docutils literal"><span class="pre">socket.socket.makefile()</span></code></a> for example.</p>
158
<div class="section" id="raw-i-o">
159
<h3>16.2.1.3. Raw I/O<a class="headerlink" href="#raw-i-o" title="Permalink to this headline">¶</a></h3>
160
<p>Raw I/O (also called <em>unbuffered I/O</em>) is generally used as a low-level
161
building-block for binary and text streams; it is rarely useful to directly
162
manipulate a raw stream from user code. Nevertheless, you can create a raw
163
stream by opening a file in binary mode with buffering disabled:</p>
164
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"myfile.jpg"</span><span class="p">,</span> <span class="s2">"rb"</span><span class="p">,</span> <span class="n">buffering</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
167
<p>The raw stream API is described in detail in the docs of <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a>.</p>
170
<div class="section" id="high-level-module-interface">
171
<h2>16.2.2. High-level Module Interface<a class="headerlink" href="#high-level-module-interface" title="Permalink to this headline">¶</a></h2>
173
<dt id="io.DEFAULT_BUFFER_SIZE">
174
<code class="descclassname">io.</code><code class="descname">DEFAULT_BUFFER_SIZE</code><a class="headerlink" href="#io.DEFAULT_BUFFER_SIZE" title="Permalink to this definition">¶</a></dt>
175
<dd><p>An int containing the default buffer size used by the module’s buffered I/O
176
classes. <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> uses the file’s blksize (as obtained by
177
<a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal"><span class="pre">os.stat()</span></code></a>) if possible.</p>
180
<dl class="function">
182
<code class="descclassname">io.</code><code class="descname">open</code><span class="sig-paren">(</span><em>file</em>, <em>mode='r'</em>, <em>buffering=-1</em>, <em>encoding=None</em>, <em>errors=None</em>, <em>newline=None</em>, <em>closefd=True</em>, <em>opener=None</em><span class="sig-paren">)</span><a class="headerlink" href="#io.open" title="Permalink to this definition">¶</a></dt>
183
<dd><p>This is an alias for the builtin <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> function.</p>
186
<dl class="exception">
187
<dt id="io.BlockingIOError">
188
<em class="property">exception </em><code class="descclassname">io.</code><code class="descname">BlockingIOError</code><a class="headerlink" href="#io.BlockingIOError" title="Permalink to this definition">¶</a></dt>
189
<dd><p>This is a compatibility alias for the builtin <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal"><span class="pre">BlockingIOError</span></code></a>
193
<dl class="exception">
194
<dt id="io.UnsupportedOperation">
195
<em class="property">exception </em><code class="descclassname">io.</code><code class="descname">UnsupportedOperation</code><a class="headerlink" href="#io.UnsupportedOperation" title="Permalink to this definition">¶</a></dt>
196
<dd><p>An exception inheriting <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> and <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> that is raised
197
when an unsupported operation is called on a stream.</p>
200
<div class="section" id="in-memory-streams">
201
<h3>16.2.2.1. In-memory streams<a class="headerlink" href="#in-memory-streams" title="Permalink to this headline">¶</a></h3>
202
<p>It is also possible to use a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> or <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> as a
203
file for both reading and writing. For strings <a class="reference internal" href="#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal"><span class="pre">StringIO</span></code></a> can be used
204
like a file opened in text mode. <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">BytesIO</span></code></a> can be used like a file
205
opened in binary mode. Both provide full read-write capabilities with random
207
<div class="admonition seealso">
208
<p class="first admonition-title">See also</p>
209
<dl class="last docutils">
210
<dt><a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal"><span class="pre">sys</span></code></a></dt>
211
<dd>contains the standard IO streams: <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal"><span class="pre">sys.stdin</span></code></a>, <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal"><span class="pre">sys.stdout</span></code></a>,
212
and <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal"><span class="pre">sys.stderr</span></code></a>.</dd>
217
<div class="section" id="class-hierarchy">
218
<h2>16.2.3. Class hierarchy<a class="headerlink" href="#class-hierarchy" title="Permalink to this headline">¶</a></h2>
219
<p>The implementation of I/O streams is organized as a hierarchy of classes. First
220
<a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base classes</span></a> (ABCs), which are used to
221
specify the various categories of streams, then concrete classes providing the
222
standard stream implementations.</p>
224
<div><div class="admonition note">
225
<p class="first admonition-title">Note</p>
226
<p class="last">The abstract base classes also provide default implementations of some
227
methods in order to help implementation of concrete stream classes. For
228
example, <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> provides unoptimized implementations of
229
<code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code> and <a class="reference internal" href="#io.IOBase.readline" title="io.IOBase.readline"><code class="xref py py-meth docutils literal"><span class="pre">readline()</span></code></a>.</p>
232
<p>At the top of the I/O hierarchy is the abstract base class <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>. It
233
defines the basic interface to a stream. Note, however, that there is no
234
separation between reading and writing to streams; implementations are allowed
235
to raise <a class="reference internal" href="#io.UnsupportedOperation" title="io.UnsupportedOperation"><code class="xref py py-exc docutils literal"><span class="pre">UnsupportedOperation</span></code></a> if they do not support a given operation.</p>
236
<p>The <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> ABC extends <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>. It deals with the reading
237
and writing of bytes to a stream. <a class="reference internal" href="#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal"><span class="pre">FileIO</span></code></a> subclasses <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a>
238
to provide an interface to files in the machine’s file system.</p>
239
<p>The <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> ABC deals with buffering on a raw byte stream
240
(<a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a>). Its subclasses, <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></code></a>,
241
<a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">BufferedReader</span></code></a>, and <a class="reference internal" href="#io.BufferedRWPair" title="io.BufferedRWPair"><code class="xref py py-class docutils literal"><span class="pre">BufferedRWPair</span></code></a> buffer streams that are
242
readable, writable, and both readable and writable. <a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal"><span class="pre">BufferedRandom</span></code></a>
243
provides a buffered interface to random access streams. Another
244
<a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> subclass, <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">BytesIO</span></code></a>, is a stream of in-memory
246
<p>The <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a> ABC, another subclass of <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>, deals with
247
streams whose bytes represent text, and handles encoding and decoding to and
248
from strings. <a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextIOWrapper</span></code></a>, which extends it, is a buffered text
249
interface to a buffered raw stream (<a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a>). Finally,
250
<a class="reference internal" href="#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal"><span class="pre">StringIO</span></code></a> is an in-memory stream for text.</p>
251
<p>Argument names are not part of the specification, and only the arguments of
252
<a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> are intended to be used as keyword arguments.</p>
253
<p>The following table summarizes the ABCs provided by the <a class="reference internal" href="#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal"><span class="pre">io</span></code></a> module:</p>
254
<table border="1" class="docutils">
261
<thead valign="bottom">
262
<tr class="row-odd"><th class="head">ABC</th>
263
<th class="head">Inherits</th>
264
<th class="head">Stub Methods</th>
265
<th class="head">Mixin Methods and Properties</th>
269
<tr class="row-even"><td><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a></td>
271
<td><code class="docutils literal"><span class="pre">fileno</span></code>, <code class="docutils literal"><span class="pre">seek</span></code>,
272
and <code class="docutils literal"><span class="pre">truncate</span></code></td>
273
<td><code class="docutils literal"><span class="pre">close</span></code>, <code class="docutils literal"><span class="pre">closed</span></code>, <code class="docutils literal"><span class="pre">__enter__</span></code>,
274
<code class="docutils literal"><span class="pre">__exit__</span></code>, <code class="docutils literal"><span class="pre">flush</span></code>, <code class="docutils literal"><span class="pre">isatty</span></code>, <code class="docutils literal"><span class="pre">__iter__</span></code>,
275
<code class="docutils literal"><span class="pre">__next__</span></code>, <code class="docutils literal"><span class="pre">readable</span></code>, <code class="docutils literal"><span class="pre">readline</span></code>,
276
<code class="docutils literal"><span class="pre">readlines</span></code>, <code class="docutils literal"><span class="pre">seekable</span></code>, <code class="docutils literal"><span class="pre">tell</span></code>,
277
<code class="docutils literal"><span class="pre">writable</span></code>, and <code class="docutils literal"><span class="pre">writelines</span></code></td>
279
<tr class="row-odd"><td><a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a></td>
280
<td><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a></td>
281
<td><code class="docutils literal"><span class="pre">readinto</span></code> and
282
<code class="docutils literal"><span class="pre">write</span></code></td>
283
<td>Inherited <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> methods, <code class="docutils literal"><span class="pre">read</span></code>,
284
and <code class="docutils literal"><span class="pre">readall</span></code></td>
286
<tr class="row-even"><td><a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a></td>
287
<td><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a></td>
288
<td><code class="docutils literal"><span class="pre">detach</span></code>, <code class="docutils literal"><span class="pre">read</span></code>,
289
<code class="docutils literal"><span class="pre">read1</span></code>, and <code class="docutils literal"><span class="pre">write</span></code></td>
290
<td>Inherited <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> methods, <code class="docutils literal"><span class="pre">readinto</span></code></td>
292
<tr class="row-odd"><td><a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a></td>
293
<td><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a></td>
294
<td><code class="docutils literal"><span class="pre">detach</span></code>, <code class="docutils literal"><span class="pre">read</span></code>,
295
<code class="docutils literal"><span class="pre">readline</span></code>, and
296
<code class="docutils literal"><span class="pre">write</span></code></td>
297
<td>Inherited <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> methods, <code class="docutils literal"><span class="pre">encoding</span></code>,
298
<code class="docutils literal"><span class="pre">errors</span></code>, and <code class="docutils literal"><span class="pre">newlines</span></code></td>
302
<div class="section" id="i-o-base-classes">
303
<h3>16.2.3.1. I/O Base Classes<a class="headerlink" href="#i-o-base-classes" title="Permalink to this headline">¶</a></h3>
306
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">IOBase</code><a class="headerlink" href="#io.IOBase" title="Permalink to this definition">¶</a></dt>
307
<dd><p>The abstract base class for all I/O classes, acting on streams of bytes.
308
There is no public constructor.</p>
309
<p>This class provides empty abstract implementations for many methods
310
that derived classes can override selectively; the default
311
implementations represent a file that cannot be read, written or
313
<p>Even though <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> does not declare <code class="xref py py-meth docutils literal"><span class="pre">read()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code>,
314
or <code class="xref py py-meth docutils literal"><span class="pre">write()</span></code> because their signatures will vary, implementations and
315
clients should consider those methods part of the interface. Also,
316
implementations may raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> (or <a class="reference internal" href="#io.UnsupportedOperation" title="io.UnsupportedOperation"><code class="xref py py-exc docutils literal"><span class="pre">UnsupportedOperation</span></code></a>)
317
when operations they do not support are called.</p>
318
<p>The basic type used for binary data read from or written to a file is
319
<a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>. Other <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> are
320
accepted as method arguments too. In some cases, such as
321
<a class="reference internal" href="#io.RawIOBase.readinto" title="io.RawIOBase.readinto"><code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code></a>, a writable object such as <a class="reference internal" href="functions.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal"><span class="pre">bytearray</span></code></a>
322
is required. Text I/O classes work with <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> data.</p>
323
<p>Note that calling any method (even inquiries) on a closed stream is
324
undefined. Implementations may raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> in this case.</p>
325
<p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> (and its subclasses) supports the iterator protocol, meaning
326
that an <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> object can be iterated over yielding the lines in a
327
stream. Lines are defined slightly differently depending on whether the
328
stream is a binary stream (yielding bytes), or a text stream (yielding
329
character strings). See <a class="reference internal" href="#io.IOBase.readline" title="io.IOBase.readline"><code class="xref py py-meth docutils literal"><span class="pre">readline()</span></code></a> below.</p>
330
<p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> is also a context manager and therefore supports the
331
<a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statement. In this example, <em>file</em> is closed after the
332
<a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statement’s suite is finished—even if an exception occurs:</p>
333
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'spam.txt'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
334
<span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'Spam and eggs!'</span><span class="p">)</span>
337
<p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> provides these data attributes and methods:</p>
339
<dt id="io.IOBase.close">
340
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.close" title="Permalink to this definition">¶</a></dt>
341
<dd><p>Flush and close this stream. This method has no effect if the file is
342
already closed. Once the file is closed, any operation on the file
343
(e.g. reading or writing) will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a>.</p>
344
<p>As a convenience, it is allowed to call this method more than once;
345
only the first call, however, will have an effect.</p>
348
<dl class="attribute">
349
<dt id="io.IOBase.closed">
350
<code class="descname">closed</code><a class="headerlink" href="#io.IOBase.closed" title="Permalink to this definition">¶</a></dt>
351
<dd><p><code class="docutils literal"><span class="pre">True</span></code> if the stream is closed.</p>
355
<dt id="io.IOBase.fileno">
356
<code class="descname">fileno</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.fileno" title="Permalink to this definition">¶</a></dt>
357
<dd><p>Return the underlying file descriptor (an integer) of the stream if it
358
exists. An <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> is raised if the IO object does not use a file
363
<dt id="io.IOBase.flush">
364
<code class="descname">flush</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.flush" title="Permalink to this definition">¶</a></dt>
365
<dd><p>Flush the write buffers of the stream if applicable. This does nothing
366
for read-only and non-blocking streams.</p>
370
<dt id="io.IOBase.isatty">
371
<code class="descname">isatty</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.isatty" title="Permalink to this definition">¶</a></dt>
372
<dd><p>Return <code class="docutils literal"><span class="pre">True</span></code> if the stream is interactive (i.e., connected to
373
a terminal/tty device).</p>
377
<dt id="io.IOBase.readable">
378
<code class="descname">readable</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.readable" title="Permalink to this definition">¶</a></dt>
379
<dd><p>Return <code class="docutils literal"><span class="pre">True</span></code> if the stream can be read from. If <code class="docutils literal"><span class="pre">False</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">read()</span></code>
380
will raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a>.</p>
384
<dt id="io.IOBase.readline">
385
<code class="descname">readline</code><span class="sig-paren">(</span><em>size=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.readline" title="Permalink to this definition">¶</a></dt>
386
<dd><p>Read and return one line from the stream. If <em>size</em> is specified, at
387
most <em>size</em> bytes will be read.</p>
388
<p>The line terminator is always <code class="docutils literal"><span class="pre">b'\n'</span></code> for binary files; for text files,
389
the <em>newline</em> argument to <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> can be used to select the line
390
terminator(s) recognized.</p>
394
<dt id="io.IOBase.readlines">
395
<code class="descname">readlines</code><span class="sig-paren">(</span><em>hint=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.readlines" title="Permalink to this definition">¶</a></dt>
396
<dd><p>Read and return a list of lines from the stream. <em>hint</em> 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 <em>hint</em>.</p>
399
<p>Note that it’s already possible to iterate on file objects using <code class="docutils literal"><span class="pre">for</span>
400
<span class="pre">line</span> <span class="pre">in</span> <span class="pre">file:</span> <span class="pre">...</span></code> without calling <code class="docutils literal"><span class="pre">file.readlines()</span></code>.</p>
404
<dt id="io.IOBase.seek">
405
<code class="descname">seek</code><span class="sig-paren">(</span><em>offset</em><span class="optional">[</span>, <em>whence</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.seek" title="Permalink to this definition">¶</a></dt>
406
<dd><p>Change the stream position to the given byte <em>offset</em>. <em>offset</em> is
407
interpreted relative to the position indicated by <em>whence</em>. The default
408
value for <em>whence</em> is <code class="xref py py-data docutils literal"><span class="pre">SEEK_SET</span></code>. Values for <em>whence</em> are:</p>
410
<li><code class="xref py py-data docutils literal"><span class="pre">SEEK_SET</span></code> or <code class="docutils literal"><span class="pre">0</span></code> – start of the stream (the default);
411
<em>offset</em> should be zero or positive</li>
412
<li><code class="xref py py-data docutils literal"><span class="pre">SEEK_CUR</span></code> or <code class="docutils literal"><span class="pre">1</span></code> – current stream position; <em>offset</em> may
414
<li><code class="xref py py-data docutils literal"><span class="pre">SEEK_END</span></code> or <code class="docutils literal"><span class="pre">2</span></code> – end of the stream; <em>offset</em> is usually
417
<p>Return the new absolute position.</p>
418
<div class="versionadded">
419
<p><span class="versionmodified">New in version 3.1: </span>The <code class="docutils literal"><span class="pre">SEEK_*</span></code> constants.</p>
421
<div class="versionadded">
422
<p><span class="versionmodified">New in version 3.3: </span>Some operating systems could support additional values, like
423
<code class="xref py py-data docutils literal"><span class="pre">os.SEEK_HOLE</span></code> or <code class="xref py py-data docutils literal"><span class="pre">os.SEEK_DATA</span></code>. The valid values
424
for a file could depend on it being open in text or binary mode.</p>
429
<dt id="io.IOBase.seekable">
430
<code class="descname">seekable</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.seekable" title="Permalink to this definition">¶</a></dt>
431
<dd><p>Return <code class="docutils literal"><span class="pre">True</span></code> if the stream supports random access. If <code class="docutils literal"><span class="pre">False</span></code>,
432
<a class="reference internal" href="#io.IOBase.seek" title="io.IOBase.seek"><code class="xref py py-meth docutils literal"><span class="pre">seek()</span></code></a>, <a class="reference internal" href="#io.IOBase.tell" title="io.IOBase.tell"><code class="xref py py-meth docutils literal"><span class="pre">tell()</span></code></a> and <a class="reference internal" href="#io.IOBase.truncate" title="io.IOBase.truncate"><code class="xref py py-meth docutils literal"><span class="pre">truncate()</span></code></a> will raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a>.</p>
436
<dt id="io.IOBase.tell">
437
<code class="descname">tell</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.tell" title="Permalink to this definition">¶</a></dt>
438
<dd><p>Return the current stream position.</p>
442
<dt id="io.IOBase.truncate">
443
<code class="descname">truncate</code><span class="sig-paren">(</span><em>size=None</em><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.truncate" title="Permalink to this definition">¶</a></dt>
444
<dd><p>Resize the stream to the given <em>size</em> in bytes (or the current position
445
if <em>size</em> is not specified). The current stream position isn’t changed.
446
This resizing can extend or reduce the current file size. In case of
447
extension, the contents of the new file area depend on the platform
448
(on most systems, additional bytes are zero-filled). The new file size
452
<div class="versionchanged">
453
<p><span class="versionmodified">Changed in version 3.5: </span>Windows will now zero-fill files when extending.</p>
456
<dt id="io.IOBase.writable">
457
<code class="descname">writable</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.writable" title="Permalink to this definition">¶</a></dt>
458
<dd><p>Return <code class="docutils literal"><span class="pre">True</span></code> if the stream supports writing. If <code class="docutils literal"><span class="pre">False</span></code>,
459
<code class="xref py py-meth docutils literal"><span class="pre">write()</span></code> and <a class="reference internal" href="#io.IOBase.truncate" title="io.IOBase.truncate"><code class="xref py py-meth docutils literal"><span class="pre">truncate()</span></code></a> will raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a>.</p>
463
<dt id="io.IOBase.writelines">
464
<code class="descname">writelines</code><span class="sig-paren">(</span><em>lines</em><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.writelines" title="Permalink to this definition">¶</a></dt>
465
<dd><p>Write a list of lines to the stream. Line separators are not added, so it
466
is usual for each of the lines provided to have a line separator at the
471
<dt id="io.IOBase.__del__">
472
<code class="descname">__del__</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.__del__" title="Permalink to this definition">¶</a></dt>
473
<dd><p>Prepare for object destruction. <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> provides a default
474
implementation of this method that calls the instance’s
475
<a class="reference internal" href="#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal"><span class="pre">close()</span></code></a> method.</p>
481
<dt id="io.RawIOBase">
482
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">RawIOBase</code><a class="headerlink" href="#io.RawIOBase" title="Permalink to this definition">¶</a></dt>
483
<dd><p>Base class for raw binary I/O. It inherits <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>. There is no
484
public constructor.</p>
485
<p>Raw binary I/O typically provides low-level access to an underlying OS
486
device or API, and does not try to encapsulate it in high-level primitives
487
(this is left to Buffered I/O and Text I/O, described later in this page).</p>
488
<p>In addition to the attributes and methods from <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>,
489
<a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> provides the following methods:</p>
491
<dt id="io.RawIOBase.read">
492
<code class="descname">read</code><span class="sig-paren">(</span><em>size=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#io.RawIOBase.read" title="Permalink to this definition">¶</a></dt>
493
<dd><p>Read up to <em>size</em> bytes from the object and return them. As a convenience,
494
if <em>size</em> is unspecified or -1, <a class="reference internal" href="#io.RawIOBase.readall" title="io.RawIOBase.readall"><code class="xref py py-meth docutils literal"><span class="pre">readall()</span></code></a> is called. Otherwise,
495
only one system call is ever made. Fewer than <em>size</em> bytes may be
496
returned if the operating system call returns fewer than <em>size</em> bytes.</p>
497
<p>If 0 bytes are returned, and <em>size</em> was not 0, this indicates end of file.
498
If the object is in non-blocking mode and no bytes are available,
499
<code class="docutils literal"><span class="pre">None</span></code> is returned.</p>
503
<dt id="io.RawIOBase.readall">
504
<code class="descname">readall</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.RawIOBase.readall" title="Permalink to this definition">¶</a></dt>
505
<dd><p>Read and return all the bytes from the stream until EOF, using multiple
506
calls to the stream if necessary.</p>
510
<dt id="io.RawIOBase.readinto">
511
<code class="descname">readinto</code><span class="sig-paren">(</span><em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#io.RawIOBase.readinto" title="Permalink to this definition">¶</a></dt>
512
<dd><p>Read bytes into a pre-allocated, writable
513
<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>b</em>, and return the
514
number of bytes read. If the object is in non-blocking mode and no bytes
515
are available, <code class="docutils literal"><span class="pre">None</span></code> is returned.</p>
519
<dt id="io.RawIOBase.write">
520
<code class="descname">write</code><span class="sig-paren">(</span><em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#io.RawIOBase.write" title="Permalink to this definition">¶</a></dt>
521
<dd><p>Write the given <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>, <em>b</em>, to the
522
underlying raw stream, and return the number of
523
bytes written. This can be less than the length of <em>b</em> in
524
bytes, depending on specifics of the underlying raw
525
stream, and especially if it is in non-blocking mode. <code class="docutils literal"><span class="pre">None</span></code> is
526
returned if the raw stream is set not to block and no single byte could
527
be readily written to it. The caller may release or mutate <em>b</em> after
528
this method returns, so the implementation should only access <em>b</em>
529
during the method call.</p>
535
<dt id="io.BufferedIOBase">
536
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">BufferedIOBase</code><a class="headerlink" href="#io.BufferedIOBase" title="Permalink to this definition">¶</a></dt>
537
<dd><p>Base class for binary streams that support some kind of buffering.
538
It inherits <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>. There is no public constructor.</p>
539
<p>The main difference with <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> is that methods <a class="reference internal" href="#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal"><span class="pre">read()</span></code></a>,
540
<a class="reference internal" href="#io.BufferedIOBase.readinto" title="io.BufferedIOBase.readinto"><code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code></a> and <a class="reference internal" href="#io.BufferedIOBase.write" title="io.BufferedIOBase.write"><code class="xref py py-meth docutils literal"><span class="pre">write()</span></code></a> will try (respectively) to read as much
541
input as requested or to consume all given output, at the expense of
542
making perhaps more than one system call.</p>
543
<p>In addition, those methods can raise <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal"><span class="pre">BlockingIOError</span></code></a> if the
544
underlying raw stream is in non-blocking mode and cannot take or give
545
enough data; unlike their <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> counterparts, they will
546
never return <code class="docutils literal"><span class="pre">None</span></code>.</p>
547
<p>Besides, the <a class="reference internal" href="#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal"><span class="pre">read()</span></code></a> method does not have a default
548
implementation that defers to <a class="reference internal" href="#io.BufferedIOBase.readinto" title="io.BufferedIOBase.readinto"><code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code></a>.</p>
549
<p>A typical <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> implementation should not inherit from a
550
<a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> implementation, but wrap one, like
551
<a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></code></a> and <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">BufferedReader</span></code></a> do.</p>
552
<p><a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> provides or overrides these methods and attribute in
553
addition to those from <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>:</p>
554
<dl class="attribute">
555
<dt id="io.BufferedIOBase.raw">
556
<code class="descname">raw</code><a class="headerlink" href="#io.BufferedIOBase.raw" title="Permalink to this definition">¶</a></dt>
557
<dd><p>The underlying raw stream (a <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> instance) that
558
<a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> deals with. This is not part of the
559
<a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> API and may not exist on some implementations.</p>
563
<dt id="io.BufferedIOBase.detach">
564
<code class="descname">detach</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.detach" title="Permalink to this definition">¶</a></dt>
565
<dd><p>Separate the underlying raw stream from the buffer and return it.</p>
566
<p>After the raw stream has been detached, the buffer is in an unusable
568
<p>Some buffers, like <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">BytesIO</span></code></a>, do not have the concept of a single
569
raw stream to return from this method. They raise
570
<a class="reference internal" href="#io.UnsupportedOperation" title="io.UnsupportedOperation"><code class="xref py py-exc docutils literal"><span class="pre">UnsupportedOperation</span></code></a>.</p>
571
<div class="versionadded">
572
<p><span class="versionmodified">New in version 3.1.</span></p>
577
<dt id="io.BufferedIOBase.read">
578
<code class="descname">read</code><span class="sig-paren">(</span><em>size=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.read" title="Permalink to this definition">¶</a></dt>
579
<dd><p>Read and return up to <em>size</em> bytes. If the argument is omitted, <code class="docutils literal"><span class="pre">None</span></code>,
580
or negative, data is read and returned until EOF is reached. An empty
581
<a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> object is returned if the stream is already at EOF.</p>
582
<p>If the argument is positive, and the underlying raw stream is not
583
interactive, multiple raw reads may be issued to satisfy the byte count
584
(unless EOF is reached first). But for interactive raw streams, at most
585
one raw read will be issued, and a short result does not imply that EOF is
587
<p>A <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal"><span class="pre">BlockingIOError</span></code></a> is raised if the underlying raw stream is in
588
non blocking-mode, and has no data available at the moment.</p>
592
<dt id="io.BufferedIOBase.read1">
593
<code class="descname">read1</code><span class="sig-paren">(</span><em>size=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.read1" title="Permalink to this definition">¶</a></dt>
594
<dd><p>Read and return up to <em>size</em> bytes, with at most one call to the
595
underlying raw stream’s <a class="reference internal" href="#io.RawIOBase.read" title="io.RawIOBase.read"><code class="xref py py-meth docutils literal"><span class="pre">read()</span></code></a> (or
596
<a class="reference internal" href="#io.RawIOBase.readinto" title="io.RawIOBase.readinto"><code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code></a>) method. This can be useful if you are
597
implementing your own buffering on top of a <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a>
602
<dt id="io.BufferedIOBase.readinto">
603
<code class="descname">readinto</code><span class="sig-paren">(</span><em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.readinto" title="Permalink to this definition">¶</a></dt>
604
<dd><p>Read bytes into a pre-allocated, writable
605
<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>b</em> and return the number of bytes read.</p>
606
<p>Like <a class="reference internal" href="#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal"><span class="pre">read()</span></code></a>, multiple reads may be issued to the underlying raw
607
stream, unless the latter is interactive.</p>
608
<p>A <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal"><span class="pre">BlockingIOError</span></code></a> is raised if the underlying raw stream is in non
609
blocking-mode, and has no data available at the moment.</p>
613
<dt id="io.BufferedIOBase.readinto1">
614
<code class="descname">readinto1</code><span class="sig-paren">(</span><em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.readinto1" title="Permalink to this definition">¶</a></dt>
615
<dd><p>Read bytes into a pre-allocated, writable
616
<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>b</em>, using at most one call to
617
the underlying raw stream’s <a class="reference internal" href="#io.RawIOBase.read" title="io.RawIOBase.read"><code class="xref py py-meth docutils literal"><span class="pre">read()</span></code></a> (or
618
<a class="reference internal" href="#io.RawIOBase.readinto" title="io.RawIOBase.readinto"><code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code></a>) method. Return the number of bytes read.</p>
619
<p>A <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal"><span class="pre">BlockingIOError</span></code></a> is raised if the underlying raw stream is in non
620
blocking-mode, and has no data available at the moment.</p>
621
<div class="versionadded">
622
<p><span class="versionmodified">New in version 3.5.</span></p>
627
<dt id="io.BufferedIOBase.write">
628
<code class="descname">write</code><span class="sig-paren">(</span><em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.write" title="Permalink to this definition">¶</a></dt>
629
<dd><p>Write the given <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>, <em>b</em>, and return the number
630
of bytes written (always equal to the length of <em>b</em> in bytes, since if
631
the write fails an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> will be raised). Depending on the
632
actual implementation, these bytes may be readily written to the
633
underlying stream, or held in a buffer for performance and latency
635
<p>When in non-blocking mode, a <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal"><span class="pre">BlockingIOError</span></code></a> is raised if the
636
data needed to be written to the raw stream but it couldn’t accept
637
all the data without blocking.</p>
638
<p>The caller may release or mutate <em>b</em> after this method returns,
639
so the implementation should only access <em>b</em> during the method call.</p>
645
<div class="section" id="raw-file-i-o">
646
<h3>16.2.3.2. Raw File I/O<a class="headerlink" href="#raw-file-i-o" title="Permalink to this headline">¶</a></h3>
649
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">FileIO</code><span class="sig-paren">(</span><em>name</em>, <em>mode='r'</em>, <em>closefd=True</em>, <em>opener=None</em><span class="sig-paren">)</span><a class="headerlink" href="#io.FileIO" title="Permalink to this definition">¶</a></dt>
650
<dd><p><a class="reference internal" href="#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal"><span class="pre">FileIO</span></code></a> represents an OS-level file containing bytes data.
651
It implements the <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> interface (and therefore the
652
<a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> interface, too).</p>
653
<p>The <em>name</em> can be one of two things:</p>
655
<li>a character string or <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> object representing the path to the
656
file which will be opened. In this case closefd must be <code class="docutils literal"><span class="pre">True</span></code> (the default)
657
otherwise an error will be raised.</li>
658
<li>an integer representing the number of an existing OS-level file descriptor
659
to which the resulting <a class="reference internal" href="#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal"><span class="pre">FileIO</span></code></a> object will give access. When the
660
FileIO object is closed this fd will be closed as well, unless <em>closefd</em>
661
is set to <code class="docutils literal"><span class="pre">False</span></code>.</li>
663
<p>The <em>mode</em> can be <code class="docutils literal"><span class="pre">'r'</span></code>, <code class="docutils literal"><span class="pre">'w'</span></code>, <code class="docutils literal"><span class="pre">'x'</span></code> or <code class="docutils literal"><span class="pre">'a'</span></code> for reading
664
(default), writing, exclusive creation or appending. The file will be
665
created if it doesn’t exist when opened for writing or appending; it will be
666
truncated when opened for writing. <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal"><span class="pre">FileExistsError</span></code></a> will be raised if
667
it already exists when opened for creating. Opening a file for creating
668
implies writing, so this mode behaves in a similar way to <code class="docutils literal"><span class="pre">'w'</span></code>. Add a
669
<code class="docutils literal"><span class="pre">'+'</span></code> to the mode to allow simultaneous reading and writing.</p>
670
<p>The <code class="xref py py-meth docutils literal"><span class="pre">read()</span></code> (when called with a positive argument), <code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code>
671
and <code class="xref py py-meth docutils literal"><span class="pre">write()</span></code> methods on this class will only make one system call.</p>
672
<p>A custom opener can be used by passing a callable as <em>opener</em>. The underlying
673
file descriptor for the file object is then obtained by calling <em>opener</em> with
674
(<em>name</em>, <em>flags</em>). <em>opener</em> must return an open file descriptor (passing
675
<a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-mod docutils literal"><span class="pre">os.open</span></code></a> as <em>opener</em> results in functionality similar to passing
676
<code class="docutils literal"><span class="pre">None</span></code>).</p>
677
<p>The newly created file is <a class="reference internal" href="os.html#fd-inheritance"><span>non-inheritable</span></a>.</p>
678
<p>See the <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> built-in function for examples on using the <em>opener</em>
680
<div class="versionchanged">
681
<p><span class="versionmodified">Changed in version 3.3: </span>The <em>opener</em> parameter was added.
682
The <code class="docutils literal"><span class="pre">'x'</span></code> mode was added.</p>
684
<div class="versionchanged">
685
<p><span class="versionmodified">Changed in version 3.4: </span>The file is now non-inheritable.</p>
687
<p>In addition to the attributes and methods from <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a> and
688
<a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a>, <a class="reference internal" href="#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal"><span class="pre">FileIO</span></code></a> provides the following data
690
<dl class="attribute">
691
<dt id="io.FileIO.mode">
692
<code class="descname">mode</code><a class="headerlink" href="#io.FileIO.mode" title="Permalink to this definition">¶</a></dt>
693
<dd><p>The mode as given in the constructor.</p>
696
<dl class="attribute">
697
<dt id="io.FileIO.name">
698
<code class="descname">name</code><a class="headerlink" href="#io.FileIO.name" title="Permalink to this definition">¶</a></dt>
699
<dd><p>The file name. This is the file descriptor of the file when no name is
700
given in the constructor.</p>
706
<div class="section" id="buffered-streams">
707
<h3>16.2.3.3. Buffered Streams<a class="headerlink" href="#buffered-streams" title="Permalink to this headline">¶</a></h3>
708
<p>Buffered I/O streams provide a higher-level interface to an I/O device
709
than raw I/O does.</p>
712
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">BytesIO</code><span class="sig-paren">(</span><span class="optional">[</span><em>initial_bytes</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO" title="Permalink to this definition">¶</a></dt>
713
<dd><p>A stream implementation using an in-memory bytes buffer. It inherits
714
<a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a>. The buffer is discarded when the
715
<a class="reference internal" href="#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal"><span class="pre">close()</span></code></a> method is called.</p>
716
<p>The optional argument <em>initial_bytes</em> is a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> that
717
contains initial data.</p>
718
<p><a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">BytesIO</span></code></a> provides or overrides these methods in addition to those
719
from <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> and <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>:</p>
721
<dt id="io.BytesIO.getbuffer">
722
<code class="descname">getbuffer</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO.getbuffer" title="Permalink to this definition">¶</a></dt>
723
<dd><p>Return a readable and writable view over the contents of the buffer
724
without copying them. Also, mutating the view will transparently
725
update the contents of the buffer:</p>
726
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">BytesIO</span><span class="p">(</span><span class="n">b</span><span class="s2">"abcdef"</span><span class="p">)</span>
727
<span class="gp">>>> </span><span class="n">view</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">getbuffer</span><span class="p">()</span>
728
<span class="gp">>>> </span><span class="n">view</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="n">b</span><span class="s2">"56"</span>
729
<span class="gp">>>> </span><span class="n">b</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
730
<span class="go">b'ab56ef'</span>
733
<div class="admonition note">
734
<p class="first admonition-title">Note</p>
735
<p class="last">As long as the view exists, the <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">BytesIO</span></code></a> object cannot be
736
resized or closed.</p>
738
<div class="versionadded">
739
<p><span class="versionmodified">New in version 3.2.</span></p>
744
<dt id="io.BytesIO.getvalue">
745
<code class="descname">getvalue</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO.getvalue" title="Permalink to this definition">¶</a></dt>
746
<dd><p>Return <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> containing the entire contents of the buffer.</p>
750
<dt id="io.BytesIO.read1">
751
<code class="descname">read1</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO.read1" title="Permalink to this definition">¶</a></dt>
752
<dd><p>In <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">BytesIO</span></code></a>, this is the same as <code class="xref py py-meth docutils literal"><span class="pre">read()</span></code>.</p>
756
<dt id="io.BytesIO.readinto1">
757
<code class="descname">readinto1</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO.readinto1" title="Permalink to this definition">¶</a></dt>
758
<dd><p>In <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">BytesIO</span></code></a>, this is the same as <code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code>.</p>
759
<div class="versionadded">
760
<p><span class="versionmodified">New in version 3.5.</span></p>
767
<dt id="io.BufferedReader">
768
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">BufferedReader</code><span class="sig-paren">(</span><em>raw</em>, <em>buffer_size=DEFAULT_BUFFER_SIZE</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedReader" title="Permalink to this definition">¶</a></dt>
769
<dd><p>A buffer providing higher-level access to a readable, sequential
770
<a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> object. It inherits <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a>.
771
When reading data from this object, a larger amount of data may be
772
requested from the underlying raw stream, and kept in an internal buffer.
773
The buffered data can then be returned directly on subsequent reads.</p>
774
<p>The constructor creates a <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">BufferedReader</span></code></a> for the given readable
775
<em>raw</em> stream and <em>buffer_size</em>. If <em>buffer_size</em> is omitted,
776
<a class="reference internal" href="#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><code class="xref py py-data docutils literal"><span class="pre">DEFAULT_BUFFER_SIZE</span></code></a> is used.</p>
777
<p><a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">BufferedReader</span></code></a> provides or overrides these methods in addition to
778
those from <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> and <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>:</p>
780
<dt id="io.BufferedReader.peek">
781
<code class="descname">peek</code><span class="sig-paren">(</span><span class="optional">[</span><em>size</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedReader.peek" title="Permalink to this definition">¶</a></dt>
782
<dd><p>Return bytes from the stream without advancing the position. At most one
783
single read on the raw stream is done to satisfy the call. The number of
784
bytes returned may be less or more than requested.</p>
788
<dt id="io.BufferedReader.read">
789
<code class="descname">read</code><span class="sig-paren">(</span><span class="optional">[</span><em>size</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedReader.read" title="Permalink to this definition">¶</a></dt>
790
<dd><p>Read and return <em>size</em> bytes, or if <em>size</em> is not given or negative, until
791
EOF or if the read call would block in non-blocking mode.</p>
795
<dt id="io.BufferedReader.read1">
796
<code class="descname">read1</code><span class="sig-paren">(</span><em>size</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedReader.read1" title="Permalink to this definition">¶</a></dt>
797
<dd><p>Read and return up to <em>size</em> bytes with only one call on the raw stream.
798
If at least one byte is buffered, only buffered bytes are returned.
799
Otherwise, one raw stream read call is made.</p>
805
<dt id="io.BufferedWriter">
806
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">BufferedWriter</code><span class="sig-paren">(</span><em>raw</em>, <em>buffer_size=DEFAULT_BUFFER_SIZE</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedWriter" title="Permalink to this definition">¶</a></dt>
807
<dd><p>A buffer providing higher-level access to a writeable, sequential
808
<a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> object. It inherits <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a>.
809
When writing to this object, data is normally placed into an internal
810
buffer. The buffer will be written out to the underlying <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a>
811
object under various conditions, including:</p>
813
<li>when the buffer gets too small for all pending data;</li>
814
<li>when <a class="reference internal" href="#io.BufferedWriter.flush" title="io.BufferedWriter.flush"><code class="xref py py-meth docutils literal"><span class="pre">flush()</span></code></a> is called;</li>
815
<li>when a <code class="xref py py-meth docutils literal"><span class="pre">seek()</span></code> is requested (for <a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal"><span class="pre">BufferedRandom</span></code></a> objects);</li>
816
<li>when the <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></code></a> object is closed or destroyed.</li>
818
<p>The constructor creates a <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></code></a> for the given writeable
819
<em>raw</em> stream. If the <em>buffer_size</em> is not given, it defaults to
820
<a class="reference internal" href="#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><code class="xref py py-data docutils literal"><span class="pre">DEFAULT_BUFFER_SIZE</span></code></a>.</p>
821
<p><a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></code></a> provides or overrides these methods in addition to
822
those from <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> and <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>:</p>
824
<dt id="io.BufferedWriter.flush">
825
<code class="descname">flush</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedWriter.flush" title="Permalink to this definition">¶</a></dt>
826
<dd><p>Force bytes held in the buffer into the raw stream. A
827
<a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal"><span class="pre">BlockingIOError</span></code></a> should be raised if the raw stream blocks.</p>
831
<dt id="io.BufferedWriter.write">
832
<code class="descname">write</code><span class="sig-paren">(</span><em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedWriter.write" title="Permalink to this definition">¶</a></dt>
833
<dd><p>Write the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>, <em>b</em>, and return the
834
number of bytes written. When in non-blocking mode, a
835
<a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal"><span class="pre">BlockingIOError</span></code></a> is raised if the buffer needs to be written out but
836
the raw stream blocks.</p>
842
<dt id="io.BufferedRandom">
843
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">BufferedRandom</code><span class="sig-paren">(</span><em>raw</em>, <em>buffer_size=DEFAULT_BUFFER_SIZE</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedRandom" title="Permalink to this definition">¶</a></dt>
844
<dd><p>A buffered interface to random access streams. It inherits
845
<a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">BufferedReader</span></code></a> and <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></code></a>, and further supports
846
<code class="xref py py-meth docutils literal"><span class="pre">seek()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">tell()</span></code> functionality.</p>
847
<p>The constructor creates a reader and writer for a seekable raw stream, given
848
in the first argument. If the <em>buffer_size</em> is omitted it defaults to
849
<a class="reference internal" href="#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><code class="xref py py-data docutils literal"><span class="pre">DEFAULT_BUFFER_SIZE</span></code></a>.</p>
850
<p><a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal"><span class="pre">BufferedRandom</span></code></a> is capable of anything <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">BufferedReader</span></code></a> or
851
<a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></code></a> can do.</p>
855
<dt id="io.BufferedRWPair">
856
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">BufferedRWPair</code><span class="sig-paren">(</span><em>reader</em>, <em>writer</em>, <em>buffer_size=DEFAULT_BUFFER_SIZE</em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedRWPair" title="Permalink to this definition">¶</a></dt>
857
<dd><p>A buffered I/O object combining two unidirectional <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a>
858
objects – one readable, the other writeable – into a single bidirectional
859
endpoint. It inherits <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a>.</p>
860
<p><em>reader</em> and <em>writer</em> are <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></code></a> objects that are readable and
861
writeable respectively. If the <em>buffer_size</em> is omitted it defaults to
862
<a class="reference internal" href="#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><code class="xref py py-data docutils literal"><span class="pre">DEFAULT_BUFFER_SIZE</span></code></a>.</p>
863
<p><a class="reference internal" href="#io.BufferedRWPair" title="io.BufferedRWPair"><code class="xref py py-class docutils literal"><span class="pre">BufferedRWPair</span></code></a> implements all of <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a>‘s methods
864
except for <a class="reference internal" href="#io.BufferedIOBase.detach" title="io.BufferedIOBase.detach"><code class="xref py py-meth docutils literal"><span class="pre">detach()</span></code></a>, which raises
865
<a class="reference internal" href="#io.UnsupportedOperation" title="io.UnsupportedOperation"><code class="xref py py-exc docutils literal"><span class="pre">UnsupportedOperation</span></code></a>.</p>
866
<div class="admonition warning">
867
<p class="first admonition-title">Warning</p>
868
<p class="last"><a class="reference internal" href="#io.BufferedRWPair" title="io.BufferedRWPair"><code class="xref py py-class docutils literal"><span class="pre">BufferedRWPair</span></code></a> does not attempt to synchronize accesses to
869
its underlying raw streams. You should not pass it the same object
870
as reader and writer; use <a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal"><span class="pre">BufferedRandom</span></code></a> instead.</p>
875
<div class="section" id="id1">
876
<h3>16.2.3.4. Text I/O<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
878
<dt id="io.TextIOBase">
879
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">TextIOBase</code><a class="headerlink" href="#io.TextIOBase" title="Permalink to this definition">¶</a></dt>
880
<dd><p>Base class for text streams. This class provides a character and line based
881
interface to stream I/O. There is no <code class="xref py py-meth docutils literal"><span class="pre">readinto()</span></code> method because
882
Python’s character strings are immutable. It inherits <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>.
883
There is no public constructor.</p>
884
<p><a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a> provides or overrides these data attributes and
885
methods in addition to those from <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal"><span class="pre">IOBase</span></code></a>:</p>
886
<dl class="attribute">
887
<dt id="io.TextIOBase.encoding">
888
<code class="descname">encoding</code><a class="headerlink" href="#io.TextIOBase.encoding" title="Permalink to this definition">¶</a></dt>
889
<dd><p>The name of the encoding used to decode the stream’s bytes into
890
strings, and to encode strings into bytes.</p>
893
<dl class="attribute">
894
<dt id="io.TextIOBase.errors">
895
<code class="descname">errors</code><a class="headerlink" href="#io.TextIOBase.errors" title="Permalink to this definition">¶</a></dt>
896
<dd><p>The error setting of the decoder or encoder.</p>
899
<dl class="attribute">
900
<dt id="io.TextIOBase.newlines">
901
<code class="descname">newlines</code><a class="headerlink" href="#io.TextIOBase.newlines" title="Permalink to this definition">¶</a></dt>
902
<dd><p>A string, a tuple of strings, or <code class="docutils literal"><span class="pre">None</span></code>, indicating the newlines
903
translated so far. Depending on the implementation and the initial
904
constructor flags, this may not be available.</p>
907
<dl class="attribute">
908
<dt id="io.TextIOBase.buffer">
909
<code class="descname">buffer</code><a class="headerlink" href="#io.TextIOBase.buffer" title="Permalink to this definition">¶</a></dt>
910
<dd><p>The underlying binary buffer (a <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> instance) that
911
<a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a> deals with. This is not part of the
912
<a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a> API and may not exist in some implementations.</p>
916
<dt id="io.TextIOBase.detach">
917
<code class="descname">detach</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.detach" title="Permalink to this definition">¶</a></dt>
918
<dd><p>Separate the underlying binary buffer from the <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a> and
920
<p>After the underlying buffer has been detached, the <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a> is
921
in an unusable state.</p>
922
<p>Some <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a> implementations, like <a class="reference internal" href="#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal"><span class="pre">StringIO</span></code></a>, may not
923
have the concept of an underlying buffer and calling this method will
924
raise <a class="reference internal" href="#io.UnsupportedOperation" title="io.UnsupportedOperation"><code class="xref py py-exc docutils literal"><span class="pre">UnsupportedOperation</span></code></a>.</p>
925
<div class="versionadded">
926
<p><span class="versionmodified">New in version 3.1.</span></p>
931
<dt id="io.TextIOBase.read">
932
<code class="descname">read</code><span class="sig-paren">(</span><em>size</em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.read" title="Permalink to this definition">¶</a></dt>
933
<dd><p>Read and return at most <em>size</em> characters from the stream as a single
934
<a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a>. If <em>size</em> is negative or <code class="docutils literal"><span class="pre">None</span></code>, reads until EOF.</p>
938
<dt id="io.TextIOBase.readline">
939
<code class="descname">readline</code><span class="sig-paren">(</span><em>size=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.readline" title="Permalink to this definition">¶</a></dt>
940
<dd><p>Read until newline or EOF and return a single <code class="docutils literal"><span class="pre">str</span></code>. If the stream is
941
already at EOF, an empty string is returned.</p>
942
<p>If <em>size</em> is specified, at most <em>size</em> characters will be read.</p>
946
<dt id="io.TextIOBase.seek">
947
<code class="descname">seek</code><span class="sig-paren">(</span><em>offset</em><span class="optional">[</span>, <em>whence</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.seek" title="Permalink to this definition">¶</a></dt>
948
<dd><p>Change the stream position to the given <em>offset</em>. Behaviour depends on
949
the <em>whence</em> parameter. The default value for <em>whence</em> is
950
<code class="xref py py-data docutils literal"><span class="pre">SEEK_SET</span></code>.</p>
952
<li><code class="xref py py-data docutils literal"><span class="pre">SEEK_SET</span></code> or <code class="docutils literal"><span class="pre">0</span></code>: seek from the start of the stream
953
(the default); <em>offset</em> must either be a number returned by
954
<a class="reference internal" href="#io.TextIOBase.tell" title="io.TextIOBase.tell"><code class="xref py py-meth docutils literal"><span class="pre">TextIOBase.tell()</span></code></a>, or zero. Any other <em>offset</em> value
955
produces undefined behaviour.</li>
956
<li><code class="xref py py-data docutils literal"><span class="pre">SEEK_CUR</span></code> or <code class="docutils literal"><span class="pre">1</span></code>: “seek” to the current position;
957
<em>offset</em> must be zero, which is a no-operation (all other values
958
are unsupported).</li>
959
<li><code class="xref py py-data docutils literal"><span class="pre">SEEK_END</span></code> or <code class="docutils literal"><span class="pre">2</span></code>: seek to the end of the stream;
960
<em>offset</em> must be zero (all other values are unsupported).</li>
962
<p>Return the new absolute position as an opaque number.</p>
963
<div class="versionadded">
964
<p><span class="versionmodified">New in version 3.1: </span>The <code class="docutils literal"><span class="pre">SEEK_*</span></code> constants.</p>
969
<dt id="io.TextIOBase.tell">
970
<code class="descname">tell</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.tell" title="Permalink to this definition">¶</a></dt>
971
<dd><p>Return the current stream position as an opaque number. The number
972
does not usually represent a number of bytes in the underlying
977
<dt id="io.TextIOBase.write">
978
<code class="descname">write</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.write" title="Permalink to this definition">¶</a></dt>
979
<dd><p>Write the string <em>s</em> to the stream and return the number of characters
986
<dt id="io.TextIOWrapper">
987
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">TextIOWrapper</code><span class="sig-paren">(</span><em>buffer</em>, <em>encoding=None</em>, <em>errors=None</em>, <em>newline=None</em>, <em>line_buffering=False</em>, <em>write_through=False</em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOWrapper" title="Permalink to this definition">¶</a></dt>
988
<dd><p>A buffered text stream over a <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></code></a> binary stream.
989
It inherits <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a>.</p>
990
<p><em>encoding</em> gives the name of the encoding that the stream will be decoded or
991
encoded with. It defaults to
992
<a class="reference internal" href="locale.html#locale.getpreferredencoding" title="locale.getpreferredencoding"><code class="xref py py-func docutils literal"><span class="pre">locale.getpreferredencoding(False)</span></code></a>.</p>
993
<p><em>errors</em> is an optional string that specifies how encoding and decoding
994
errors are to be handled. Pass <code class="docutils literal"><span class="pre">'strict'</span></code> to raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a>
995
exception if there is an encoding error (the default of <code class="docutils literal"><span class="pre">None</span></code> has the same
996
effect), or pass <code class="docutils literal"><span class="pre">'ignore'</span></code> to ignore errors. (Note that ignoring encoding
997
errors can lead to data loss.) <code class="docutils literal"><span class="pre">'replace'</span></code> causes a replacement marker
998
(such as <code class="docutils literal"><span class="pre">'?'</span></code>) to be inserted where there is malformed data.
999
<code class="docutils literal"><span class="pre">'backslashreplace'</span></code> causes malformed data to be replaced by a
1000
backslashed escape sequence. When writing, <code class="docutils literal"><span class="pre">'xmlcharrefreplace'</span></code>
1001
(replace with the appropriate XML character reference) or <code class="docutils literal"><span class="pre">'namereplace'</span></code>
1002
(replace with <code class="docutils literal"><span class="pre">\N{...}</span></code> escape sequences) can be used. Any other error
1003
handling name that has been registered with
1004
<a class="reference internal" href="codecs.html#codecs.register_error" title="codecs.register_error"><code class="xref py py-func docutils literal"><span class="pre">codecs.register_error()</span></code></a> is also valid.</p>
1005
<p id="index-1"><em>newline</em> controls how line endings are handled. It can be <code class="docutils literal"><span class="pre">None</span></code>,
1006
<code class="docutils literal"><span class="pre">''</span></code>, <code class="docutils literal"><span class="pre">'\n'</span></code>, <code class="docutils literal"><span class="pre">'\r'</span></code>, and <code class="docutils literal"><span class="pre">'\r\n'</span></code>. It works as follows:</p>
1008
<li>When reading input from the stream, if <em>newline</em> is <code class="docutils literal"><span class="pre">None</span></code>,
1009
<a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a> mode is enabled. Lines in the input can end in
1010
<code class="docutils literal"><span class="pre">'\n'</span></code>, <code class="docutils literal"><span class="pre">'\r'</span></code>, or <code class="docutils literal"><span class="pre">'\r\n'</span></code>, and these are translated into <code class="docutils literal"><span class="pre">'\n'</span></code>
1011
before being returned to the caller. If it is <code class="docutils literal"><span class="pre">''</span></code>, universal newlines
1012
mode is enabled, but line endings are returned to the caller untranslated.
1013
If it has any of the other legal values, input lines are only terminated
1014
by the given string, and the line ending is returned to the caller
1016
<li>When writing output to the stream, if <em>newline</em> is <code class="docutils literal"><span class="pre">None</span></code>, any <code class="docutils literal"><span class="pre">'\n'</span></code>
1017
characters written are translated to the system default line separator,
1018
<a class="reference internal" href="os.html#os.linesep" title="os.linesep"><code class="xref py py-data docutils literal"><span class="pre">os.linesep</span></code></a>. If <em>newline</em> is <code class="docutils literal"><span class="pre">''</span></code> or <code class="docutils literal"><span class="pre">'\n'</span></code>, no translation
1019
takes place. If <em>newline</em> is any of the other legal values, any <code class="docutils literal"><span class="pre">'\n'</span></code>
1020
characters written are translated to the given string.</li>
1022
<p>If <em>line_buffering</em> is <code class="docutils literal"><span class="pre">True</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">flush()</span></code> is implied when a call to
1023
write contains a newline character.</p>
1024
<p>If <em>write_through</em> is <code class="docutils literal"><span class="pre">True</span></code>, calls to <code class="xref py py-meth docutils literal"><span class="pre">write()</span></code> are guaranteed
1025
not to be buffered: any data written on the <a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextIOWrapper</span></code></a>
1026
object is immediately handled to its underlying binary <em>buffer</em>.</p>
1027
<div class="versionchanged">
1028
<p><span class="versionmodified">Changed in version 3.3: </span>The <em>write_through</em> argument has been added.</p>
1030
<div class="versionchanged">
1031
<p><span class="versionmodified">Changed in version 3.3: </span>The default <em>encoding</em> is now <code class="docutils literal"><span class="pre">locale.getpreferredencoding(False)</span></code>
1032
instead of <code class="docutils literal"><span class="pre">locale.getpreferredencoding()</span></code>. Don’t change temporary the
1033
locale encoding using <a class="reference internal" href="locale.html#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal"><span class="pre">locale.setlocale()</span></code></a>, use the current locale
1034
encoding instead of the user preferred encoding.</p>
1036
<p><a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextIOWrapper</span></code></a> provides one attribute in addition to those of
1037
<a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a> and its parents:</p>
1038
<dl class="attribute">
1039
<dt id="io.TextIOWrapper.line_buffering">
1040
<code class="descname">line_buffering</code><a class="headerlink" href="#io.TextIOWrapper.line_buffering" title="Permalink to this definition">¶</a></dt>
1041
<dd><p>Whether line buffering is enabled.</p>
1047
<dt id="io.StringIO">
1048
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">StringIO</code><span class="sig-paren">(</span><em>initial_value=''</em>, <em>newline='\n'</em><span class="sig-paren">)</span><a class="headerlink" href="#io.StringIO" title="Permalink to this definition">¶</a></dt>
1049
<dd><p>An in-memory stream for text I/O. The text buffer is discarded when the
1050
<a class="reference internal" href="#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal"><span class="pre">close()</span></code></a> method is called.</p>
1051
<p>The initial value of the buffer can be set by providing <em>initial_value</em>.
1052
If newline translation is enabled, newlines will be encoded as if by
1053
<a class="reference internal" href="#io.TextIOBase.write" title="io.TextIOBase.write"><code class="xref py py-meth docutils literal"><span class="pre">write()</span></code></a>. The stream is positioned at the start of
1055
<p>The <em>newline</em> argument works like that of <a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextIOWrapper</span></code></a>.
1056
The default is to consider only <code class="docutils literal"><span class="pre">\n</span></code> characters as ends of lines and
1057
to do no newline translation. If <em>newline</em> is set to <code class="docutils literal"><span class="pre">None</span></code>,
1058
newlines are written as <code class="docutils literal"><span class="pre">\n</span></code> on all platforms, but universal
1059
newline decoding is still performed when reading.</p>
1060
<p><a class="reference internal" href="#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal"><span class="pre">StringIO</span></code></a> provides this method in addition to those from
1061
<a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></code></a> and its parents:</p>
1063
<dt id="io.StringIO.getvalue">
1064
<code class="descname">getvalue</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.StringIO.getvalue" title="Permalink to this definition">¶</a></dt>
1065
<dd><p>Return a <code class="docutils literal"><span class="pre">str</span></code> containing the entire contents of the buffer.
1066
Newlines are decoded as if by <a class="reference internal" href="#io.TextIOBase.read" title="io.TextIOBase.read"><code class="xref py py-meth docutils literal"><span class="pre">read()</span></code></a>, although
1067
the stream position is not changed.</p>
1070
<p>Example usage:</p>
1071
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">io</span>
1073
<span class="n">output</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">StringIO</span><span class="p">()</span>
1074
<span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'First line.</span><span class="se">\n</span><span class="s1">'</span><span class="p">)</span>
1075
<span class="nb">print</span><span class="p">(</span><span class="s1">'Second line.'</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">output</span><span class="p">)</span>
1077
<span class="c1"># Retrieve file contents -- this will be</span>
1078
<span class="c1"># 'First line.\nSecond line.\n'</span>
1079
<span class="n">contents</span> <span class="o">=</span> <span class="n">output</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
1081
<span class="c1"># Close object and discard memory buffer --</span>
1082
<span class="c1"># .getvalue() will now raise an exception.</span>
1083
<span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
1088
<span class="target" id="index-2"></span><dl class="class">
1089
<dt id="io.IncrementalNewlineDecoder">
1090
<em class="property">class </em><code class="descclassname">io.</code><code class="descname">IncrementalNewlineDecoder</code><a class="headerlink" href="#io.IncrementalNewlineDecoder" title="Permalink to this definition">¶</a></dt>
1091
<dd><p>A helper codec that decodes newlines for <a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a> mode.
1092
It inherits <a class="reference internal" href="codecs.html#codecs.IncrementalDecoder" title="codecs.IncrementalDecoder"><code class="xref py py-class docutils literal"><span class="pre">codecs.IncrementalDecoder</span></code></a>.</p>
1097
<div class="section" id="performance">
1098
<h2>16.2.4. Performance<a class="headerlink" href="#performance" title="Permalink to this headline">¶</a></h2>
1099
<p>This section discusses the performance of the provided concrete I/O
1100
implementations.</p>
1101
<div class="section" id="id2">
1102
<h3>16.2.4.1. Binary I/O<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
1103
<p>By reading and writing only large chunks of data even when the user asks for a
1104
single byte, buffered I/O hides any inefficiency in calling and executing the
1105
operating system’s unbuffered I/O routines. The gain depends on the OS and the
1106
kind of I/O which is performed. For example, on some modern OSes such as Linux,
1107
unbuffered disk I/O can be as fast as buffered I/O. The bottom line, however,
1108
is that buffered I/O offers predictable performance regardless of the platform
1109
and the backing device. Therefore, it is almost always preferable to use
1110
buffered I/O rather than unbuffered I/O for binary data.</p>
1112
<div class="section" id="id3">
1113
<h3>16.2.4.2. Text I/O<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
1114
<p>Text I/O over a binary storage (such as a file) is significantly slower than
1115
binary I/O over the same storage, because it requires conversions between
1116
unicode and binary data using a character codec. This can become noticeable
1117
handling huge amounts of text data like large log files. Also,
1118
<code class="xref py py-meth docutils literal"><span class="pre">TextIOWrapper.tell()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">TextIOWrapper.seek()</span></code> are both quite slow
1119
due to the reconstruction algorithm used.</p>
1120
<p><a class="reference internal" href="#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal"><span class="pre">StringIO</span></code></a>, however, is a native in-memory unicode container and will
1121
exhibit similar speed to <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">BytesIO</span></code></a>.</p>
1123
<div class="section" id="multi-threading">
1124
<h3>16.2.4.3. Multi-threading<a class="headerlink" href="#multi-threading" title="Permalink to this headline">¶</a></h3>
1125
<p><a class="reference internal" href="#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal"><span class="pre">FileIO</span></code></a> objects are thread-safe to the extent that the operating system
1126
calls (such as <code class="docutils literal"><span class="pre">read(2)</span></code> under Unix) they wrap are thread-safe too.</p>
1127
<p>Binary buffered objects (instances of <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">BufferedReader</span></code></a>,
1128
<a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></code></a>, <a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal"><span class="pre">BufferedRandom</span></code></a> and <a class="reference internal" href="#io.BufferedRWPair" title="io.BufferedRWPair"><code class="xref py py-class docutils literal"><span class="pre">BufferedRWPair</span></code></a>)
1129
protect their internal structures using a lock; it is therefore safe to call
1130
them from multiple threads at once.</p>
1131
<p><a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextIOWrapper</span></code></a> objects are not thread-safe.</p>
1133
<div class="section" id="reentrancy">
1134
<h3>16.2.4.4. Reentrancy<a class="headerlink" href="#reentrancy" title="Permalink to this headline">¶</a></h3>
1135
<p>Binary buffered objects (instances of <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">BufferedReader</span></code></a>,
1136
<a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></code></a>, <a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal"><span class="pre">BufferedRandom</span></code></a> and <a class="reference internal" href="#io.BufferedRWPair" title="io.BufferedRWPair"><code class="xref py py-class docutils literal"><span class="pre">BufferedRWPair</span></code></a>)
1137
are not reentrant. While reentrant calls will not happen in normal situations,
1138
they can arise from doing I/O in a <a class="reference internal" href="signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal"><span class="pre">signal</span></code></a> handler. If a thread tries to
1139
re-enter a buffered object which it is already accessing, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></code></a>
1140
is raised. Note this doesn’t prohibit a different thread from entering the
1141
buffered object.</p>
1142
<p>The above implicitly extends to text files, since the <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> function
1143
will wrap a buffered object inside a <a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextIOWrapper</span></code></a>. This includes
1144
standard streams and therefore affects the built-in function <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal"><span class="pre">print()</span></code></a> as
1154
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
1155
<div class="sphinxsidebarwrapper">
1156
<h3><a href="../contents.html">Table Of Contents</a></h3>
1158
<li><a class="reference internal" href="#">16.2. <code class="docutils literal"><span class="pre">io</span></code> — Core tools for working with streams</a><ul>
1159
<li><a class="reference internal" href="#overview">16.2.1. Overview</a><ul>
1160
<li><a class="reference internal" href="#text-i-o">16.2.1.1. Text I/O</a></li>
1161
<li><a class="reference internal" href="#binary-i-o">16.2.1.2. Binary I/O</a></li>
1162
<li><a class="reference internal" href="#raw-i-o">16.2.1.3. Raw I/O</a></li>
1165
<li><a class="reference internal" href="#high-level-module-interface">16.2.2. High-level Module Interface</a><ul>
1166
<li><a class="reference internal" href="#in-memory-streams">16.2.2.1. In-memory streams</a></li>
1169
<li><a class="reference internal" href="#class-hierarchy">16.2.3. Class hierarchy</a><ul>
1170
<li><a class="reference internal" href="#i-o-base-classes">16.2.3.1. I/O Base Classes</a></li>
1171
<li><a class="reference internal" href="#raw-file-i-o">16.2.3.2. Raw File I/O</a></li>
1172
<li><a class="reference internal" href="#buffered-streams">16.2.3.3. Buffered Streams</a></li>
1173
<li><a class="reference internal" href="#id1">16.2.3.4. Text I/O</a></li>
1176
<li><a class="reference internal" href="#performance">16.2.4. Performance</a><ul>
1177
<li><a class="reference internal" href="#id2">16.2.4.1. Binary I/O</a></li>
1178
<li><a class="reference internal" href="#id3">16.2.4.2. Text I/O</a></li>
1179
<li><a class="reference internal" href="#multi-threading">16.2.4.3. Multi-threading</a></li>
1180
<li><a class="reference internal" href="#reentrancy">16.2.4.4. Reentrancy</a></li>
1187
<h4>Previous topic</h4>
1188
<p class="topless"><a href="os.html"
1189
title="previous chapter">16.1. <code class="docutils literal"><span class="pre">os</span></code> — Miscellaneous operating system interfaces</a></p>
1191
<p class="topless"><a href="time.html"
1192
title="next chapter">16.3. <code class="docutils literal"><span class="pre">time</span></code> — Time access and conversions</a></p>
1193
<div role="note" aria-label="source link">
1195
<ul class="this-page-menu">
1196
<li><a href="../bugs.html">Report a Bug</a></li>
1197
<li><a href="../_sources/library/io.txt"
1198
rel="nofollow">Show Source</a></li>
1203
<div class="clearer"></div>
1205
<div class="related" role="navigation" aria-label="related navigation">
1208
<li class="right" style="margin-right: 10px">
1209
<a href="../genindex.html" title="General Index"
1212
<a href="../py-modindex.html" title="Python Module Index"
1215
<a href="time.html" title="16.3. time — Time access and conversions"
1218
<a href="os.html" title="16.1. os — Miscellaneous operating system interfaces"
1219
>previous</a> |</li>
1220
<li><img src="../_static/py.png" alt=""
1221
style="vertical-align: middle; margin-top: -1px"/></li>
1222
<li><a href="https://www.python.org/">Python</a> »</li>
1224
<span class="version_switcher_placeholder">3.5.2</span>
1225
<a href="../index.html">Documentation </a> »
1228
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
1229
<li class="nav-item nav-item-2"><a href="allos.html" >16. Generic Operating System Services</a> »</li>
1233
<div class="inline-search" style="display: none" role="search">
1234
<form class="inline-search" action="../search.html" method="get">
1235
<input placeholder="Quick search" type="text" name="q" />
1236
<input type="submit" value="Go" />
1237
<input type="hidden" name="check_keywords" value="yes" />
1238
<input type="hidden" name="area" value="default" />
1241
<script type="text/javascript">$('.inline-search').show(0);</script>
1247
<div class="footer">
1248
© <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
1250
The Python Software Foundation is a non-profit corporation.
1251
<a href="https://www.python.org/psf/donations/">Please donate.</a>
1253
Last updated on Oct 19, 2016.
1254
<a href="../bugs.html">Found a bug</a>?
1256
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'