gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1590 lines
162KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="io — Core tools for working with streams" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/io.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/io.py Overview: The io module provides Python’s main facilities for dealing with various types of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These ar..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/io.py Overview: The io module provides Python’s main facilities for dealing with various types of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These ar..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>io — Core tools for working with streams &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="time — Time access and conversions" href="time.html" />
  33. <link rel="prev" title="os — Miscellaneous operating system interfaces" href="os.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/io.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code> — Core tools for working with streams</a><ul>
  86. <li><a class="reference internal" href="#overview">Overview</a><ul>
  87. <li><a class="reference internal" href="#text-i-o">Text I/O</a></li>
  88. <li><a class="reference internal" href="#binary-i-o">Binary I/O</a></li>
  89. <li><a class="reference internal" href="#raw-i-o">Raw I/O</a></li>
  90. </ul>
  91. </li>
  92. <li><a class="reference internal" href="#text-encoding">Text Encoding</a><ul>
  93. <li><a class="reference internal" href="#opt-in-encodingwarning">Opt-in EncodingWarning</a></li>
  94. </ul>
  95. </li>
  96. <li><a class="reference internal" href="#high-level-module-interface">High-level Module Interface</a></li>
  97. <li><a class="reference internal" href="#class-hierarchy">Class hierarchy</a><ul>
  98. <li><a class="reference internal" href="#i-o-base-classes">I/O Base Classes</a></li>
  99. <li><a class="reference internal" href="#raw-file-i-o">Raw File I/O</a></li>
  100. <li><a class="reference internal" href="#buffered-streams">Buffered Streams</a></li>
  101. <li><a class="reference internal" href="#id1">Text I/O</a></li>
  102. </ul>
  103. </li>
  104. <li><a class="reference internal" href="#performance">Performance</a><ul>
  105. <li><a class="reference internal" href="#id2">Binary I/O</a></li>
  106. <li><a class="reference internal" href="#id3">Text I/O</a></li>
  107. <li><a class="reference internal" href="#multi-threading">Multi-threading</a></li>
  108. <li><a class="reference internal" href="#reentrancy">Reentrancy</a></li>
  109. </ul>
  110. </li>
  111. </ul>
  112. </li>
  113. </ul>
  114. </div>
  115. <div>
  116. <h4>Previous topic</h4>
  117. <p class="topless"><a href="os.html"
  118. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code> — Miscellaneous operating system interfaces</a></p>
  119. </div>
  120. <div>
  121. <h4>Next topic</h4>
  122. <p class="topless"><a href="time.html"
  123. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code> — Time access and conversions</a></p>
  124. </div>
  125. <div role="note" aria-label="source link">
  126. <h3>This Page</h3>
  127. <ul class="this-page-menu">
  128. <li><a href="../bugs.html">Report a Bug</a></li>
  129. <li>
  130. <a href="https://github.com/python/cpython/blob/main/Doc/library/io.rst"
  131. rel="nofollow">Show Source
  132. </a>
  133. </li>
  134. </ul>
  135. </div>
  136. </nav>
  137. </div>
  138. </div>
  139. <div class="related" role="navigation" aria-label="related navigation">
  140. <h3>Navigation</h3>
  141. <ul>
  142. <li class="right" style="margin-right: 10px">
  143. <a href="../genindex.html" title="General Index"
  144. accesskey="I">index</a></li>
  145. <li class="right" >
  146. <a href="../py-modindex.html" title="Python Module Index"
  147. >modules</a> |</li>
  148. <li class="right" >
  149. <a href="time.html" title="time — Time access and conversions"
  150. accesskey="N">next</a> |</li>
  151. <li class="right" >
  152. <a href="os.html" title="os — Miscellaneous operating system interfaces"
  153. accesskey="P">previous</a> |</li>
  154. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  155. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  156. <li class="switchers">
  157. <div class="language_switcher_placeholder"></div>
  158. <div class="version_switcher_placeholder"></div>
  159. </li>
  160. <li>
  161. </li>
  162. <li id="cpython-language-and-version">
  163. <a href="../index.html">3.12.3 Documentation</a> &#187;
  164. </li>
  165. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  166. <li class="nav-item nav-item-2"><a href="allos.html" accesskey="U">Generic Operating System Services</a> &#187;</li>
  167. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code> — Core tools for working with streams</a></li>
  168. <li class="right">
  169. <div class="inline-search" role="search">
  170. <form class="inline-search" action="../search.html" method="get">
  171. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  172. <input type="submit" value="Go" />
  173. </form>
  174. </div>
  175. |
  176. </li>
  177. <li class="right">
  178. <label class="theme-selector-label">
  179. Theme
  180. <select class="theme-selector" oninput="activateTheme(this.value)">
  181. <option value="auto" selected>Auto</option>
  182. <option value="light">Light</option>
  183. <option value="dark">Dark</option>
  184. </select>
  185. </label> |</li>
  186. </ul>
  187. </div>
  188. <div class="document">
  189. <div class="documentwrapper">
  190. <div class="bodywrapper">
  191. <div class="body" role="main">
  192. <section id="module-io">
  193. <span id="io-core-tools-for-working-with-streams"></span><h1><a class="reference internal" href="#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> — Core tools for working with streams<a class="headerlink" href="#module-io" title="Link to this heading">¶</a></h1>
  194. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/io.py">Lib/io.py</a></p>
  195. <hr class="docutils" />
  196. <section id="overview">
  197. <span id="io-overview"></span><h2>Overview<a class="headerlink" href="#overview" title="Link to this heading">¶</a></h2>
  198. <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 notranslate"><span class="pre">io</span></code></a> module provides Python’s main facilities for dealing with various
  199. types of I/O. There are three main types of I/O: <em>text I/O</em>, <em>binary I/O</em>
  200. and <em>raw I/O</em>. These are generic categories, and various backing stores can
  201. be used for each of them. A concrete object belonging to any of these
  202. 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>
  203. and <em>file-like object</em>.</p>
  204. <p>Independent of its category, each concrete stream object will also have
  205. various capabilities: it can be read-only, write-only, or read-write. It can
  206. also allow arbitrary random access (seeking forwards or backwards to any
  207. location), or only sequential access (for example in the case of a socket or
  208. pipe).</p>
  209. <p>All streams are careful about the type of data you give to them. For example
  210. giving a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> object to the <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> method of a binary stream
  211. will raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. So will giving a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object to the
  212. <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> method of a text stream.</p>
  213. <div class="versionchanged">
  214. <p><span class="versionmodified changed">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 notranslate"><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 notranslate"><span class="pre">OSError</span></code></a>, since
  215. <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><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 notranslate"><span class="pre">OSError</span></code></a>.</p>
  216. </div>
  217. <section id="text-i-o">
  218. <h3>Text I/O<a class="headerlink" href="#text-i-o" title="Link to this heading">¶</a></h3>
  219. <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 notranslate"><span class="pre">str</span></code></a> objects. This means that whenever
  220. the backing store is natively made of bytes (such as in the case of a file),
  221. encoding and decoding of data is made transparently as well as optional
  222. translation of platform-specific newline characters.</p>
  223. <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 notranslate"><span class="pre">open()</span></code></a>, optionally
  224. specifying an encoding:</p>
  225. <div class="highlight-python3 notranslate"><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">&quot;myfile.txt&quot;</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span>
  226. </pre></div>
  227. </div>
  228. <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 notranslate"><span class="pre">StringIO</span></code></a> objects:</p>
  229. <div class="highlight-python3 notranslate"><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">&quot;some initial text data&quot;</span><span class="p">)</span>
  230. </pre></div>
  231. </div>
  232. <p>The text stream API is described in detail in the documentation of
  233. <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a>.</p>
  234. </section>
  235. <section id="binary-i-o">
  236. <h3>Binary I/O<a class="headerlink" href="#binary-i-o" title="Link to this heading">¶</a></h3>
  237. <p>Binary I/O (also called <em>buffered I/O</em>) expects
  238. <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="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>
  239. objects. No encoding, decoding, or newline translation is performed. This
  240. category of streams can be used for all kinds of non-text data, and also when
  241. manual control over the handling of text data is desired.</p>
  242. <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 notranslate"><span class="pre">open()</span></code></a> with <code class="docutils literal notranslate"><span class="pre">'b'</span></code> in
  243. the mode string:</p>
  244. <div class="highlight-python3 notranslate"><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">&quot;myfile.jpg&quot;</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span>
  245. </pre></div>
  246. </div>
  247. <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 notranslate"><span class="pre">BytesIO</span></code></a> objects:</p>
  248. <div class="highlight-python3 notranslate"><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="sa">b</span><span class="s2">&quot;some initial binary data: </span><span class="se">\x00\x01</span><span class="s2">&quot;</span><span class="p">)</span>
  249. </pre></div>
  250. </div>
  251. <p>The binary stream API is described in detail in the docs of
  252. <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a>.</p>
  253. <p>Other library modules may provide additional ways to create text or binary
  254. streams. See <a class="reference internal" href="socket.html#socket.socket.makefile" title="socket.socket.makefile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.socket.makefile()</span></code></a> for example.</p>
  255. </section>
  256. <section id="raw-i-o">
  257. <h3>Raw I/O<a class="headerlink" href="#raw-i-o" title="Link to this heading">¶</a></h3>
  258. <p>Raw I/O (also called <em>unbuffered I/O</em>) is generally used as a low-level
  259. building-block for binary and text streams; it is rarely useful to directly
  260. manipulate a raw stream from user code. Nevertheless, you can create a raw
  261. stream by opening a file in binary mode with buffering disabled:</p>
  262. <div class="highlight-python3 notranslate"><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">&quot;myfile.jpg&quot;</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">,</span> <span class="n">buffering</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
  263. </pre></div>
  264. </div>
  265. <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 notranslate"><span class="pre">RawIOBase</span></code></a>.</p>
  266. </section>
  267. </section>
  268. <section id="text-encoding">
  269. <span id="io-text-encoding"></span><h2>Text Encoding<a class="headerlink" href="#text-encoding" title="Link to this heading">¶</a></h2>
  270. <p>The default encoding of <a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a> and <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> is
  271. locale-specific (<a class="reference internal" href="locale.html#locale.getencoding" title="locale.getencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getencoding()</span></code></a>).</p>
  272. <p>However, many developers forget to specify the encoding when opening text files
  273. encoded in UTF-8 (e.g. JSON, TOML, Markdown, etc…) since most Unix
  274. platforms use UTF-8 locale by default. This causes bugs because the locale
  275. encoding is not UTF-8 for most Windows users. For example:</p>
  276. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># May not work on Windows when non-ASCII characters in the file.</span>
  277. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;README.md&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  278. <span class="n">long_description</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  279. </pre></div>
  280. </div>
  281. <p>Accordingly, it is highly recommended that you specify the encoding
  282. explicitly when opening text files. If you want to use UTF-8, pass
  283. <code class="docutils literal notranslate"><span class="pre">encoding=&quot;utf-8&quot;</span></code>. To use the current locale encoding,
  284. <code class="docutils literal notranslate"><span class="pre">encoding=&quot;locale&quot;</span></code> is supported since Python 3.10.</p>
  285. <div class="admonition seealso">
  286. <p class="admonition-title">See also</p>
  287. <dl class="simple">
  288. <dt><a class="reference internal" href="os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a></dt><dd><p>Python UTF-8 Mode can be used to change the default encoding to
  289. UTF-8 from locale-specific encoding.</p>
  290. </dd>
  291. <dt><span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0686/"><strong>PEP 686</strong></a></dt><dd><p>Python 3.15 will make <a class="reference internal" href="os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> default.</p>
  292. </dd>
  293. </dl>
  294. </div>
  295. <section id="opt-in-encodingwarning">
  296. <span id="io-encoding-warning"></span><h3>Opt-in EncodingWarning<a class="headerlink" href="#opt-in-encodingwarning" title="Link to this heading">¶</a></h3>
  297. <div class="versionadded">
  298. <p><span class="versionmodified added">New in version 3.10: </span>See <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0597/"><strong>PEP 597</strong></a> for more details.</p>
  299. </div>
  300. <p>To find where the default locale encoding is used, you can enable
  301. the <a class="reference internal" href="../using/cmdline.html#cmdoption-X"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span> <span class="pre">warn_default_encoding</span></code></a> command line option or set the
  302. <span class="target" id="index-3"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONWARNDEFAULTENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONWARNDEFAULTENCODING</span></code></a> environment variable, which will
  303. emit an <a class="reference internal" href="exceptions.html#EncodingWarning" title="EncodingWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EncodingWarning</span></code></a> when the default encoding is used.</p>
  304. <p>If you are providing an API that uses <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> or
  305. <a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a> and passes <code class="docutils literal notranslate"><span class="pre">encoding=None</span></code> as a parameter, you
  306. can use <a class="reference internal" href="#io.text_encoding" title="io.text_encoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">text_encoding()</span></code></a> so that callers of the API will emit an
  307. <a class="reference internal" href="exceptions.html#EncodingWarning" title="EncodingWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EncodingWarning</span></code></a> if they don’t pass an <code class="docutils literal notranslate"><span class="pre">encoding</span></code>. However,
  308. please consider using UTF-8 by default (i.e. <code class="docutils literal notranslate"><span class="pre">encoding=&quot;utf-8&quot;</span></code>) for
  309. new APIs.</p>
  310. </section>
  311. </section>
  312. <section id="high-level-module-interface">
  313. <h2>High-level Module Interface<a class="headerlink" href="#high-level-module-interface" title="Link to this heading">¶</a></h2>
  314. <dl class="py data">
  315. <dt class="sig sig-object py" id="io.DEFAULT_BUFFER_SIZE">
  316. <span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">DEFAULT_BUFFER_SIZE</span></span><a class="headerlink" href="#io.DEFAULT_BUFFER_SIZE" title="Link to this definition">¶</a></dt>
  317. <dd><p>An int containing the default buffer size used by the module’s buffered I/O
  318. classes. <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> uses the file’s blksize (as obtained by
  319. <a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a>) if possible.</p>
  320. </dd></dl>
  321. <dl class="py function">
  322. <dt class="sig sig-object py" id="io.open">
  323. <span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffering</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">closefd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">opener</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.open" title="Link to this definition">¶</a></dt>
  324. <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 notranslate"><span class="pre">open()</span></code></a> function.</p>
  325. <p class="audit-hook"><p>This function raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">open</span></code> with
  326. arguments <em>path</em>, <em>mode</em> and <em>flags</em>. The <em>mode</em> and <em>flags</em>
  327. arguments may have been modified or inferred from the original call.</p>
  328. </p>
  329. </dd></dl>
  330. <dl class="py function">
  331. <dt class="sig sig-object py" id="io.open_code">
  332. <span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">open_code</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.open_code" title="Link to this definition">¶</a></dt>
  333. <dd><p>Opens the provided file with mode <code class="docutils literal notranslate"><span class="pre">'rb'</span></code>. This function should be used
  334. when the intent is to treat the contents as executable code.</p>
  335. <p><em>path</em> should be a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> and an absolute path.</p>
  336. <p>The behavior of this function may be overridden by an earlier call to the
  337. <a class="reference internal" href="../c-api/file.html#c.PyFile_SetOpenCodeHook" title="PyFile_SetOpenCodeHook"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyFile_SetOpenCodeHook()</span></code></a>. However, assuming that <em>path</em> is a
  338. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> and an absolute path, <code class="docutils literal notranslate"><span class="pre">open_code(path)</span></code> should always behave
  339. the same as <code class="docutils literal notranslate"><span class="pre">open(path,</span> <span class="pre">'rb')</span></code>. Overriding the behavior is intended for
  340. additional validation or preprocessing of the file.</p>
  341. <div class="versionadded">
  342. <p><span class="versionmodified added">New in version 3.8.</span></p>
  343. </div>
  344. </dd></dl>
  345. <dl class="py function">
  346. <dt class="sig sig-object py" id="io.text_encoding">
  347. <span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">text_encoding</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stacklevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">2</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.text_encoding" title="Link to this definition">¶</a></dt>
  348. <dd><p>This is a helper function for callables that use <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> or
  349. <a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a> and have an <code class="docutils literal notranslate"><span class="pre">encoding=None</span></code> parameter.</p>
  350. <p>This function returns <em>encoding</em> if it is not <code class="docutils literal notranslate"><span class="pre">None</span></code>.
  351. Otherwise, it returns <code class="docutils literal notranslate"><span class="pre">&quot;locale&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;utf-8&quot;</span></code> depending on
  352. <a class="reference internal" href="os.html#utf8-mode"><span class="std std-ref">UTF-8 Mode</span></a>.</p>
  353. <p>This function emits an <a class="reference internal" href="exceptions.html#EncodingWarning" title="EncodingWarning"><code class="xref py py-class docutils literal notranslate"><span class="pre">EncodingWarning</span></code></a> if
  354. <a class="reference internal" href="sys.html#sys.flags" title="sys.flags"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.flags.warn_default_encoding</span></code></a> is true and <em>encoding</em>
  355. is <code class="docutils literal notranslate"><span class="pre">None</span></code>. <em>stacklevel</em> specifies where the warning is emitted.
  356. For example:</p>
  357. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">read_text</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  358. <span class="n">encoding</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">text_encoding</span><span class="p">(</span><span class="n">encoding</span><span class="p">)</span> <span class="c1"># stacklevel=2</span>
  359. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  360. <span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  361. </pre></div>
  362. </div>
  363. <p>In this example, an <a class="reference internal" href="exceptions.html#EncodingWarning" title="EncodingWarning"><code class="xref py py-class docutils literal notranslate"><span class="pre">EncodingWarning</span></code></a> is emitted for the caller of
  364. <code class="docutils literal notranslate"><span class="pre">read_text()</span></code>.</p>
  365. <p>See <a class="reference internal" href="#io-text-encoding"><span class="std std-ref">Text Encoding</span></a> for more information.</p>
  366. <div class="versionadded">
  367. <p><span class="versionmodified added">New in version 3.10.</span></p>
  368. </div>
  369. <div class="versionchanged">
  370. <p><span class="versionmodified changed">Changed in version 3.11: </span><a class="reference internal" href="#io.text_encoding" title="io.text_encoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">text_encoding()</span></code></a> returns “utf-8” when UTF-8 mode is enabled and
  371. <em>encoding</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  372. </div>
  373. </dd></dl>
  374. <dl class="py exception">
  375. <dt class="sig sig-object py" id="io.BlockingIOError">
  376. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">BlockingIOError</span></span><a class="headerlink" href="#io.BlockingIOError" title="Link to this definition">¶</a></dt>
  377. <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 notranslate"><span class="pre">BlockingIOError</span></code></a>
  378. exception.</p>
  379. </dd></dl>
  380. <dl class="py exception">
  381. <dt class="sig sig-object py" id="io.UnsupportedOperation">
  382. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">UnsupportedOperation</span></span><a class="headerlink" href="#io.UnsupportedOperation" title="Link to this definition">¶</a></dt>
  383. <dd><p>An exception inheriting <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><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 notranslate"><span class="pre">ValueError</span></code></a> that is raised
  384. when an unsupported operation is called on a stream.</p>
  385. </dd></dl>
  386. <div class="admonition seealso">
  387. <p class="admonition-title">See also</p>
  388. <dl class="simple">
  389. <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 notranslate"><span class="pre">sys</span></code></a></dt><dd><p>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 notranslate"><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 notranslate"><span class="pre">sys.stdout</span></code></a>,
  390. and <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>.</p>
  391. </dd>
  392. </dl>
  393. </div>
  394. </section>
  395. <section id="class-hierarchy">
  396. <h2>Class hierarchy<a class="headerlink" href="#class-hierarchy" title="Link to this heading">¶</a></h2>
  397. <p>The implementation of I/O streams is organized as a hierarchy of classes. First
  398. <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
  399. specify the various categories of streams, then concrete classes providing the
  400. standard stream implementations.</p>
  401. <div class="admonition note">
  402. <p class="admonition-title">Note</p>
  403. <p>The abstract base classes also provide default implementations of some
  404. methods in order to help implementation of concrete stream classes. For
  405. example, <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a> provides unoptimized implementations of
  406. <code class="xref py py-meth docutils literal notranslate"><span class="pre">readinto()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code>.</p>
  407. </div>
  408. <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 notranslate"><span class="pre">IOBase</span></code></a>. It
  409. defines the basic interface to a stream. Note, however, that there is no
  410. separation between reading and writing to streams; implementations are allowed
  411. to raise <a class="reference internal" href="#io.UnsupportedOperation" title="io.UnsupportedOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnsupportedOperation</span></code></a> if they do not support a given operation.</p>
  412. <p>The <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">IOBase</span></code></a>. It deals with the reading
  413. 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 notranslate"><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 notranslate"><span class="pre">RawIOBase</span></code></a>
  414. to provide an interface to files in the machine’s file system.</p>
  415. <p>The <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a> ABC extends <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>. It deals with
  416. buffering on a raw binary stream (<a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a>). Its subclasses,
  417. <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedWriter</span></code></a>, <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">BufferedRWPair</span></code></a>
  418. buffer raw binary streams that are writable, readable, and both readable and writable,
  419. respectively. <a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedRandom</span></code></a> provides a buffered interface to seekable streams.
  420. Another <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">BytesIO</span></code></a>, is a stream of
  421. in-memory bytes.</p>
  422. <p>The <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a> ABC extends <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>. It deals with
  423. streams whose bytes represent text, and handles encoding and decoding to and
  424. from strings. <a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a>, which extends <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a>, is a buffered text
  425. interface to a buffered raw stream (<a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a>). Finally,
  426. <a class="reference internal" href="#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> is an in-memory stream for text.</p>
  427. <p>Argument names are not part of the specification, and only the arguments of
  428. <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> are intended to be used as keyword arguments.</p>
  429. <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 notranslate"><span class="pre">io</span></code></a> module:</p>
  430. <table class="docutils align-default">
  431. <thead>
  432. <tr class="row-odd"><th class="head"><p>ABC</p></th>
  433. <th class="head"><p>Inherits</p></th>
  434. <th class="head"><p>Stub Methods</p></th>
  435. <th class="head"><p>Mixin Methods and Properties</p></th>
  436. </tr>
  437. </thead>
  438. <tbody>
  439. <tr class="row-even"><td><p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a></p></td>
  440. <td></td>
  441. <td><p><code class="docutils literal notranslate"><span class="pre">fileno</span></code>, <code class="docutils literal notranslate"><span class="pre">seek</span></code>,
  442. and <code class="docutils literal notranslate"><span class="pre">truncate</span></code></p></td>
  443. <td><p><code class="docutils literal notranslate"><span class="pre">close</span></code>, <code class="docutils literal notranslate"><span class="pre">closed</span></code>, <code class="docutils literal notranslate"><span class="pre">__enter__</span></code>,
  444. <code class="docutils literal notranslate"><span class="pre">__exit__</span></code>, <code class="docutils literal notranslate"><span class="pre">flush</span></code>, <code class="docutils literal notranslate"><span class="pre">isatty</span></code>, <code class="docutils literal notranslate"><span class="pre">__iter__</span></code>,
  445. <code class="docutils literal notranslate"><span class="pre">__next__</span></code>, <code class="docutils literal notranslate"><span class="pre">readable</span></code>, <code class="docutils literal notranslate"><span class="pre">readline</span></code>,
  446. <code class="docutils literal notranslate"><span class="pre">readlines</span></code>, <code class="docutils literal notranslate"><span class="pre">seekable</span></code>, <code class="docutils literal notranslate"><span class="pre">tell</span></code>,
  447. <code class="docutils literal notranslate"><span class="pre">writable</span></code>, and <code class="docutils literal notranslate"><span class="pre">writelines</span></code></p></td>
  448. </tr>
  449. <tr class="row-odd"><td><p><a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a></p></td>
  450. <td><p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a></p></td>
  451. <td><p><code class="docutils literal notranslate"><span class="pre">readinto</span></code> and
  452. <code class="docutils literal notranslate"><span class="pre">write</span></code></p></td>
  453. <td><p>Inherited <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a> methods, <code class="docutils literal notranslate"><span class="pre">read</span></code>,
  454. and <code class="docutils literal notranslate"><span class="pre">readall</span></code></p></td>
  455. </tr>
  456. <tr class="row-even"><td><p><a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a></p></td>
  457. <td><p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a></p></td>
  458. <td><p><code class="docutils literal notranslate"><span class="pre">detach</span></code>, <code class="docutils literal notranslate"><span class="pre">read</span></code>,
  459. <code class="docutils literal notranslate"><span class="pre">read1</span></code>, and <code class="docutils literal notranslate"><span class="pre">write</span></code></p></td>
  460. <td><p>Inherited <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a> methods, <code class="docutils literal notranslate"><span class="pre">readinto</span></code>,
  461. and <code class="docutils literal notranslate"><span class="pre">readinto1</span></code></p></td>
  462. </tr>
  463. <tr class="row-odd"><td><p><a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a></p></td>
  464. <td><p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a></p></td>
  465. <td><p><code class="docutils literal notranslate"><span class="pre">detach</span></code>, <code class="docutils literal notranslate"><span class="pre">read</span></code>,
  466. <code class="docutils literal notranslate"><span class="pre">readline</span></code>, and
  467. <code class="docutils literal notranslate"><span class="pre">write</span></code></p></td>
  468. <td><p>Inherited <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a> methods, <code class="docutils literal notranslate"><span class="pre">encoding</span></code>,
  469. <code class="docutils literal notranslate"><span class="pre">errors</span></code>, and <code class="docutils literal notranslate"><span class="pre">newlines</span></code></p></td>
  470. </tr>
  471. </tbody>
  472. </table>
  473. <section id="i-o-base-classes">
  474. <h3>I/O Base Classes<a class="headerlink" href="#i-o-base-classes" title="Link to this heading">¶</a></h3>
  475. <dl class="py class">
  476. <dt class="sig sig-object py" id="io.IOBase">
  477. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">IOBase</span></span><a class="headerlink" href="#io.IOBase" title="Link to this definition">¶</a></dt>
  478. <dd><p>The abstract base class for all I/O classes.</p>
  479. <p>This class provides empty abstract implementations for many methods
  480. that derived classes can override selectively; the default
  481. implementations represent a file that cannot be read, written or
  482. seeked.</p>
  483. <p>Even though <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a> does not declare <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code>
  484. or <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> because their signatures will vary, implementations and
  485. clients should consider those methods part of the interface. Also,
  486. implementations may raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><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 notranslate"><span class="pre">UnsupportedOperation</span></code></a>)
  487. when operations they do not support are called.</p>
  488. <p>The basic type used for binary data read from or written to a file is
  489. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><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
  490. accepted as method arguments too. Text I/O classes work with <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> data.</p>
  491. <p>Note that calling any method (even inquiries) on a closed stream is
  492. undefined. Implementations may raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> in this case.</p>
  493. <p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a> (and its subclasses) supports the iterator protocol, meaning
  494. that an <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a> object can be iterated over yielding the lines in a
  495. stream. Lines are defined slightly differently depending on whether the
  496. stream is a binary stream (yielding bytes), or a text stream (yielding
  497. character strings). See <a class="reference internal" href="#io.IOBase.readline" title="io.IOBase.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a> below.</p>
  498. <p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a> is also a context manager and therefore supports the
  499. <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement. In this example, <em>file</em> is closed after the
  500. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> statement’s suite is finished—even if an exception occurs:</p>
  501. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;spam.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
  502. <span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;Spam and eggs!&#39;</span><span class="p">)</span>
  503. </pre></div>
  504. </div>
  505. <p><a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a> provides these data attributes and methods:</p>
  506. <dl class="py method">
  507. <dt class="sig sig-object py" id="io.IOBase.close">
  508. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.close" title="Link to this definition">¶</a></dt>
  509. <dd><p>Flush and close this stream. This method has no effect if the file is
  510. already closed. Once the file is closed, any operation on the file
  511. (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 notranslate"><span class="pre">ValueError</span></code></a>.</p>
  512. <p>As a convenience, it is allowed to call this method more than once;
  513. only the first call, however, will have an effect.</p>
  514. </dd></dl>
  515. <dl class="py attribute">
  516. <dt class="sig sig-object py" id="io.IOBase.closed">
  517. <span class="sig-name descname"><span class="pre">closed</span></span><a class="headerlink" href="#io.IOBase.closed" title="Link to this definition">¶</a></dt>
  518. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the stream is closed.</p>
  519. </dd></dl>
  520. <dl class="py method">
  521. <dt class="sig sig-object py" id="io.IOBase.fileno">
  522. <span class="sig-name descname"><span class="pre">fileno</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.fileno" title="Link to this definition">¶</a></dt>
  523. <dd><p>Return the underlying file descriptor (an integer) of the stream if it
  524. exists. An <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised if the IO object does not use a file
  525. descriptor.</p>
  526. </dd></dl>
  527. <dl class="py method">
  528. <dt class="sig sig-object py" id="io.IOBase.flush">
  529. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.flush" title="Link to this definition">¶</a></dt>
  530. <dd><p>Flush the write buffers of the stream if applicable. This does nothing
  531. for read-only and non-blocking streams.</p>
  532. </dd></dl>
  533. <dl class="py method">
  534. <dt class="sig sig-object py" id="io.IOBase.isatty">
  535. <span class="sig-name descname"><span class="pre">isatty</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.isatty" title="Link to this definition">¶</a></dt>
  536. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the stream is interactive (i.e., connected to
  537. a terminal/tty device).</p>
  538. </dd></dl>
  539. <dl class="py method">
  540. <dt class="sig sig-object py" id="io.IOBase.readable">
  541. <span class="sig-name descname"><span class="pre">readable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.readable" title="Link to this definition">¶</a></dt>
  542. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the stream can be read from.
  543. If <code class="docutils literal notranslate"><span class="pre">False</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> will raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p>
  544. </dd></dl>
  545. <dl class="py method">
  546. <dt class="sig sig-object py" id="io.IOBase.readline">
  547. <span class="sig-name descname"><span class="pre">readline</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.readline" title="Link to this definition">¶</a></dt>
  548. <dd><p>Read and return one line from the stream. If <em>size</em> is specified, at
  549. most <em>size</em> bytes will be read.</p>
  550. <p>The line terminator is always <code class="docutils literal notranslate"><span class="pre">b'\n'</span></code> for binary files; for text files,
  551. the <em>newline</em> argument to <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> can be used to select the line
  552. terminator(s) recognized.</p>
  553. </dd></dl>
  554. <dl class="py method">
  555. <dt class="sig sig-object py" id="io.IOBase.readlines">
  556. <span class="sig-name descname"><span class="pre">readlines</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">hint</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.readlines" title="Link to this definition">¶</a></dt>
  557. <dd><p>Read and return a list of lines from the stream. <em>hint</em> can be specified
  558. to control the number of lines read: no more lines will be read if the
  559. total size (in bytes/characters) of all lines so far exceeds <em>hint</em>.</p>
  560. <p><em>hint</em> values of <code class="docutils literal notranslate"><span class="pre">0</span></code> or less, as well as <code class="docutils literal notranslate"><span class="pre">None</span></code>, are treated as no
  561. hint.</p>
  562. <p>Note that it’s already possible to iterate on file objects using <code class="docutils literal notranslate"><span class="pre">for</span>
  563. <span class="pre">line</span> <span class="pre">in</span> <span class="pre">file:</span> <span class="pre">...</span></code> without calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">file.readlines()</span></code>.</p>
  564. </dd></dl>
  565. <dl class="py method">
  566. <dt class="sig sig-object py" id="io.IOBase.seek">
  567. <span class="sig-name descname"><span class="pre">seek</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">whence</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">os.SEEK_SET</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.seek" title="Link to this definition">¶</a></dt>
  568. <dd><p>Change the stream position to the given byte <em>offset</em>,
  569. interpreted relative to the position indicated by <em>whence</em>,
  570. and return the new absolute position.
  571. Values for <em>whence</em> are:</p>
  572. <ul class="simple">
  573. <li><p><a class="reference internal" href="os.html#os.SEEK_SET" title="os.SEEK_SET"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.SEEK_SET</span></code></a> or <code class="docutils literal notranslate"><span class="pre">0</span></code> – start of the stream (the default);
  574. <em>offset</em> should be zero or positive</p></li>
  575. <li><p><a class="reference internal" href="os.html#os.SEEK_CUR" title="os.SEEK_CUR"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.SEEK_CUR</span></code></a> or <code class="docutils literal notranslate"><span class="pre">1</span></code> – current stream position;
  576. <em>offset</em> may be negative</p></li>
  577. <li><p><a class="reference internal" href="os.html#os.SEEK_END" title="os.SEEK_END"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.SEEK_END</span></code></a> or <code class="docutils literal notranslate"><span class="pre">2</span></code> – end of the stream;
  578. <em>offset</em> is usually negative</p></li>
  579. </ul>
  580. <div class="versionadded">
  581. <p><span class="versionmodified added">New in version 3.1: </span>The <code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_*</span></code> constants.</p>
  582. </div>
  583. <div class="versionadded">
  584. <p><span class="versionmodified added">New in version 3.3: </span>Some operating systems could support additional values, like
  585. <a class="reference internal" href="os.html#os.SEEK_HOLE" title="os.SEEK_HOLE"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.SEEK_HOLE</span></code></a> or <a class="reference internal" href="os.html#os.SEEK_DATA" title="os.SEEK_DATA"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.SEEK_DATA</span></code></a>. The valid values
  586. for a file could depend on it being open in text or binary mode.</p>
  587. </div>
  588. </dd></dl>
  589. <dl class="py method">
  590. <dt class="sig sig-object py" id="io.IOBase.seekable">
  591. <span class="sig-name descname"><span class="pre">seekable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.seekable" title="Link to this definition">¶</a></dt>
  592. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the stream supports random access. If <code class="docutils literal notranslate"><span class="pre">False</span></code>,
  593. <a class="reference internal" href="#io.IOBase.seek" title="io.IOBase.seek"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><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 notranslate"><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 notranslate"><span class="pre">OSError</span></code></a>.</p>
  594. </dd></dl>
  595. <dl class="py method">
  596. <dt class="sig sig-object py" id="io.IOBase.tell">
  597. <span class="sig-name descname"><span class="pre">tell</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.tell" title="Link to this definition">¶</a></dt>
  598. <dd><p>Return the current stream position.</p>
  599. </dd></dl>
  600. <dl class="py method">
  601. <dt class="sig sig-object py" id="io.IOBase.truncate">
  602. <span class="sig-name descname"><span class="pre">truncate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.truncate" title="Link to this definition">¶</a></dt>
  603. <dd><p>Resize the stream to the given <em>size</em> in bytes (or the current position
  604. if <em>size</em> is not specified). The current stream position isn’t changed.
  605. This resizing can extend or reduce the current file size. In case of
  606. extension, the contents of the new file area depend on the platform
  607. (on most systems, additional bytes are zero-filled). The new file size
  608. is returned.</p>
  609. <div class="versionchanged">
  610. <p><span class="versionmodified changed">Changed in version 3.5: </span>Windows will now zero-fill files when extending.</p>
  611. </div>
  612. </dd></dl>
  613. <dl class="py method">
  614. <dt class="sig sig-object py" id="io.IOBase.writable">
  615. <span class="sig-name descname"><span class="pre">writable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.writable" title="Link to this definition">¶</a></dt>
  616. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the stream supports writing. If <code class="docutils literal notranslate"><span class="pre">False</span></code>,
  617. <code class="xref py py-meth docutils literal notranslate"><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 notranslate"><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 notranslate"><span class="pre">OSError</span></code></a>.</p>
  618. </dd></dl>
  619. <dl class="py method">
  620. <dt class="sig sig-object py" id="io.IOBase.writelines">
  621. <span class="sig-name descname"><span class="pre">writelines</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">lines</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.writelines" title="Link to this definition">¶</a></dt>
  622. <dd><p>Write a list of lines to the stream. Line separators are not added, so it
  623. is usual for each of the lines provided to have a line separator at the
  624. end.</p>
  625. </dd></dl>
  626. <dl class="py method">
  627. <dt class="sig sig-object py" id="io.IOBase.__del__">
  628. <span class="sig-name descname"><span class="pre">__del__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.IOBase.__del__" title="Link to this definition">¶</a></dt>
  629. <dd><p>Prepare for object destruction. <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a> provides a default
  630. implementation of this method that calls the instance’s
  631. <a class="reference internal" href="#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method.</p>
  632. </dd></dl>
  633. </dd></dl>
  634. <dl class="py class">
  635. <dt class="sig sig-object py" id="io.RawIOBase">
  636. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">RawIOBase</span></span><a class="headerlink" href="#io.RawIOBase" title="Link to this definition">¶</a></dt>
  637. <dd><p>Base class for raw binary streams. It inherits from <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>.</p>
  638. <p>Raw binary streams typically provide low-level access to an underlying OS
  639. device or API, and do not try to encapsulate it in high-level primitives
  640. (this functionality is done at a higher-level in buffered binary streams and text streams, described later
  641. in this page).</p>
  642. <p><a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a> provides these methods in addition to those from
  643. <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>:</p>
  644. <dl class="py method">
  645. <dt class="sig sig-object py" id="io.RawIOBase.read">
  646. <span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.RawIOBase.read" title="Link to this definition">¶</a></dt>
  647. <dd><p>Read up to <em>size</em> bytes from the object and return them. As a convenience,
  648. if <em>size</em> is unspecified or -1, all bytes until EOF are returned.
  649. Otherwise, only one system call is ever made. Fewer than <em>size</em> bytes may
  650. be returned if the operating system call returns fewer than <em>size</em> bytes.</p>
  651. <p>If 0 bytes are returned, and <em>size</em> was not 0, this indicates end of file.
  652. If the object is in non-blocking mode and no bytes are available,
  653. <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p>
  654. <p>The default implementation defers to <a class="reference internal" href="#io.RawIOBase.readall" title="io.RawIOBase.readall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readall()</span></code></a> and
  655. <a class="reference internal" href="#io.RawIOBase.readinto" title="io.RawIOBase.readinto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readinto()</span></code></a>.</p>
  656. </dd></dl>
  657. <dl class="py method">
  658. <dt class="sig sig-object py" id="io.RawIOBase.readall">
  659. <span class="sig-name descname"><span class="pre">readall</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.RawIOBase.readall" title="Link to this definition">¶</a></dt>
  660. <dd><p>Read and return all the bytes from the stream until EOF, using multiple
  661. calls to the stream if necessary.</p>
  662. </dd></dl>
  663. <dl class="py method">
  664. <dt class="sig sig-object py" id="io.RawIOBase.readinto">
  665. <span class="sig-name descname"><span class="pre">readinto</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.RawIOBase.readinto" title="Link to this definition">¶</a></dt>
  666. <dd><p>Read bytes into a pre-allocated, writable
  667. <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
  668. number of bytes read. For example, <em>b</em> might be a <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>.
  669. If the object is in non-blocking mode and no bytes
  670. are available, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p>
  671. </dd></dl>
  672. <dl class="py method">
  673. <dt class="sig sig-object py" id="io.RawIOBase.write">
  674. <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.RawIOBase.write" title="Link to this definition">¶</a></dt>
  675. <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
  676. underlying raw stream, and return the number of
  677. bytes written. This can be less than the length of <em>b</em> in
  678. bytes, depending on specifics of the underlying raw
  679. stream, and especially if it is in non-blocking mode. <code class="docutils literal notranslate"><span class="pre">None</span></code> is
  680. returned if the raw stream is set not to block and no single byte could
  681. be readily written to it. The caller may release or mutate <em>b</em> after
  682. this method returns, so the implementation should only access <em>b</em>
  683. during the method call.</p>
  684. </dd></dl>
  685. </dd></dl>
  686. <dl class="py class">
  687. <dt class="sig sig-object py" id="io.BufferedIOBase">
  688. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">BufferedIOBase</span></span><a class="headerlink" href="#io.BufferedIOBase" title="Link to this definition">¶</a></dt>
  689. <dd><p>Base class for binary streams that support some kind of buffering.
  690. It inherits from <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>.</p>
  691. <p>The main difference with <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">read()</span></code></a>,
  692. <a class="reference internal" href="#io.BufferedIOBase.readinto" title="io.BufferedIOBase.readinto"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">write()</span></code></a> will try (respectively) to read as much
  693. input as requested or to consume all given output, at the expense of
  694. making perhaps more than one system call.</p>
  695. <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 notranslate"><span class="pre">BlockingIOError</span></code></a> if the
  696. underlying raw stream is in non-blocking mode and cannot take or give
  697. enough data; unlike their <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a> counterparts, they will
  698. never return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  699. <p>Besides, the <a class="reference internal" href="#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> method does not have a default
  700. implementation that defers to <a class="reference internal" href="#io.BufferedIOBase.readinto" title="io.BufferedIOBase.readinto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readinto()</span></code></a>.</p>
  701. <p>A typical <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a> implementation should not inherit from a
  702. <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a> implementation, but wrap one, like
  703. <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">BufferedReader</span></code></a> do.</p>
  704. <p><a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a> provides or overrides these data attributes and
  705. methods in addition to those from <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>:</p>
  706. <dl class="py attribute">
  707. <dt class="sig sig-object py" id="io.BufferedIOBase.raw">
  708. <span class="sig-name descname"><span class="pre">raw</span></span><a class="headerlink" href="#io.BufferedIOBase.raw" title="Link to this definition">¶</a></dt>
  709. <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 notranslate"><span class="pre">RawIOBase</span></code></a> instance) that
  710. <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a> deals with. This is not part of the
  711. <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a> API and may not exist on some implementations.</p>
  712. </dd></dl>
  713. <dl class="py method">
  714. <dt class="sig sig-object py" id="io.BufferedIOBase.detach">
  715. <span class="sig-name descname"><span class="pre">detach</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.detach" title="Link to this definition">¶</a></dt>
  716. <dd><p>Separate the underlying raw stream from the buffer and return it.</p>
  717. <p>After the raw stream has been detached, the buffer is in an unusable
  718. state.</p>
  719. <p>Some buffers, like <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesIO</span></code></a>, do not have the concept of a single
  720. raw stream to return from this method. They raise
  721. <a class="reference internal" href="#io.UnsupportedOperation" title="io.UnsupportedOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnsupportedOperation</span></code></a>.</p>
  722. <div class="versionadded">
  723. <p><span class="versionmodified added">New in version 3.1.</span></p>
  724. </div>
  725. </dd></dl>
  726. <dl class="py method">
  727. <dt class="sig sig-object py" id="io.BufferedIOBase.read">
  728. <span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.read" title="Link to this definition">¶</a></dt>
  729. <dd><p>Read and return up to <em>size</em> bytes. If the argument is omitted, <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  730. or negative, data is read and returned until EOF is reached. An empty
  731. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object is returned if the stream is already at EOF.</p>
  732. <p>If the argument is positive, and the underlying raw stream is not
  733. interactive, multiple raw reads may be issued to satisfy the byte count
  734. (unless EOF is reached first). But for interactive raw streams, at most
  735. one raw read will be issued, and a short result does not imply that EOF is
  736. imminent.</p>
  737. <p>A <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a> is raised if the underlying raw stream is in
  738. non blocking-mode, and has no data available at the moment.</p>
  739. </dd></dl>
  740. <dl class="py method">
  741. <dt class="sig sig-object py" id="io.BufferedIOBase.read1">
  742. <span class="sig-name descname"><span class="pre">read1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.read1" title="Link to this definition">¶</a></dt>
  743. <dd><p>Read and return up to <em>size</em> bytes, with at most one call to the
  744. underlying raw stream’s <a class="reference internal" href="#io.RawIOBase.read" title="io.RawIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> (or
  745. <a class="reference internal" href="#io.RawIOBase.readinto" title="io.RawIOBase.readinto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readinto()</span></code></a>) method. This can be useful if you are
  746. 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 notranslate"><span class="pre">BufferedIOBase</span></code></a>
  747. object.</p>
  748. <p>If <em>size</em> is <code class="docutils literal notranslate"><span class="pre">-1</span></code> (the default), an arbitrary number of bytes are
  749. returned (more than zero unless EOF is reached).</p>
  750. </dd></dl>
  751. <dl class="py method">
  752. <dt class="sig sig-object py" id="io.BufferedIOBase.readinto">
  753. <span class="sig-name descname"><span class="pre">readinto</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.readinto" title="Link to this definition">¶</a></dt>
  754. <dd><p>Read bytes into a pre-allocated, writable
  755. <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.
  756. For example, <em>b</em> might be a <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>.</p>
  757. <p>Like <a class="reference internal" href="#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a>, multiple reads may be issued to the underlying raw
  758. stream, unless the latter is interactive.</p>
  759. <p>A <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a> is raised if the underlying raw stream is in non
  760. blocking-mode, and has no data available at the moment.</p>
  761. </dd></dl>
  762. <dl class="py method">
  763. <dt class="sig sig-object py" id="io.BufferedIOBase.readinto1">
  764. <span class="sig-name descname"><span class="pre">readinto1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.readinto1" title="Link to this definition">¶</a></dt>
  765. <dd><p>Read bytes into a pre-allocated, writable
  766. <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
  767. 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 notranslate"><span class="pre">read()</span></code></a> (or
  768. <a class="reference internal" href="#io.RawIOBase.readinto" title="io.RawIOBase.readinto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readinto()</span></code></a>) method. Return the number of bytes read.</p>
  769. <p>A <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a> is raised if the underlying raw stream is in non
  770. blocking-mode, and has no data available at the moment.</p>
  771. <div class="versionadded">
  772. <p><span class="versionmodified added">New in version 3.5.</span></p>
  773. </div>
  774. </dd></dl>
  775. <dl class="py method">
  776. <dt class="sig sig-object py" id="io.BufferedIOBase.write">
  777. <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedIOBase.write" title="Link to this definition">¶</a></dt>
  778. <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
  779. of bytes written (always equal to the length of <em>b</em> in bytes, since if
  780. the write fails an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> will be raised). Depending on the
  781. actual implementation, these bytes may be readily written to the
  782. underlying stream, or held in a buffer for performance and latency
  783. reasons.</p>
  784. <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 notranslate"><span class="pre">BlockingIOError</span></code></a> is raised if the
  785. data needed to be written to the raw stream but it couldn’t accept
  786. all the data without blocking.</p>
  787. <p>The caller may release or mutate <em>b</em> after this method returns,
  788. so the implementation should only access <em>b</em> during the method call.</p>
  789. </dd></dl>
  790. </dd></dl>
  791. </section>
  792. <section id="raw-file-i-o">
  793. <h3>Raw File I/O<a class="headerlink" href="#raw-file-i-o" title="Link to this heading">¶</a></h3>
  794. <dl class="py class">
  795. <dt class="sig sig-object py" id="io.FileIO">
  796. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">FileIO</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">closefd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">opener</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.FileIO" title="Link to this definition">¶</a></dt>
  797. <dd><p>A raw binary stream representing an OS-level file containing bytes data. It
  798. inherits from <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a>.</p>
  799. <p>The <em>name</em> can be one of two things:</p>
  800. <ul class="simple">
  801. <li><p>a character string or <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object representing the path to the
  802. file which will be opened. In this case closefd must be <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default)
  803. otherwise an error will be raised.</p></li>
  804. <li><p>an integer representing the number of an existing OS-level file descriptor
  805. to which the resulting <a class="reference internal" href="#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileIO</span></code></a> object will give access. When the
  806. FileIO object is closed this fd will be closed as well, unless <em>closefd</em>
  807. is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p></li>
  808. </ul>
  809. <p>The <em>mode</em> can be <code class="docutils literal notranslate"><span class="pre">'r'</span></code>, <code class="docutils literal notranslate"><span class="pre">'w'</span></code>, <code class="docutils literal notranslate"><span class="pre">'x'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a'</span></code> for reading
  810. (default), writing, exclusive creation or appending. The file will be
  811. created if it doesn’t exist when opened for writing or appending; it will be
  812. truncated when opened for writing. <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> will be raised if
  813. it already exists when opened for creating. Opening a file for creating
  814. implies writing, so this mode behaves in a similar way to <code class="docutils literal notranslate"><span class="pre">'w'</span></code>. Add a
  815. <code class="docutils literal notranslate"><span class="pre">'+'</span></code> to the mode to allow simultaneous reading and writing.</p>
  816. <p>The <a class="reference internal" href="#io.RawIOBase.read" title="io.RawIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> (when called with a positive argument),
  817. <a class="reference internal" href="#io.RawIOBase.readinto" title="io.RawIOBase.readinto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readinto()</span></code></a> and <a class="reference internal" href="#io.RawIOBase.write" title="io.RawIOBase.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> methods on this
  818. class will only make one system call.</p>
  819. <p>A custom opener can be used by passing a callable as <em>opener</em>. The underlying
  820. file descriptor for the file object is then obtained by calling <em>opener</em> with
  821. (<em>name</em>, <em>flags</em>). <em>opener</em> must return an open file descriptor (passing
  822. <a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.open</span></code></a> as <em>opener</em> results in functionality similar to passing
  823. <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p>
  824. <p>The newly created file is <a class="reference internal" href="os.html#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p>
  825. <p>See the <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> built-in function for examples on using the <em>opener</em>
  826. parameter.</p>
  827. <div class="versionchanged">
  828. <p><span class="versionmodified changed">Changed in version 3.3: </span>The <em>opener</em> parameter was added.
  829. The <code class="docutils literal notranslate"><span class="pre">'x'</span></code> mode was added.</p>
  830. </div>
  831. <div class="versionchanged">
  832. <p><span class="versionmodified changed">Changed in version 3.4: </span>The file is now non-inheritable.</p>
  833. </div>
  834. <p><a class="reference internal" href="#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileIO</span></code></a> provides these data attributes in addition to those from
  835. <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a> and <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>:</p>
  836. <dl class="py attribute">
  837. <dt class="sig sig-object py" id="io.FileIO.mode">
  838. <span class="sig-name descname"><span class="pre">mode</span></span><a class="headerlink" href="#io.FileIO.mode" title="Link to this definition">¶</a></dt>
  839. <dd><p>The mode as given in the constructor.</p>
  840. </dd></dl>
  841. <dl class="py attribute">
  842. <dt class="sig sig-object py" id="io.FileIO.name">
  843. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#io.FileIO.name" title="Link to this definition">¶</a></dt>
  844. <dd><p>The file name. This is the file descriptor of the file when no name is
  845. given in the constructor.</p>
  846. </dd></dl>
  847. </dd></dl>
  848. </section>
  849. <section id="buffered-streams">
  850. <h3>Buffered Streams<a class="headerlink" href="#buffered-streams" title="Link to this heading">¶</a></h3>
  851. <p>Buffered I/O streams provide a higher-level interface to an I/O device
  852. than raw I/O does.</p>
  853. <dl class="py class">
  854. <dt class="sig sig-object py" id="io.BytesIO">
  855. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">BytesIO</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">initial_bytes</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO" title="Link to this definition">¶</a></dt>
  856. <dd><p>A binary stream using an in-memory bytes buffer. It inherits from
  857. <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a>. The buffer is discarded when the
  858. <a class="reference internal" href="#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method is called.</p>
  859. <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
  860. contains initial data.</p>
  861. <p><a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesIO</span></code></a> provides or overrides these methods in addition to those
  862. from <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">IOBase</span></code></a>:</p>
  863. <dl class="py method">
  864. <dt class="sig sig-object py" id="io.BytesIO.getbuffer">
  865. <span class="sig-name descname"><span class="pre">getbuffer</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO.getbuffer" title="Link to this definition">¶</a></dt>
  866. <dd><p>Return a readable and writable view over the contents of the buffer
  867. without copying them. Also, mutating the view will transparently
  868. update the contents of the buffer:</p>
  869. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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="sa">b</span><span class="s2">&quot;abcdef&quot;</span><span class="p">)</span>
  870. <span class="gp">&gt;&gt;&gt; </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>
  871. <span class="gp">&gt;&gt;&gt; </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="sa">b</span><span class="s2">&quot;56&quot;</span>
  872. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
  873. <span class="go">b&#39;ab56ef&#39;</span>
  874. </pre></div>
  875. </div>
  876. <div class="admonition note">
  877. <p class="admonition-title">Note</p>
  878. <p>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 notranslate"><span class="pre">BytesIO</span></code></a> object cannot be
  879. resized or closed.</p>
  880. </div>
  881. <div class="versionadded">
  882. <p><span class="versionmodified added">New in version 3.2.</span></p>
  883. </div>
  884. </dd></dl>
  885. <dl class="py method">
  886. <dt class="sig sig-object py" id="io.BytesIO.getvalue">
  887. <span class="sig-name descname"><span class="pre">getvalue</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO.getvalue" title="Link to this definition">¶</a></dt>
  888. <dd><p>Return <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> containing the entire contents of the buffer.</p>
  889. </dd></dl>
  890. <dl class="py method">
  891. <dt class="sig sig-object py" id="io.BytesIO.read1">
  892. <span class="sig-name descname"><span class="pre">read1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO.read1" title="Link to this definition">¶</a></dt>
  893. <dd><p>In <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesIO</span></code></a>, this is the same as <a class="reference internal" href="#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a>.</p>
  894. <div class="versionchanged">
  895. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>size</em> argument is now optional.</p>
  896. </div>
  897. </dd></dl>
  898. <dl class="py method">
  899. <dt class="sig sig-object py" id="io.BytesIO.readinto1">
  900. <span class="sig-name descname"><span class="pre">readinto1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BytesIO.readinto1" title="Link to this definition">¶</a></dt>
  901. <dd><p>In <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesIO</span></code></a>, this is the same as <a class="reference internal" href="#io.BufferedIOBase.readinto" title="io.BufferedIOBase.readinto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readinto()</span></code></a>.</p>
  902. <div class="versionadded">
  903. <p><span class="versionmodified added">New in version 3.5.</span></p>
  904. </div>
  905. </dd></dl>
  906. </dd></dl>
  907. <dl class="py class">
  908. <dt class="sig sig-object py" id="io.BufferedReader">
  909. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">BufferedReader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">raw</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DEFAULT_BUFFER_SIZE</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedReader" title="Link to this definition">¶</a></dt>
  910. <dd><p>A buffered binary stream providing higher-level access to a readable, non
  911. seekable <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a> raw binary stream. It inherits from
  912. <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a>.</p>
  913. <p>When reading data from this object, a larger amount of data may be
  914. requested from the underlying raw stream, and kept in an internal buffer.
  915. The buffered data can then be returned directly on subsequent reads.</p>
  916. <p>The constructor creates a <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedReader</span></code></a> for the given readable
  917. <em>raw</em> stream and <em>buffer_size</em>. If <em>buffer_size</em> is omitted,
  918. <a class="reference internal" href="#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEFAULT_BUFFER_SIZE</span></code></a> is used.</p>
  919. <p><a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedReader</span></code></a> provides or overrides these methods in addition to
  920. those from <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">IOBase</span></code></a>:</p>
  921. <dl class="py method">
  922. <dt class="sig sig-object py" id="io.BufferedReader.peek">
  923. <span class="sig-name descname"><span class="pre">peek</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedReader.peek" title="Link to this definition">¶</a></dt>
  924. <dd><p>Return bytes from the stream without advancing the position. At most one
  925. single read on the raw stream is done to satisfy the call. The number of
  926. bytes returned may be less or more than requested.</p>
  927. </dd></dl>
  928. <dl class="py method">
  929. <dt class="sig sig-object py" id="io.BufferedReader.read">
  930. <span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedReader.read" title="Link to this definition">¶</a></dt>
  931. <dd><p>Read and return <em>size</em> bytes, or if <em>size</em> is not given or negative, until
  932. EOF or if the read call would block in non-blocking mode.</p>
  933. </dd></dl>
  934. <dl class="py method">
  935. <dt class="sig sig-object py" id="io.BufferedReader.read1">
  936. <span class="sig-name descname"><span class="pre">read1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedReader.read1" title="Link to this definition">¶</a></dt>
  937. <dd><p>Read and return up to <em>size</em> bytes with only one call on the raw stream.
  938. If at least one byte is buffered, only buffered bytes are returned.
  939. Otherwise, one raw stream read call is made.</p>
  940. <div class="versionchanged">
  941. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>size</em> argument is now optional.</p>
  942. </div>
  943. </dd></dl>
  944. </dd></dl>
  945. <dl class="py class">
  946. <dt class="sig sig-object py" id="io.BufferedWriter">
  947. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">BufferedWriter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">raw</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DEFAULT_BUFFER_SIZE</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedWriter" title="Link to this definition">¶</a></dt>
  948. <dd><p>A buffered binary stream providing higher-level access to a writeable, non
  949. seekable <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a> raw binary stream. It inherits from
  950. <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a>.</p>
  951. <p>When writing to this object, data is normally placed into an internal
  952. 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 notranslate"><span class="pre">RawIOBase</span></code></a>
  953. object under various conditions, including:</p>
  954. <ul class="simple">
  955. <li><p>when the buffer gets too small for all pending data;</p></li>
  956. <li><p>when <a class="reference internal" href="#io.BufferedWriter.flush" title="io.BufferedWriter.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> is called;</p></li>
  957. <li><p>when a <a class="reference internal" href="#io.IOBase.seek" title="io.IOBase.seek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code></a> is requested (for <a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedRandom</span></code></a> objects);</p></li>
  958. <li><p>when the <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedWriter</span></code></a> object is closed or destroyed.</p></li>
  959. </ul>
  960. <p>The constructor creates a <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedWriter</span></code></a> for the given writeable
  961. <em>raw</em> stream. If the <em>buffer_size</em> is not given, it defaults to
  962. <a class="reference internal" href="#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEFAULT_BUFFER_SIZE</span></code></a>.</p>
  963. <p><a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedWriter</span></code></a> provides or overrides these methods in addition to
  964. those from <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">IOBase</span></code></a>:</p>
  965. <dl class="py method">
  966. <dt class="sig sig-object py" id="io.BufferedWriter.flush">
  967. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedWriter.flush" title="Link to this definition">¶</a></dt>
  968. <dd><p>Force bytes held in the buffer into the raw stream. A
  969. <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a> should be raised if the raw stream blocks.</p>
  970. </dd></dl>
  971. <dl class="py method">
  972. <dt class="sig sig-object py" id="io.BufferedWriter.write">
  973. <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedWriter.write" title="Link to this definition">¶</a></dt>
  974. <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
  975. number of bytes written. When in non-blocking mode, a
  976. <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a> is raised if the buffer needs to be written out but
  977. the raw stream blocks.</p>
  978. </dd></dl>
  979. </dd></dl>
  980. <dl class="py class">
  981. <dt class="sig sig-object py" id="io.BufferedRandom">
  982. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">BufferedRandom</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">raw</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DEFAULT_BUFFER_SIZE</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedRandom" title="Link to this definition">¶</a></dt>
  983. <dd><p>A buffered binary stream providing higher-level access to a seekable
  984. <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a> raw binary stream. It inherits from <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedReader</span></code></a>
  985. and <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedWriter</span></code></a>.</p>
  986. <p>The constructor creates a reader and writer for a seekable raw stream, given
  987. in the first argument. If the <em>buffer_size</em> is omitted it defaults to
  988. <a class="reference internal" href="#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEFAULT_BUFFER_SIZE</span></code></a>.</p>
  989. <p><a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">BufferedReader</span></code></a> or
  990. <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedWriter</span></code></a> can do. In addition, <a class="reference internal" href="#io.IOBase.seek" title="io.IOBase.seek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code></a> and
  991. <a class="reference internal" href="#io.IOBase.tell" title="io.IOBase.tell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tell()</span></code></a> are guaranteed to be implemented.</p>
  992. </dd></dl>
  993. <dl class="py class">
  994. <dt class="sig sig-object py" id="io.BufferedRWPair">
  995. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">BufferedRWPair</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">reader</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">writer</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DEFAULT_BUFFER_SIZE</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.BufferedRWPair" title="Link to this definition">¶</a></dt>
  996. <dd><p>A buffered binary stream providing higher-level access to two non seekable
  997. <a class="reference internal" href="#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawIOBase</span></code></a> raw binary streams—one readable, the other writeable.
  998. It inherits from <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a>.</p>
  999. <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 notranslate"><span class="pre">RawIOBase</span></code></a> objects that are readable and
  1000. writeable respectively. If the <em>buffer_size</em> is omitted it defaults to
  1001. <a class="reference internal" href="#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEFAULT_BUFFER_SIZE</span></code></a>.</p>
  1002. <p><a class="reference internal" href="#io.BufferedRWPair" title="io.BufferedRWPair"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">BufferedIOBase</span></code></a>'s methods
  1003. except for <a class="reference internal" href="#io.BufferedIOBase.detach" title="io.BufferedIOBase.detach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">detach()</span></code></a>, which raises
  1004. <a class="reference internal" href="#io.UnsupportedOperation" title="io.UnsupportedOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnsupportedOperation</span></code></a>.</p>
  1005. <div class="admonition warning">
  1006. <p class="admonition-title">Warning</p>
  1007. <p><a class="reference internal" href="#io.BufferedRWPair" title="io.BufferedRWPair"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedRWPair</span></code></a> does not attempt to synchronize accesses to
  1008. its underlying raw streams. You should not pass it the same object
  1009. as reader and writer; use <a class="reference internal" href="#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedRandom</span></code></a> instead.</p>
  1010. </div>
  1011. </dd></dl>
  1012. </section>
  1013. <section id="id1">
  1014. <h3>Text I/O<a class="headerlink" href="#id1" title="Link to this heading">¶</a></h3>
  1015. <dl class="py class">
  1016. <dt class="sig sig-object py" id="io.TextIOBase">
  1017. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">TextIOBase</span></span><a class="headerlink" href="#io.TextIOBase" title="Link to this definition">¶</a></dt>
  1018. <dd><p>Base class for text streams. This class provides a character and line based
  1019. interface to stream I/O. It inherits from <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>.</p>
  1020. <p><a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a> provides or overrides these data attributes and
  1021. methods in addition to those from <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>:</p>
  1022. <dl class="py attribute">
  1023. <dt class="sig sig-object py" id="io.TextIOBase.encoding">
  1024. <span class="sig-name descname"><span class="pre">encoding</span></span><a class="headerlink" href="#io.TextIOBase.encoding" title="Link to this definition">¶</a></dt>
  1025. <dd><p>The name of the encoding used to decode the stream’s bytes into
  1026. strings, and to encode strings into bytes.</p>
  1027. </dd></dl>
  1028. <dl class="py attribute">
  1029. <dt class="sig sig-object py" id="io.TextIOBase.errors">
  1030. <span class="sig-name descname"><span class="pre">errors</span></span><a class="headerlink" href="#io.TextIOBase.errors" title="Link to this definition">¶</a></dt>
  1031. <dd><p>The error setting of the decoder or encoder.</p>
  1032. </dd></dl>
  1033. <dl class="py attribute">
  1034. <dt class="sig sig-object py" id="io.TextIOBase.newlines">
  1035. <span class="sig-name descname"><span class="pre">newlines</span></span><a class="headerlink" href="#io.TextIOBase.newlines" title="Link to this definition">¶</a></dt>
  1036. <dd><p>A string, a tuple of strings, or <code class="docutils literal notranslate"><span class="pre">None</span></code>, indicating the newlines
  1037. translated so far. Depending on the implementation and the initial
  1038. constructor flags, this may not be available.</p>
  1039. </dd></dl>
  1040. <dl class="py attribute">
  1041. <dt class="sig sig-object py" id="io.TextIOBase.buffer">
  1042. <span class="sig-name descname"><span class="pre">buffer</span></span><a class="headerlink" href="#io.TextIOBase.buffer" title="Link to this definition">¶</a></dt>
  1043. <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 notranslate"><span class="pre">BufferedIOBase</span></code></a> instance) that
  1044. <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a> deals with. This is not part of the
  1045. <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a> API and may not exist in some implementations.</p>
  1046. </dd></dl>
  1047. <dl class="py method">
  1048. <dt class="sig sig-object py" id="io.TextIOBase.detach">
  1049. <span class="sig-name descname"><span class="pre">detach</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.detach" title="Link to this definition">¶</a></dt>
  1050. <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 notranslate"><span class="pre">TextIOBase</span></code></a> and
  1051. return it.</p>
  1052. <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 notranslate"><span class="pre">TextIOBase</span></code></a> is
  1053. in an unusable state.</p>
  1054. <p>Some <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">StringIO</span></code></a>, may not
  1055. have the concept of an underlying buffer and calling this method will
  1056. raise <a class="reference internal" href="#io.UnsupportedOperation" title="io.UnsupportedOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnsupportedOperation</span></code></a>.</p>
  1057. <div class="versionadded">
  1058. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1059. </div>
  1060. </dd></dl>
  1061. <dl class="py method">
  1062. <dt class="sig sig-object py" id="io.TextIOBase.read">
  1063. <span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.read" title="Link to this definition">¶</a></dt>
  1064. <dd><p>Read and return at most <em>size</em> characters from the stream as a single
  1065. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>. If <em>size</em> is negative or <code class="docutils literal notranslate"><span class="pre">None</span></code>, reads until EOF.</p>
  1066. </dd></dl>
  1067. <dl class="py method">
  1068. <dt class="sig sig-object py" id="io.TextIOBase.readline">
  1069. <span class="sig-name descname"><span class="pre">readline</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.readline" title="Link to this definition">¶</a></dt>
  1070. <dd><p>Read until newline or EOF and return a single <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>. If the stream is
  1071. already at EOF, an empty string is returned.</p>
  1072. <p>If <em>size</em> is specified, at most <em>size</em> characters will be read.</p>
  1073. </dd></dl>
  1074. <dl class="py method">
  1075. <dt class="sig sig-object py" id="io.TextIOBase.seek">
  1076. <span class="sig-name descname"><span class="pre">seek</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">whence</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">SEEK_SET</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.seek" title="Link to this definition">¶</a></dt>
  1077. <dd><p>Change the stream position to the given <em>offset</em>. Behaviour depends on
  1078. the <em>whence</em> parameter. The default value for <em>whence</em> is
  1079. <code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_SET</span></code>.</p>
  1080. <ul class="simple">
  1081. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_SET</span></code> or <code class="docutils literal notranslate"><span class="pre">0</span></code>: seek from the start of the stream
  1082. (the default); <em>offset</em> must either be a number returned by
  1083. <a class="reference internal" href="#io.TextIOBase.tell" title="io.TextIOBase.tell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TextIOBase.tell()</span></code></a>, or zero. Any other <em>offset</em> value
  1084. produces undefined behaviour.</p></li>
  1085. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_CUR</span></code> or <code class="docutils literal notranslate"><span class="pre">1</span></code>: “seek” to the current position;
  1086. <em>offset</em> must be zero, which is a no-operation (all other values
  1087. are unsupported).</p></li>
  1088. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_END</span></code> or <code class="docutils literal notranslate"><span class="pre">2</span></code>: seek to the end of the stream;
  1089. <em>offset</em> must be zero (all other values are unsupported).</p></li>
  1090. </ul>
  1091. <p>Return the new absolute position as an opaque number.</p>
  1092. <div class="versionadded">
  1093. <p><span class="versionmodified added">New in version 3.1: </span>The <code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_*</span></code> constants.</p>
  1094. </div>
  1095. </dd></dl>
  1096. <dl class="py method">
  1097. <dt class="sig sig-object py" id="io.TextIOBase.tell">
  1098. <span class="sig-name descname"><span class="pre">tell</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.tell" title="Link to this definition">¶</a></dt>
  1099. <dd><p>Return the current stream position as an opaque number. The number
  1100. does not usually represent a number of bytes in the underlying
  1101. binary storage.</p>
  1102. </dd></dl>
  1103. <dl class="py method">
  1104. <dt class="sig sig-object py" id="io.TextIOBase.write">
  1105. <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOBase.write" title="Link to this definition">¶</a></dt>
  1106. <dd><p>Write the string <em>s</em> to the stream and return the number of characters
  1107. written.</p>
  1108. </dd></dl>
  1109. </dd></dl>
  1110. <dl class="py class">
  1111. <dt class="sig sig-object py" id="io.TextIOWrapper">
  1112. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">TextIOWrapper</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">line_buffering</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">write_through</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOWrapper" title="Link to this definition">¶</a></dt>
  1113. <dd><p>A buffered text stream providing higher-level access to a
  1114. <a class="reference internal" href="#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a> buffered binary stream. It inherits from
  1115. <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a>.</p>
  1116. <p><em>encoding</em> gives the name of the encoding that the stream will be decoded or
  1117. encoded with. It defaults to <a class="reference internal" href="locale.html#locale.getencoding" title="locale.getencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getencoding()</span></code></a>.
  1118. <code class="docutils literal notranslate"><span class="pre">encoding=&quot;locale&quot;</span></code> can be used to specify the current locale’s encoding
  1119. explicitly. See <a class="reference internal" href="#io-text-encoding"><span class="std std-ref">Text Encoding</span></a> for more information.</p>
  1120. <p><em>errors</em> is an optional string that specifies how encoding and decoding
  1121. errors are to be handled. Pass <code class="docutils literal notranslate"><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 notranslate"><span class="pre">ValueError</span></code></a>
  1122. exception if there is an encoding error (the default of <code class="docutils literal notranslate"><span class="pre">None</span></code> has the same
  1123. effect), or pass <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> to ignore errors. (Note that ignoring encoding
  1124. errors can lead to data loss.) <code class="docutils literal notranslate"><span class="pre">'replace'</span></code> causes a replacement marker
  1125. (such as <code class="docutils literal notranslate"><span class="pre">'?'</span></code>) to be inserted where there is malformed data.
  1126. <code class="docutils literal notranslate"><span class="pre">'backslashreplace'</span></code> causes malformed data to be replaced by a
  1127. backslashed escape sequence. When writing, <code class="docutils literal notranslate"><span class="pre">'xmlcharrefreplace'</span></code>
  1128. (replace with the appropriate XML character reference) or <code class="docutils literal notranslate"><span class="pre">'namereplace'</span></code>
  1129. (replace with <code class="docutils literal notranslate"><span class="pre">\N{...}</span></code> escape sequences) can be used. Any other error
  1130. handling name that has been registered with
  1131. <a class="reference internal" href="codecs.html#codecs.register_error" title="codecs.register_error"><code class="xref py py-func docutils literal notranslate"><span class="pre">codecs.register_error()</span></code></a> is also valid.</p>
  1132. <p id="index-4"><em>newline</em> controls how line endings are handled. It can be <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  1133. <code class="docutils literal notranslate"><span class="pre">''</span></code>, <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>, <code class="docutils literal notranslate"><span class="pre">'\r'</span></code>, and <code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code>. It works as follows:</p>
  1134. <ul class="simple">
  1135. <li><p>When reading input from the stream, if <em>newline</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  1136. <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
  1137. <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>, <code class="docutils literal notranslate"><span class="pre">'\r'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code>, and these are translated into <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>
  1138. before being returned to the caller. If <em>newline</em> is <code class="docutils literal notranslate"><span class="pre">''</span></code>, universal
  1139. newlines mode is enabled, but line endings are returned to the caller
  1140. untranslated. If <em>newline</em> has any of the other legal values, input lines
  1141. are only terminated by the given string, and the line ending is returned to
  1142. the caller untranslated.</p></li>
  1143. <li><p>When writing output to the stream, if <em>newline</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, any <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>
  1144. characters written are translated to the system default line separator,
  1145. <a class="reference internal" href="os.html#os.linesep" title="os.linesep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.linesep</span></code></a>. If <em>newline</em> is <code class="docutils literal notranslate"><span class="pre">''</span></code> or <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>, no translation
  1146. takes place. If <em>newline</em> is any of the other legal values, any <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>
  1147. characters written are translated to the given string.</p></li>
  1148. </ul>
  1149. <p>If <em>line_buffering</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, <a class="reference internal" href="#io.IOBase.flush" title="io.IOBase.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> is implied when a call to
  1150. write contains a newline character or a carriage return.</p>
  1151. <p>If <em>write_through</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, calls to <a class="reference internal" href="#io.BufferedIOBase.write" title="io.BufferedIOBase.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> are guaranteed
  1152. 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 notranslate"><span class="pre">TextIOWrapper</span></code></a>
  1153. object is immediately handled to its underlying binary <em>buffer</em>.</p>
  1154. <div class="versionchanged">
  1155. <p><span class="versionmodified changed">Changed in version 3.3: </span>The <em>write_through</em> argument has been added.</p>
  1156. </div>
  1157. <div class="versionchanged">
  1158. <p><span class="versionmodified changed">Changed in version 3.3: </span>The default <em>encoding</em> is now <code class="docutils literal notranslate"><span class="pre">locale.getpreferredencoding(False)</span></code>
  1159. instead of <code class="docutils literal notranslate"><span class="pre">locale.getpreferredencoding()</span></code>. Don’t change temporary the
  1160. locale encoding using <a class="reference internal" href="locale.html#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.setlocale()</span></code></a>, use the current locale
  1161. encoding instead of the user preferred encoding.</p>
  1162. </div>
  1163. <div class="versionchanged">
  1164. <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>encoding</em> argument now supports the <code class="docutils literal notranslate"><span class="pre">&quot;locale&quot;</span></code> dummy encoding name.</p>
  1165. </div>
  1166. <p><a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a> provides these data attributes and methods in
  1167. addition to those from <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a> and <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>:</p>
  1168. <dl class="py attribute">
  1169. <dt class="sig sig-object py" id="io.TextIOWrapper.line_buffering">
  1170. <span class="sig-name descname"><span class="pre">line_buffering</span></span><a class="headerlink" href="#io.TextIOWrapper.line_buffering" title="Link to this definition">¶</a></dt>
  1171. <dd><p>Whether line buffering is enabled.</p>
  1172. </dd></dl>
  1173. <dl class="py attribute">
  1174. <dt class="sig sig-object py" id="io.TextIOWrapper.write_through">
  1175. <span class="sig-name descname"><span class="pre">write_through</span></span><a class="headerlink" href="#io.TextIOWrapper.write_through" title="Link to this definition">¶</a></dt>
  1176. <dd><p>Whether writes are passed immediately to the underlying binary
  1177. buffer.</p>
  1178. <div class="versionadded">
  1179. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1180. </div>
  1181. </dd></dl>
  1182. <dl class="py method">
  1183. <dt class="sig sig-object py" id="io.TextIOWrapper.reconfigure">
  1184. <span class="sig-name descname"><span class="pre">reconfigure</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">line_buffering</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">write_through</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOWrapper.reconfigure" title="Link to this definition">¶</a></dt>
  1185. <dd><p>Reconfigure this text stream using new settings for <em>encoding</em>,
  1186. <em>errors</em>, <em>newline</em>, <em>line_buffering</em> and <em>write_through</em>.</p>
  1187. <p>Parameters not specified keep current settings, except
  1188. <code class="docutils literal notranslate"><span class="pre">errors='strict'</span></code> is used when <em>encoding</em> is specified but
  1189. <em>errors</em> is not specified.</p>
  1190. <p>It is not possible to change the encoding or newline if some data
  1191. has already been read from the stream. On the other hand, changing
  1192. encoding after write is possible.</p>
  1193. <p>This method does an implicit stream flush before setting the
  1194. new parameters.</p>
  1195. <div class="versionadded">
  1196. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1197. </div>
  1198. <div class="versionchanged">
  1199. <p><span class="versionmodified changed">Changed in version 3.11: </span>The method supports <code class="docutils literal notranslate"><span class="pre">encoding=&quot;locale&quot;</span></code> option.</p>
  1200. </div>
  1201. </dd></dl>
  1202. <dl class="py method">
  1203. <dt class="sig sig-object py" id="io.TextIOWrapper.seek">
  1204. <span class="sig-name descname"><span class="pre">seek</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cookie</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">whence</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">os.SEEK_SET</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOWrapper.seek" title="Link to this definition">¶</a></dt>
  1205. <dd><p>Set the stream position.
  1206. Return the new stream position as an <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>.</p>
  1207. <p>Four operations are supported,
  1208. given by the following argument combinations:</p>
  1209. <ul class="simple">
  1210. <li><p><code class="docutils literal notranslate"><span class="pre">seek(0,</span> <span class="pre">SEEK_SET)</span></code>: Rewind to the start of the stream.</p></li>
  1211. <li><p><code class="docutils literal notranslate"><span class="pre">seek(cookie,</span> <span class="pre">SEEK_SET)</span></code>: Restore a previous position;
  1212. <em>cookie</em> <strong>must be</strong> a number returned by <a class="reference internal" href="#io.TextIOWrapper.tell" title="io.TextIOWrapper.tell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tell()</span></code></a>.</p></li>
  1213. <li><p><code class="docutils literal notranslate"><span class="pre">seek(0,</span> <span class="pre">SEEK_END)</span></code>: Fast-forward to the end of the stream.</p></li>
  1214. <li><p><code class="docutils literal notranslate"><span class="pre">seek(0,</span> <span class="pre">SEEK_CUR)</span></code>: Leave the current stream position unchanged.</p></li>
  1215. </ul>
  1216. <p>Any other argument combinations are invalid,
  1217. and may raise exceptions.</p>
  1218. <div class="admonition seealso">
  1219. <p class="admonition-title">See also</p>
  1220. <p><a class="reference internal" href="os.html#os.SEEK_SET" title="os.SEEK_SET"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.SEEK_SET</span></code></a>, <a class="reference internal" href="os.html#os.SEEK_CUR" title="os.SEEK_CUR"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.SEEK_CUR</span></code></a>, and <a class="reference internal" href="os.html#os.SEEK_END" title="os.SEEK_END"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.SEEK_END</span></code></a>.</p>
  1221. </div>
  1222. </dd></dl>
  1223. <dl class="py method">
  1224. <dt class="sig sig-object py" id="io.TextIOWrapper.tell">
  1225. <span class="sig-name descname"><span class="pre">tell</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.TextIOWrapper.tell" title="Link to this definition">¶</a></dt>
  1226. <dd><p>Return the stream position as an opaque number.
  1227. The return value of <code class="xref py py-meth docutils literal notranslate"><span class="pre">tell()</span></code> can be given as input to <a class="reference internal" href="#io.TextIOWrapper.seek" title="io.TextIOWrapper.seek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code></a>,
  1228. to restore a previous stream position.</p>
  1229. </dd></dl>
  1230. </dd></dl>
  1231. <dl class="py class">
  1232. <dt class="sig sig-object py" id="io.StringIO">
  1233. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">StringIO</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">initial_value</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'\n'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#io.StringIO" title="Link to this definition">¶</a></dt>
  1234. <dd><p>A text stream using an in-memory text buffer. It inherits from
  1235. <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a>.</p>
  1236. <p>The text buffer is discarded when the <a class="reference internal" href="#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method is
  1237. called.</p>
  1238. <p>The initial value of the buffer can be set by providing <em>initial_value</em>.
  1239. If newline translation is enabled, newlines will be encoded as if by
  1240. <a class="reference internal" href="#io.TextIOBase.write" title="io.TextIOBase.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a>. The stream is positioned at the start of the
  1241. buffer which emulates opening an existing file in a <code class="docutils literal notranslate"><span class="pre">w+</span></code> mode, making it
  1242. ready for an immediate write from the beginning or for a write that
  1243. would overwrite the initial value. To emulate opening a file in an <code class="docutils literal notranslate"><span class="pre">a+</span></code>
  1244. mode ready for appending, use <code class="docutils literal notranslate"><span class="pre">f.seek(0,</span> <span class="pre">io.SEEK_END)</span></code> to reposition the
  1245. stream at the end of the buffer.</p>
  1246. <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 notranslate"><span class="pre">TextIOWrapper</span></code></a>,
  1247. except that when writing output to the stream, if <em>newline</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  1248. newlines are written as <code class="docutils literal notranslate"><span class="pre">\n</span></code> on all platforms.</p>
  1249. <p><a class="reference internal" href="#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> provides this method in addition to those from
  1250. <a class="reference internal" href="#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a> and <a class="reference internal" href="#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">IOBase</span></code></a>:</p>
  1251. <dl class="py method">
  1252. <dt class="sig sig-object py" id="io.StringIO.getvalue">
  1253. <span class="sig-name descname"><span class="pre">getvalue</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#io.StringIO.getvalue" title="Link to this definition">¶</a></dt>
  1254. <dd><p>Return a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> containing the entire contents of the buffer.
  1255. 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 notranslate"><span class="pre">read()</span></code></a>, although
  1256. the stream position is not changed.</p>
  1257. </dd></dl>
  1258. <p>Example usage:</p>
  1259. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">io</span>
  1260. <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>
  1261. <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;First line.</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
  1262. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Second line.&#39;</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">output</span><span class="p">)</span>
  1263. <span class="c1"># Retrieve file contents -- this will be</span>
  1264. <span class="c1"># &#39;First line.\nSecond line.\n&#39;</span>
  1265. <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>
  1266. <span class="c1"># Close object and discard memory buffer --</span>
  1267. <span class="c1"># .getvalue() will now raise an exception.</span>
  1268. <span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1269. </pre></div>
  1270. </div>
  1271. </dd></dl>
  1272. <dl class="py class" id="index-5">
  1273. <dt class="sig sig-object py" id="io.IncrementalNewlineDecoder">
  1274. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">io.</span></span><span class="sig-name descname"><span class="pre">IncrementalNewlineDecoder</span></span><a class="headerlink" href="#io.IncrementalNewlineDecoder" title="Link to this definition">¶</a></dt>
  1275. <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.
  1276. It inherits from <a class="reference internal" href="codecs.html#codecs.IncrementalDecoder" title="codecs.IncrementalDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">codecs.IncrementalDecoder</span></code></a>.</p>
  1277. </dd></dl>
  1278. </section>
  1279. </section>
  1280. <section id="performance">
  1281. <h2>Performance<a class="headerlink" href="#performance" title="Link to this heading">¶</a></h2>
  1282. <p>This section discusses the performance of the provided concrete I/O
  1283. implementations.</p>
  1284. <section id="id2">
  1285. <h3>Binary I/O<a class="headerlink" href="#id2" title="Link to this heading">¶</a></h3>
  1286. <p>By reading and writing only large chunks of data even when the user asks for a
  1287. single byte, buffered I/O hides any inefficiency in calling and executing the
  1288. operating system’s unbuffered I/O routines. The gain depends on the OS and the
  1289. kind of I/O which is performed. For example, on some modern OSes such as Linux,
  1290. unbuffered disk I/O can be as fast as buffered I/O. The bottom line, however,
  1291. is that buffered I/O offers predictable performance regardless of the platform
  1292. and the backing device. Therefore, it is almost always preferable to use
  1293. buffered I/O rather than unbuffered I/O for binary data.</p>
  1294. </section>
  1295. <section id="id3">
  1296. <h3>Text I/O<a class="headerlink" href="#id3" title="Link to this heading">¶</a></h3>
  1297. <p>Text I/O over a binary storage (such as a file) is significantly slower than
  1298. binary I/O over the same storage, because it requires conversions between
  1299. unicode and binary data using a character codec. This can become noticeable
  1300. handling huge amounts of text data like large log files. Also,
  1301. <a class="reference internal" href="#io.TextIOBase.tell" title="io.TextIOBase.tell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tell()</span></code></a> and <a class="reference internal" href="#io.TextIOBase.seek" title="io.TextIOBase.seek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code></a> are both quite slow
  1302. due to the reconstruction algorithm used.</p>
  1303. <p><a class="reference internal" href="#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a>, however, is a native in-memory unicode container and will
  1304. exhibit similar speed to <a class="reference internal" href="#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesIO</span></code></a>.</p>
  1305. </section>
  1306. <section id="multi-threading">
  1307. <h3>Multi-threading<a class="headerlink" href="#multi-threading" title="Link to this heading">¶</a></h3>
  1308. <p><a class="reference internal" href="#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileIO</span></code></a> objects are thread-safe to the extent that the operating system
  1309. calls (such as <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/read(2)">read(2)</a></em> under Unix) they wrap are thread-safe too.</p>
  1310. <p>Binary buffered objects (instances of <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedReader</span></code></a>,
  1311. <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><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 notranslate"><span class="pre">BufferedRWPair</span></code></a>)
  1312. protect their internal structures using a lock; it is therefore safe to call
  1313. them from multiple threads at once.</p>
  1314. <p><a class="reference internal" href="#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a> objects are not thread-safe.</p>
  1315. </section>
  1316. <section id="reentrancy">
  1317. <h3>Reentrancy<a class="headerlink" href="#reentrancy" title="Link to this heading">¶</a></h3>
  1318. <p>Binary buffered objects (instances of <a class="reference internal" href="#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedReader</span></code></a>,
  1319. <a class="reference internal" href="#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><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 notranslate"><span class="pre">BufferedRWPair</span></code></a>)
  1320. are not reentrant. While reentrant calls will not happen in normal situations,
  1321. 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 notranslate"><span class="pre">signal</span></code></a> handler. If a thread tries to
  1322. 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 notranslate"><span class="pre">RuntimeError</span></code></a>
  1323. is raised. Note this doesn’t prohibit a different thread from entering the
  1324. buffered object.</p>
  1325. <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 notranslate"><span class="pre">open()</span></code></a> function
  1326. 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 notranslate"><span class="pre">TextIOWrapper</span></code></a>. This includes
  1327. standard streams and therefore affects the built-in <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> function as
  1328. well.</p>
  1329. </section>
  1330. </section>
  1331. </section>
  1332. <div class="clearer"></div>
  1333. </div>
  1334. </div>
  1335. </div>
  1336. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1337. <div class="sphinxsidebarwrapper">
  1338. <div>
  1339. <h3><a href="../contents.html">Table of Contents</a></h3>
  1340. <ul>
  1341. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code> — Core tools for working with streams</a><ul>
  1342. <li><a class="reference internal" href="#overview">Overview</a><ul>
  1343. <li><a class="reference internal" href="#text-i-o">Text I/O</a></li>
  1344. <li><a class="reference internal" href="#binary-i-o">Binary I/O</a></li>
  1345. <li><a class="reference internal" href="#raw-i-o">Raw I/O</a></li>
  1346. </ul>
  1347. </li>
  1348. <li><a class="reference internal" href="#text-encoding">Text Encoding</a><ul>
  1349. <li><a class="reference internal" href="#opt-in-encodingwarning">Opt-in EncodingWarning</a></li>
  1350. </ul>
  1351. </li>
  1352. <li><a class="reference internal" href="#high-level-module-interface">High-level Module Interface</a></li>
  1353. <li><a class="reference internal" href="#class-hierarchy">Class hierarchy</a><ul>
  1354. <li><a class="reference internal" href="#i-o-base-classes">I/O Base Classes</a></li>
  1355. <li><a class="reference internal" href="#raw-file-i-o">Raw File I/O</a></li>
  1356. <li><a class="reference internal" href="#buffered-streams">Buffered Streams</a></li>
  1357. <li><a class="reference internal" href="#id1">Text I/O</a></li>
  1358. </ul>
  1359. </li>
  1360. <li><a class="reference internal" href="#performance">Performance</a><ul>
  1361. <li><a class="reference internal" href="#id2">Binary I/O</a></li>
  1362. <li><a class="reference internal" href="#id3">Text I/O</a></li>
  1363. <li><a class="reference internal" href="#multi-threading">Multi-threading</a></li>
  1364. <li><a class="reference internal" href="#reentrancy">Reentrancy</a></li>
  1365. </ul>
  1366. </li>
  1367. </ul>
  1368. </li>
  1369. </ul>
  1370. </div>
  1371. <div>
  1372. <h4>Previous topic</h4>
  1373. <p class="topless"><a href="os.html"
  1374. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code> — Miscellaneous operating system interfaces</a></p>
  1375. </div>
  1376. <div>
  1377. <h4>Next topic</h4>
  1378. <p class="topless"><a href="time.html"
  1379. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code> — Time access and conversions</a></p>
  1380. </div>
  1381. <div role="note" aria-label="source link">
  1382. <h3>This Page</h3>
  1383. <ul class="this-page-menu">
  1384. <li><a href="../bugs.html">Report a Bug</a></li>
  1385. <li>
  1386. <a href="https://github.com/python/cpython/blob/main/Doc/library/io.rst"
  1387. rel="nofollow">Show Source
  1388. </a>
  1389. </li>
  1390. </ul>
  1391. </div>
  1392. </div>
  1393. <div id="sidebarbutton" title="Collapse sidebar">
  1394. <span>«</span>
  1395. </div>
  1396. </div>
  1397. <div class="clearer"></div>
  1398. </div>
  1399. <div class="related" role="navigation" aria-label="related navigation">
  1400. <h3>Navigation</h3>
  1401. <ul>
  1402. <li class="right" style="margin-right: 10px">
  1403. <a href="../genindex.html" title="General Index"
  1404. >index</a></li>
  1405. <li class="right" >
  1406. <a href="../py-modindex.html" title="Python Module Index"
  1407. >modules</a> |</li>
  1408. <li class="right" >
  1409. <a href="time.html" title="time — Time access and conversions"
  1410. >next</a> |</li>
  1411. <li class="right" >
  1412. <a href="os.html" title="os — Miscellaneous operating system interfaces"
  1413. >previous</a> |</li>
  1414. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1415. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1416. <li class="switchers">
  1417. <div class="language_switcher_placeholder"></div>
  1418. <div class="version_switcher_placeholder"></div>
  1419. </li>
  1420. <li>
  1421. </li>
  1422. <li id="cpython-language-and-version">
  1423. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1424. </li>
  1425. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1426. <li class="nav-item nav-item-2"><a href="allos.html" >Generic Operating System Services</a> &#187;</li>
  1427. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code> — Core tools for working with streams</a></li>
  1428. <li class="right">
  1429. <div class="inline-search" role="search">
  1430. <form class="inline-search" action="../search.html" method="get">
  1431. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1432. <input type="submit" value="Go" />
  1433. </form>
  1434. </div>
  1435. |
  1436. </li>
  1437. <li class="right">
  1438. <label class="theme-selector-label">
  1439. Theme
  1440. <select class="theme-selector" oninput="activateTheme(this.value)">
  1441. <option value="auto" selected>Auto</option>
  1442. <option value="light">Light</option>
  1443. <option value="dark">Dark</option>
  1444. </select>
  1445. </label> |</li>
  1446. </ul>
  1447. </div>
  1448. <div class="footer">
  1449. &copy;
  1450. <a href="../copyright.html">
  1451. Copyright
  1452. </a>
  1453. 2001-2024, Python Software Foundation.
  1454. <br />
  1455. This page is licensed under the Python Software Foundation License Version 2.
  1456. <br />
  1457. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1458. <br />
  1459. See <a href="/license.html">History and License</a> for more information.<br />
  1460. <br />
  1461. The Python Software Foundation is a non-profit corporation.
  1462. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1463. <br />
  1464. <br />
  1465. Last updated on Apr 09, 2024 (13:47 UTC).
  1466. <a href="/bugs.html">Found a bug</a>?
  1467. <br />
  1468. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1469. </div>
  1470. </body>
  1471. </html>
上海开阖软件有限公司 沪ICP备12045867号-1