inputoutput.html 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="7. Input and Output" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/tutorial/inputoutput.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the possibilities. Fa..." />
  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="There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the possibilities. Fa..." />
  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>7. Input and Output &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="8. Errors and Exceptions" href="errors.html" />
  34. <link rel="prev" title="6. Modules" href="modules.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/tutorial/inputoutput.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.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="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="#">7. Input and Output</a><ul>
  86. <li><a class="reference internal" href="#fancier-output-formatting">7.1. Fancier Output Formatting</a><ul>
  87. <li><a class="reference internal" href="#formatted-string-literals">7.1.1. Formatted String Literals</a></li>
  88. <li><a class="reference internal" href="#the-string-format-method">7.1.2. The String format() Method</a></li>
  89. <li><a class="reference internal" href="#manual-string-formatting">7.1.3. Manual String Formatting</a></li>
  90. <li><a class="reference internal" href="#old-string-formatting">7.1.4. Old string formatting</a></li>
  91. </ul>
  92. </li>
  93. <li><a class="reference internal" href="#reading-and-writing-files">7.2. Reading and Writing Files</a><ul>
  94. <li><a class="reference internal" href="#methods-of-file-objects">7.2.1. Methods of File Objects</a></li>
  95. <li><a class="reference internal" href="#saving-structured-data-with-json">7.2.2. Saving structured data with <code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a></li>
  96. </ul>
  97. </li>
  98. </ul>
  99. </li>
  100. </ul>
  101. </div>
  102. <div>
  103. <h4>Previous topic</h4>
  104. <p class="topless"><a href="modules.html"
  105. title="previous chapter"><span class="section-number">6. </span>Modules</a></p>
  106. </div>
  107. <div>
  108. <h4>Next topic</h4>
  109. <p class="topless"><a href="errors.html"
  110. title="next chapter"><span class="section-number">8. </span>Errors and Exceptions</a></p>
  111. </div>
  112. <div role="note" aria-label="source link">
  113. <h3>This Page</h3>
  114. <ul class="this-page-menu">
  115. <li><a href="../bugs.html">Report a Bug</a></li>
  116. <li>
  117. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/inputoutput.rst"
  118. rel="nofollow">Show Source
  119. </a>
  120. </li>
  121. </ul>
  122. </div>
  123. </nav>
  124. </div>
  125. </div>
  126. <div class="related" role="navigation" aria-label="related navigation">
  127. <h3>Navigation</h3>
  128. <ul>
  129. <li class="right" style="margin-right: 10px">
  130. <a href="../genindex.html" title="General Index"
  131. accesskey="I">index</a></li>
  132. <li class="right" >
  133. <a href="../py-modindex.html" title="Python Module Index"
  134. >modules</a> |</li>
  135. <li class="right" >
  136. <a href="errors.html" title="8. Errors and Exceptions"
  137. accesskey="N">next</a> |</li>
  138. <li class="right" >
  139. <a href="modules.html" title="6. Modules"
  140. accesskey="P">previous</a> |</li>
  141. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  142. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  143. <li class="switchers">
  144. <div class="language_switcher_placeholder"></div>
  145. <div class="version_switcher_placeholder"></div>
  146. </li>
  147. <li>
  148. </li>
  149. <li id="cpython-language-and-version">
  150. <a href="../index.html">3.12.0 Documentation</a> &#187;
  151. </li>
  152. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Tutorial</a> &#187;</li>
  153. <li class="nav-item nav-item-this"><a href=""><span class="section-number">7. </span>Input and Output</a></li>
  154. <li class="right">
  155. <div class="inline-search" role="search">
  156. <form class="inline-search" action="../search.html" method="get">
  157. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  158. <input type="submit" value="Go" />
  159. </form>
  160. </div>
  161. |
  162. </li>
  163. <li class="right">
  164. <label class="theme-selector-label">
  165. Theme
  166. <select class="theme-selector" oninput="activateTheme(this.value)">
  167. <option value="auto" selected>Auto</option>
  168. <option value="light">Light</option>
  169. <option value="dark">Dark</option>
  170. </select>
  171. </label> |</li>
  172. </ul>
  173. </div>
  174. <div class="document">
  175. <div class="documentwrapper">
  176. <div class="bodywrapper">
  177. <div class="body" role="main">
  178. <section id="input-and-output">
  179. <span id="tut-io"></span><h1><span class="section-number">7. </span>Input and Output<a class="headerlink" href="#input-and-output" title="Permalink to this headline">¶</a></h1>
  180. <p>There are several ways to present the output of a program; data can be printed
  181. in a human-readable form, or written to a file for future use. This chapter will
  182. discuss some of the possibilities.</p>
  183. <section id="fancier-output-formatting">
  184. <span id="tut-formatting"></span><h2><span class="section-number">7.1. </span>Fancier Output Formatting<a class="headerlink" href="#fancier-output-formatting" title="Permalink to this headline">¶</a></h2>
  185. <p>So far we’ve encountered two ways of writing values: <em>expression statements</em> and
  186. the <a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> function. (A third way is using the <a class="reference internal" href="../library/io.html#io.TextIOBase.write" title="io.TextIOBase.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> method
  187. of file objects; the standard output file can be referenced as <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>.
  188. See the Library Reference for more information on this.)</p>
  189. <p>Often you’ll want more control over the formatting of your output than simply
  190. printing space-separated values. There are several ways to format output.</p>
  191. <ul>
  192. <li><p>To use <a class="reference internal" href="#tut-f-strings"><span class="std std-ref">formatted string literals</span></a>, begin a string
  193. with <code class="docutils literal notranslate"><span class="pre">f</span></code> or <code class="docutils literal notranslate"><span class="pre">F</span></code> before the opening quotation mark or triple quotation mark.
  194. Inside this string, you can write a Python expression between <code class="docutils literal notranslate"><span class="pre">{</span></code> and <code class="docutils literal notranslate"><span class="pre">}</span></code>
  195. characters that can refer to variables or literal values.</p>
  196. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">year</span> <span class="o">=</span> <span class="mi">2016</span>
  197. <span class="gp">&gt;&gt;&gt; </span><span class="n">event</span> <span class="o">=</span> <span class="s1">&#39;Referendum&#39;</span>
  198. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s1">&#39;Results of the </span><span class="si">{</span><span class="n">year</span><span class="si">}</span><span class="s1"> </span><span class="si">{</span><span class="n">event</span><span class="si">}</span><span class="s1">&#39;</span>
  199. <span class="go">&#39;Results of the 2016 Referendum&#39;</span>
  200. </pre></div>
  201. </div>
  202. </li>
  203. <li><p>The <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method of strings requires more manual
  204. effort. You’ll still use <code class="docutils literal notranslate"><span class="pre">{</span></code> and <code class="docutils literal notranslate"><span class="pre">}</span></code> to mark where a variable
  205. will be substituted and can provide detailed formatting directives,
  206. but you’ll also need to provide the information to be formatted.</p>
  207. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">yes_votes</span> <span class="o">=</span> <span class="mi">42_572_654</span>
  208. <span class="gp">&gt;&gt;&gt; </span><span class="n">no_votes</span> <span class="o">=</span> <span class="mi">43_132_495</span>
  209. <span class="gp">&gt;&gt;&gt; </span><span class="n">percentage</span> <span class="o">=</span> <span class="n">yes_votes</span> <span class="o">/</span> <span class="p">(</span><span class="n">yes_votes</span> <span class="o">+</span> <span class="n">no_votes</span><span class="p">)</span>
  210. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:-9}</span><span class="s1"> YES votes </span><span class="si">{:2.2%}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">yes_votes</span><span class="p">,</span> <span class="n">percentage</span><span class="p">)</span>
  211. <span class="go">&#39; 42572654 YES votes 49.67%&#39;</span>
  212. </pre></div>
  213. </div>
  214. </li>
  215. <li><p>Finally, you can do all the string handling yourself by using string slicing and
  216. concatenation operations to create any layout you can imagine. The
  217. string type has some methods that perform useful operations for padding
  218. strings to a given column width.</p></li>
  219. </ul>
  220. <p>When you don’t need fancy output but just want a quick display of some
  221. variables for debugging purposes, you can convert any value to a string with
  222. the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> or <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> functions.</p>
  223. <p>The <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> function is meant to return representations of values which are
  224. fairly human-readable, while <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> is meant to generate representations
  225. which can be read by the interpreter (or will force a <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> if
  226. there is no equivalent syntax). For objects which don’t have a particular
  227. representation for human consumption, <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> will return the same value as
  228. <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a>. Many values, such as numbers or structures like lists and
  229. dictionaries, have the same representation using either function. Strings, in
  230. particular, have two distinct representations.</p>
  231. <p>Some examples:</p>
  232. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;Hello, world.&#39;</span>
  233. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  234. <span class="go">&#39;Hello, world.&#39;</span>
  235. <span class="gp">&gt;&gt;&gt; </span><span class="nb">repr</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  236. <span class="go">&quot;&#39;Hello, world.&#39;&quot;</span>
  237. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mi">7</span><span class="p">)</span>
  238. <span class="go">&#39;0.14285714285714285&#39;</span>
  239. <span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="mf">3.25</span>
  240. <span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="mi">200</span> <span class="o">*</span> <span class="mi">200</span>
  241. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;The value of x is &#39;</span> <span class="o">+</span> <span class="nb">repr</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;, and y is &#39;</span> <span class="o">+</span> <span class="nb">repr</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;...&#39;</span>
  242. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  243. <span class="go">The value of x is 32.5, and y is 40000...</span>
  244. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># The repr() of a string adds string quotes and backslashes:</span>
  245. <span class="gp">... </span><span class="n">hello</span> <span class="o">=</span> <span class="s1">&#39;hello, world</span><span class="se">\n</span><span class="s1">&#39;</span>
  246. <span class="gp">&gt;&gt;&gt; </span><span class="n">hellos</span> <span class="o">=</span> <span class="nb">repr</span><span class="p">(</span><span class="n">hello</span><span class="p">)</span>
  247. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">hellos</span><span class="p">)</span>
  248. <span class="go">&#39;hello, world\n&#39;</span>
  249. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># The argument to repr() may be any Python object:</span>
  250. <span class="gp">... </span><span class="nb">repr</span><span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">)))</span>
  251. <span class="go">&quot;(32.5, 40000, (&#39;spam&#39;, &#39;eggs&#39;))&quot;</span>
  252. </pre></div>
  253. </div>
  254. <p>The <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module contains a <a class="reference internal" href="../library/string.html#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> class that offers
  255. yet another way to substitute values into strings, using placeholders like
  256. <code class="docutils literal notranslate"><span class="pre">$x</span></code> and replacing them with values from a dictionary, but offers much less
  257. control of the formatting.</p>
  258. <section id="formatted-string-literals">
  259. <span id="tut-f-strings"></span><h3><span class="section-number">7.1.1. </span>Formatted String Literals<a class="headerlink" href="#formatted-string-literals" title="Permalink to this headline">¶</a></h3>
  260. <p><a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">Formatted string literals</span></a> (also called f-strings for
  261. short) let you include the value of Python expressions inside a string by
  262. prefixing the string with <code class="docutils literal notranslate"><span class="pre">f</span></code> or <code class="docutils literal notranslate"><span class="pre">F</span></code> and writing expressions as
  263. <code class="docutils literal notranslate"><span class="pre">{expression}</span></code>.</p>
  264. <p>An optional format specifier can follow the expression. This allows greater
  265. control over how the value is formatted. The following example rounds pi to
  266. three places after the decimal:</p>
  267. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
  268. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;The value of pi is approximately </span><span class="si">{</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="si">:</span><span class="s1">.3f</span><span class="si">}</span><span class="s1">.&#39;</span><span class="p">)</span>
  269. <span class="go">The value of pi is approximately 3.142.</span>
  270. </pre></div>
  271. </div>
  272. <p>Passing an integer after the <code class="docutils literal notranslate"><span class="pre">':'</span></code> will cause that field to be a minimum
  273. number of characters wide. This is useful for making columns line up.</p>
  274. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Sjoerd&#39;</span><span class="p">:</span> <span class="mi">4127</span><span class="p">,</span> <span class="s1">&#39;Jack&#39;</span><span class="p">:</span> <span class="mi">4098</span><span class="p">,</span> <span class="s1">&#39;Dcab&#39;</span><span class="p">:</span> <span class="mi">7678</span><span class="p">}</span>
  275. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">phone</span> <span class="ow">in</span> <span class="n">table</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
  276. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">name</span><span class="si">:</span><span class="s1">10</span><span class="si">}</span><span class="s1"> ==&gt; </span><span class="si">{</span><span class="n">phone</span><span class="si">:</span><span class="s1">10d</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
  277. <span class="gp">...</span>
  278. <span class="go">Sjoerd ==&gt; 4127</span>
  279. <span class="go">Jack ==&gt; 4098</span>
  280. <span class="go">Dcab ==&gt; 7678</span>
  281. </pre></div>
  282. </div>
  283. <p>Other modifiers can be used to convert the value before it is formatted.
  284. <code class="docutils literal notranslate"><span class="pre">'!a'</span></code> applies <a class="reference internal" href="../library/functions.html#ascii" title="ascii"><code class="xref py py-func docutils literal notranslate"><span class="pre">ascii()</span></code></a>, <code class="docutils literal notranslate"><span class="pre">'!s'</span></code> applies <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>, and <code class="docutils literal notranslate"><span class="pre">'!r'</span></code>
  285. applies <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a>:</p>
  286. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">animals</span> <span class="o">=</span> <span class="s1">&#39;eels&#39;</span>
  287. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;My hovercraft is full of </span><span class="si">{</span><span class="n">animals</span><span class="si">}</span><span class="s1">.&#39;</span><span class="p">)</span>
  288. <span class="go">My hovercraft is full of eels.</span>
  289. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;My hovercraft is full of </span><span class="si">{</span><span class="n">animals</span><span class="si">!r}</span><span class="s1">.&#39;</span><span class="p">)</span>
  290. <span class="go">My hovercraft is full of &#39;eels&#39;.</span>
  291. </pre></div>
  292. </div>
  293. <p>The <code class="docutils literal notranslate"><span class="pre">=</span></code> specifier can be used to expand an expression to the text of the
  294. expression, an equal sign, then the representation of the evaluated expression:</p>
  295. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">bugs</span> <span class="o">=</span> <span class="s1">&#39;roaches&#39;</span>
  296. <span class="gp">&gt;&gt;&gt; </span><span class="n">count</span> <span class="o">=</span> <span class="mi">13</span>
  297. <span class="gp">&gt;&gt;&gt; </span><span class="n">area</span> <span class="o">=</span> <span class="s1">&#39;living room&#39;</span>
  298. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Debugging </span><span class="si">{</span><span class="n">bugs</span><span class="si">=}</span><span class="s1"> </span><span class="si">{</span><span class="n">count</span><span class="si">=}</span><span class="s1"> </span><span class="si">{</span><span class="n">area</span><span class="si">=}</span><span class="s1">&#39;</span><span class="p">)</span>
  299. <span class="go">Debugging bugs=&#39;roaches&#39; count=13 area=&#39;living room&#39;</span>
  300. </pre></div>
  301. </div>
  302. <p>See <a class="reference internal" href="../whatsnew/3.8.html#bpo-36817-whatsnew"><span class="std std-ref">self-documenting expressions</span></a> for more information
  303. on the <code class="docutils literal notranslate"><span class="pre">=</span></code> specifier. For a reference on these format specifications, see
  304. the reference guide for the <a class="reference internal" href="../library/string.html#formatspec"><span class="std std-ref">Format Specification Mini-Language</span></a>.</p>
  305. </section>
  306. <section id="the-string-format-method">
  307. <span id="tut-string-format"></span><h3><span class="section-number">7.1.2. </span>The String format() Method<a class="headerlink" href="#the-string-format-method" title="Permalink to this headline">¶</a></h3>
  308. <p>Basic usage of the <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method looks like this:</p>
  309. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;We are the </span><span class="si">{}</span><span class="s1"> who say &quot;</span><span class="si">{}</span><span class="s1">!&quot;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;knights&#39;</span><span class="p">,</span> <span class="s1">&#39;Ni&#39;</span><span class="p">))</span>
  310. <span class="go">We are the knights who say &quot;Ni!&quot;</span>
  311. </pre></div>
  312. </div>
  313. <p>The brackets and characters within them (called format fields) are replaced with
  314. the objects passed into the <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method. A number in the
  315. brackets can be used to refer to the position of the object passed into the
  316. <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method.</p>
  317. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{0}</span><span class="s1"> and </span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">))</span>
  318. <span class="go">spam and eggs</span>
  319. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{1}</span><span class="s1"> and </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">))</span>
  320. <span class="go">eggs and spam</span>
  321. </pre></div>
  322. </div>
  323. <p>If keyword arguments are used in the <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method, their values
  324. are referred to by using the name of the argument.</p>
  325. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;This </span><span class="si">{food}</span><span class="s1"> is </span><span class="si">{adjective}</span><span class="s1">.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
  326. <span class="gp">... </span> <span class="n">food</span><span class="o">=</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="n">adjective</span><span class="o">=</span><span class="s1">&#39;absolutely horrible&#39;</span><span class="p">))</span>
  327. <span class="go">This spam is absolutely horrible.</span>
  328. </pre></div>
  329. </div>
  330. <p>Positional and keyword arguments can be arbitrarily combined:</p>
  331. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;The story of </span><span class="si">{0}</span><span class="s1">, </span><span class="si">{1}</span><span class="s1">, and </span><span class="si">{other}</span><span class="s1">.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;Bill&#39;</span><span class="p">,</span> <span class="s1">&#39;Manfred&#39;</span><span class="p">,</span>
  332. <span class="gp">... </span> <span class="n">other</span><span class="o">=</span><span class="s1">&#39;Georg&#39;</span><span class="p">))</span>
  333. <span class="go">The story of Bill, Manfred, and Georg.</span>
  334. </pre></div>
  335. </div>
  336. <p>If you have a really long format string that you don’t want to split up, it
  337. would be nice if you could reference the variables to be formatted by name
  338. instead of by position. This can be done by simply passing the dict and using
  339. square brackets <code class="docutils literal notranslate"><span class="pre">'[]'</span></code> to access the keys.</p>
  340. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Sjoerd&#39;</span><span class="p">:</span> <span class="mi">4127</span><span class="p">,</span> <span class="s1">&#39;Jack&#39;</span><span class="p">:</span> <span class="mi">4098</span><span class="p">,</span> <span class="s1">&#39;Dcab&#39;</span><span class="p">:</span> <span class="mi">8637678</span><span class="p">}</span>
  341. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Jack: </span><span class="si">{0[Jack]:d}</span><span class="s1">; Sjoerd: </span><span class="si">{0[Sjoerd]:d}</span><span class="s1">; &#39;</span>
  342. <span class="gp">... </span> <span class="s1">&#39;Dcab: </span><span class="si">{0[Dcab]:d}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">table</span><span class="p">))</span>
  343. <span class="go">Jack: 4098; Sjoerd: 4127; Dcab: 8637678</span>
  344. </pre></div>
  345. </div>
  346. <p>This could also be done by passing the <code class="docutils literal notranslate"><span class="pre">table</span></code> dictionary as keyword arguments with the <code class="docutils literal notranslate"><span class="pre">**</span></code>
  347. notation.</p>
  348. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Sjoerd&#39;</span><span class="p">:</span> <span class="mi">4127</span><span class="p">,</span> <span class="s1">&#39;Jack&#39;</span><span class="p">:</span> <span class="mi">4098</span><span class="p">,</span> <span class="s1">&#39;Dcab&#39;</span><span class="p">:</span> <span class="mi">8637678</span><span class="p">}</span>
  349. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Jack: </span><span class="si">{Jack:d}</span><span class="s1">; Sjoerd: </span><span class="si">{Sjoerd:d}</span><span class="s1">; Dcab: </span><span class="si">{Dcab:d}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">**</span><span class="n">table</span><span class="p">))</span>
  350. <span class="go">Jack: 4098; Sjoerd: 4127; Dcab: 8637678</span>
  351. </pre></div>
  352. </div>
  353. <p>This is particularly useful in combination with the built-in function
  354. <a class="reference internal" href="../library/functions.html#vars" title="vars"><code class="xref py py-func docutils literal notranslate"><span class="pre">vars()</span></code></a>, which returns a dictionary containing all local variables.</p>
  355. <p>As an example, the following lines produce a tidily aligned
  356. set of columns giving integers and their squares and cubes:</p>
  357. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">11</span><span class="p">):</span>
  358. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{0:2d}</span><span class="s1"> </span><span class="si">{1:3d}</span><span class="s1"> </span><span class="si">{2:4d}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="p">))</span>
  359. <span class="gp">...</span>
  360. <span class="go"> 1 1 1</span>
  361. <span class="go"> 2 4 8</span>
  362. <span class="go"> 3 9 27</span>
  363. <span class="go"> 4 16 64</span>
  364. <span class="go"> 5 25 125</span>
  365. <span class="go"> 6 36 216</span>
  366. <span class="go"> 7 49 343</span>
  367. <span class="go"> 8 64 512</span>
  368. <span class="go"> 9 81 729</span>
  369. <span class="go">10 100 1000</span>
  370. </pre></div>
  371. </div>
  372. <p>For a complete overview of string formatting with <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>, see
  373. <a class="reference internal" href="../library/string.html#formatstrings"><span class="std std-ref">Format String Syntax</span></a>.</p>
  374. </section>
  375. <section id="manual-string-formatting">
  376. <h3><span class="section-number">7.1.3. </span>Manual String Formatting<a class="headerlink" href="#manual-string-formatting" title="Permalink to this headline">¶</a></h3>
  377. <p>Here’s the same table of squares and cubes, formatted manually:</p>
  378. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">11</span><span class="p">):</span>
  379. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="nb">repr</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
  380. <span class="gp">... </span> <span class="c1"># Note use of &#39;end&#39; on previous line</span>
  381. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
  382. <span class="gp">...</span>
  383. <span class="go"> 1 1 1</span>
  384. <span class="go"> 2 4 8</span>
  385. <span class="go"> 3 9 27</span>
  386. <span class="go"> 4 16 64</span>
  387. <span class="go"> 5 25 125</span>
  388. <span class="go"> 6 36 216</span>
  389. <span class="go"> 7 49 343</span>
  390. <span class="go"> 8 64 512</span>
  391. <span class="go"> 9 81 729</span>
  392. <span class="go">10 100 1000</span>
  393. </pre></div>
  394. </div>
  395. <p>(Note that the one space between each column was added by the
  396. way <a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> works: it always adds spaces between its arguments.)</p>
  397. <p>The <a class="reference internal" href="../library/stdtypes.html#str.rjust" title="str.rjust"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.rjust()</span></code></a> method of string objects right-justifies a string in a
  398. field of a given width by padding it with spaces on the left. There are
  399. similar methods <a class="reference internal" href="../library/stdtypes.html#str.ljust" title="str.ljust"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.ljust()</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#str.center" title="str.center"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.center()</span></code></a>. These methods do
  400. not write anything, they just return a new string. If the input string is too
  401. long, they don’t truncate it, but return it unchanged; this will mess up your
  402. column lay-out but that’s usually better than the alternative, which would be
  403. lying about a value. (If you really want truncation you can always add a
  404. slice operation, as in <code class="docutils literal notranslate"><span class="pre">x.ljust(n)[:n]</span></code>.)</p>
  405. <p>There is another method, <a class="reference internal" href="../library/stdtypes.html#str.zfill" title="str.zfill"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.zfill()</span></code></a>, which pads a numeric string on the
  406. left with zeros. It understands about plus and minus signs:</p>
  407. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;12&#39;</span><span class="o">.</span><span class="n">zfill</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  408. <span class="go">&#39;00012&#39;</span>
  409. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;-3.14&#39;</span><span class="o">.</span><span class="n">zfill</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
  410. <span class="go">&#39;-003.14&#39;</span>
  411. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;3.14159265359&#39;</span><span class="o">.</span><span class="n">zfill</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  412. <span class="go">&#39;3.14159265359&#39;</span>
  413. </pre></div>
  414. </div>
  415. </section>
  416. <section id="old-string-formatting">
  417. <h3><span class="section-number">7.1.4. </span>Old string formatting<a class="headerlink" href="#old-string-formatting" title="Permalink to this headline">¶</a></h3>
  418. <p>The % operator (modulo) can also be used for string formatting. Given <code class="docutils literal notranslate"><span class="pre">'string'</span>
  419. <span class="pre">%</span> <span class="pre">values</span></code>, instances of <code class="docutils literal notranslate"><span class="pre">%</span></code> in <code class="docutils literal notranslate"><span class="pre">string</span></code> are replaced with zero or more
  420. elements of <code class="docutils literal notranslate"><span class="pre">values</span></code>. This operation is commonly known as string
  421. interpolation. For example:</p>
  422. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
  423. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;The value of pi is approximately </span><span class="si">%5.3f</span><span class="s1">.&#39;</span> <span class="o">%</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
  424. <span class="go">The value of pi is approximately 3.142.</span>
  425. </pre></div>
  426. </div>
  427. <p>More information can be found in the <a class="reference internal" href="../library/stdtypes.html#old-string-formatting"><span class="std std-ref">printf-style String Formatting</span></a> section.</p>
  428. </section>
  429. </section>
  430. <section id="reading-and-writing-files">
  431. <span id="tut-files"></span><h2><span class="section-number">7.2. </span>Reading and Writing Files<a class="headerlink" href="#reading-and-writing-files" title="Permalink to this headline">¶</a></h2>
  432. <p id="index-0"><a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> returns a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>, and is most commonly used with
  433. two positional arguments and one keyword argument:
  434. <code class="docutils literal notranslate"><span class="pre">open(filename,</span> <span class="pre">mode,</span> <span class="pre">encoding=None)</span></code></p>
  435. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;workfile&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</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>
  436. </pre></div>
  437. </div>
  438. <p>The first argument is a string containing the filename. The second argument is
  439. another string containing a few characters describing the way in which the file
  440. will be used. <em>mode</em> can be <code class="docutils literal notranslate"><span class="pre">'r'</span></code> when the file will only be read, <code class="docutils literal notranslate"><span class="pre">'w'</span></code>
  441. for only writing (an existing file with the same name will be erased), and
  442. <code class="docutils literal notranslate"><span class="pre">'a'</span></code> opens the file for appending; any data written to the file is
  443. automatically added to the end. <code class="docutils literal notranslate"><span class="pre">'r+'</span></code> opens the file for both reading and
  444. writing. The <em>mode</em> argument is optional; <code class="docutils literal notranslate"><span class="pre">'r'</span></code> will be assumed if it’s
  445. omitted.</p>
  446. <p>Normally, files are opened in <em class="dfn">text mode</em>, that means, you read and write
  447. strings from and to the file, which are encoded in a specific <em>encoding</em>.
  448. If <em>encoding</em> is not specified, the default is platform dependent
  449. (see <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>).
  450. Because UTF-8 is the modern de-facto standard, <code class="docutils literal notranslate"><span class="pre">encoding=&quot;utf-8&quot;</span></code> is
  451. recommended unless you know that you need to use a different encoding.
  452. Appending a <code class="docutils literal notranslate"><span class="pre">'b'</span></code> to the mode opens the file in <em class="dfn">binary mode</em>.
  453. Binary mode data is read and written as <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> objects.
  454. You can not specify <em>encoding</em> when opening file in binary mode.</p>
  455. <p>In text mode, the default when reading is to convert platform-specific line
  456. endings (<code class="docutils literal notranslate"><span class="pre">\n</span></code> on Unix, <code class="docutils literal notranslate"><span class="pre">\r\n</span></code> on Windows) to just <code class="docutils literal notranslate"><span class="pre">\n</span></code>. When writing in
  457. text mode, the default is to convert occurrences of <code class="docutils literal notranslate"><span class="pre">\n</span></code> back to
  458. platform-specific line endings. This behind-the-scenes modification
  459. to file data is fine for text files, but will corrupt binary data like that in
  460. <code class="file docutils literal notranslate"><span class="pre">JPEG</span></code> or <code class="file docutils literal notranslate"><span class="pre">EXE</span></code> files. Be very careful to use binary mode when
  461. reading and writing such files.</p>
  462. <p>It is good practice to use the <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> keyword when dealing
  463. with file objects. The advantage is that the file is properly closed
  464. after its suite finishes, even if an exception is raised at some
  465. point. Using <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> is also much shorter than writing
  466. equivalent <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a>-<a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> blocks:</p>
  467. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;workfile&#39;</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> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  468. <span class="gp">... </span> <span class="n">read_data</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  469. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># We can check that the file has been automatically closed.</span>
  470. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">closed</span>
  471. <span class="go">True</span>
  472. </pre></div>
  473. </div>
  474. <p>If you’re not using the <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> keyword, then you should call
  475. <code class="docutils literal notranslate"><span class="pre">f.close()</span></code> to close the file and immediately free up any system
  476. resources used by it.</p>
  477. <div class="admonition warning">
  478. <p class="admonition-title">Warning</p>
  479. <p>Calling <code class="docutils literal notranslate"><span class="pre">f.write()</span></code> without using the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> keyword or calling
  480. <code class="docutils literal notranslate"><span class="pre">f.close()</span></code> <strong>might</strong> result in the arguments
  481. of <code class="docutils literal notranslate"><span class="pre">f.write()</span></code> not being completely written to the disk, even if the
  482. program exits successfully.</p>
  483. </div>
  484. <p>After a file object is closed, either by a <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
  485. or by calling <code class="docutils literal notranslate"><span class="pre">f.close()</span></code>, attempts to use the file object will
  486. automatically fail.</p>
  487. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  488. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  489. <span class="gt">Traceback (most recent call last):</span>
  490. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  491. <span class="gr">ValueError</span>: <span class="n">I/O operation on closed file.</span>
  492. </pre></div>
  493. </div>
  494. <section id="methods-of-file-objects">
  495. <span id="tut-filemethods"></span><h3><span class="section-number">7.2.1. </span>Methods of File Objects<a class="headerlink" href="#methods-of-file-objects" title="Permalink to this headline">¶</a></h3>
  496. <p>The rest of the examples in this section will assume that a file object called
  497. <code class="docutils literal notranslate"><span class="pre">f</span></code> has already been created.</p>
  498. <p>To read a file’s contents, call <code class="docutils literal notranslate"><span class="pre">f.read(size)</span></code>, which reads some quantity of
  499. data and returns it as a string (in text mode) or bytes object (in binary mode).
  500. <em>size</em> is an optional numeric argument. When <em>size</em> is omitted or negative, the
  501. entire contents of the file will be read and returned; it’s your problem if the
  502. file is twice as large as your machine’s memory. Otherwise, at most <em>size</em>
  503. characters (in text mode) or <em>size</em> bytes (in binary mode) are read and returned.
  504. If the end of the file has been reached, <code class="docutils literal notranslate"><span class="pre">f.read()</span></code> will return an empty
  505. string (<code class="docutils literal notranslate"><span class="pre">''</span></code>).</p>
  506. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  507. <span class="go">&#39;This is the entire file.\n&#39;</span>
  508. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  509. <span class="go">&#39;&#39;</span>
  510. </pre></div>
  511. </div>
  512. <p><code class="docutils literal notranslate"><span class="pre">f.readline()</span></code> reads a single line from the file; a newline character (<code class="docutils literal notranslate"><span class="pre">\n</span></code>)
  513. is left at the end of the string, and is only omitted on the last line of the
  514. file if the file doesn’t end in a newline. This makes the return value
  515. unambiguous; if <code class="docutils literal notranslate"><span class="pre">f.readline()</span></code> returns an empty string, the end of the file
  516. has been reached, while a blank line is represented by <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>, a string
  517. containing only a single newline.</p>
  518. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
  519. <span class="go">&#39;This is the first line of the file.\n&#39;</span>
  520. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
  521. <span class="go">&#39;Second line of the file\n&#39;</span>
  522. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
  523. <span class="go">&#39;&#39;</span>
  524. </pre></div>
  525. </div>
  526. <p>For reading lines from a file, you can loop over the file object. This is memory
  527. efficient, fast, and leads to simple code:</p>
  528. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
  529. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">)</span>
  530. <span class="gp">...</span>
  531. <span class="go">This is the first line of the file.</span>
  532. <span class="go">Second line of the file</span>
  533. </pre></div>
  534. </div>
  535. <p>If you want to read all the lines of a file in a list you can also use
  536. <code class="docutils literal notranslate"><span class="pre">list(f)</span></code> or <code class="docutils literal notranslate"><span class="pre">f.readlines()</span></code>.</p>
  537. <p><code class="docutils literal notranslate"><span class="pre">f.write(string)</span></code> writes the contents of <em>string</em> to the file, returning
  538. the number of characters written.</p>
  539. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;This is a test</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
  540. <span class="go">15</span>
  541. </pre></div>
  542. </div>
  543. <p>Other types of objects need to be converted – either to a string (in text mode)
  544. or a bytes object (in binary mode) – before writing them:</p>
  545. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">value</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;the answer&#39;</span><span class="p">,</span> <span class="mi">42</span><span class="p">)</span>
  546. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="c1"># convert the tuple to string</span>
  547. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  548. <span class="go">18</span>
  549. </pre></div>
  550. </div>
  551. <p><code class="docutils literal notranslate"><span class="pre">f.tell()</span></code> returns an integer giving the file object’s current position in the file
  552. represented as number of bytes from the beginning of the file when in binary mode and
  553. an opaque number when in text mode.</p>
  554. <p>To change the file object’s position, use <code class="docutils literal notranslate"><span class="pre">f.seek(offset,</span> <span class="pre">whence)</span></code>. The position is computed
  555. from adding <em>offset</em> to a reference point; the reference point is selected by
  556. the <em>whence</em> argument. A <em>whence</em> value of 0 measures from the beginning
  557. of the file, 1 uses the current file position, and 2 uses the end of the file as
  558. the reference point. <em>whence</em> can be omitted and defaults to 0, using the
  559. beginning of the file as the reference point.</p>
  560. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;workfile&#39;</span><span class="p">,</span> <span class="s1">&#39;rb+&#39;</span><span class="p">)</span>
  561. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;0123456789abcdef&#39;</span><span class="p">)</span>
  562. <span class="go">16</span>
  563. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="c1"># Go to the 6th byte in the file</span>
  564. <span class="go">5</span>
  565. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  566. <span class="go">b&#39;5&#39;</span>
  567. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1"># Go to the 3rd byte before the end</span>
  568. <span class="go">13</span>
  569. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  570. <span class="go">b&#39;d&#39;</span>
  571. </pre></div>
  572. </div>
  573. <p>In text files (those opened without a <code class="docutils literal notranslate"><span class="pre">b</span></code> in the mode string), only seeks
  574. relative to the beginning of the file are allowed (the exception being seeking
  575. to the very file end with <code class="docutils literal notranslate"><span class="pre">seek(0,</span> <span class="pre">2)</span></code>) and the only valid <em>offset</em> values are
  576. those returned from the <code class="docutils literal notranslate"><span class="pre">f.tell()</span></code>, or zero. Any other <em>offset</em> value produces
  577. undefined behaviour.</p>
  578. <p>File objects have some additional methods, such as <a class="reference internal" href="../library/io.html#io.IOBase.isatty" title="io.IOBase.isatty"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isatty()</span></code></a> and
  579. <a class="reference internal" href="../library/io.html#io.IOBase.truncate" title="io.IOBase.truncate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">truncate()</span></code></a> which are less frequently used; consult the Library
  580. Reference for a complete guide to file objects.</p>
  581. </section>
  582. <section id="saving-structured-data-with-json">
  583. <span id="tut-json"></span><h3><span class="section-number">7.2.2. </span>Saving structured data with <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a><a class="headerlink" href="#saving-structured-data-with-json" title="Permalink to this headline">¶</a></h3>
  584. <p id="index-1">Strings can easily be written to and read from a file. Numbers take a bit more
  585. effort, since the <a class="reference internal" href="../library/io.html#io.TextIOBase.read" title="io.TextIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> method only returns strings, which will have to
  586. be passed to a function like <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a>, which takes a string like <code class="docutils literal notranslate"><span class="pre">'123'</span></code>
  587. and returns its numeric value 123. When you want to save more complex data
  588. types like nested lists and dictionaries, parsing and serializing by hand
  589. becomes complicated.</p>
  590. <p>Rather than having users constantly writing and debugging code to save
  591. complicated data types to files, Python allows you to use the popular data
  592. interchange format called <a class="reference external" href="https://json.org">JSON (JavaScript Object Notation)</a>. The standard module called <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> can take Python
  593. data hierarchies, and convert them to string representations; this process is
  594. called <em class="dfn">serializing</em>. Reconstructing the data from the string representation
  595. is called <em class="dfn">deserializing</em>. Between serializing and deserializing, the
  596. string representing the object may have been stored in a file or data, or
  597. sent over a network connection to some distant machine.</p>
  598. <div class="admonition note">
  599. <p class="admonition-title">Note</p>
  600. <p>The JSON format is commonly used by modern applications to allow for data
  601. exchange. Many programmers are already familiar with it, which makes
  602. it a good choice for interoperability.</p>
  603. </div>
  604. <p>If you have an object <code class="docutils literal notranslate"><span class="pre">x</span></code>, you can view its JSON string representation with a
  605. simple line of code:</p>
  606. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
  607. <span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;simple&#39;</span><span class="p">,</span> <span class="s1">&#39;list&#39;</span><span class="p">]</span>
  608. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
  609. <span class="go">&#39;[1, &quot;simple&quot;, &quot;list&quot;]&#39;</span>
  610. </pre></div>
  611. </div>
  612. <p>Another variant of the <a class="reference internal" href="../library/json.html#json.dumps" title="json.dumps"><code class="xref py py-func docutils literal notranslate"><span class="pre">dumps()</span></code></a> function, called <a class="reference internal" href="../library/json.html#json.dump" title="json.dump"><code class="xref py py-func docutils literal notranslate"><span class="pre">dump()</span></code></a>,
  613. simply serializes the object to a <a class="reference internal" href="../glossary.html#term-text-file"><span class="xref std std-term">text file</span></a>. So if <code class="docutils literal notranslate"><span class="pre">f</span></code> is a
  614. <a class="reference internal" href="../glossary.html#term-text-file"><span class="xref std std-term">text file</span></a> object opened for writing, we can do this:</p>
  615. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">json</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
  616. </pre></div>
  617. </div>
  618. <p>To decode the object again, if <code class="docutils literal notranslate"><span class="pre">f</span></code> is a <a class="reference internal" href="../glossary.html#term-binary-file"><span class="xref std std-term">binary file</span></a> or
  619. <a class="reference internal" href="../glossary.html#term-text-file"><span class="xref std std-term">text file</span></a> object which has been opened for reading:</p>
  620. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  621. </pre></div>
  622. </div>
  623. <div class="admonition note">
  624. <p class="admonition-title">Note</p>
  625. <p>JSON files must be encoded in UTF-8. Use <code class="docutils literal notranslate"><span class="pre">encoding=&quot;utf-8&quot;</span></code> when opening
  626. JSON file as a <a class="reference internal" href="../glossary.html#term-text-file"><span class="xref std std-term">text file</span></a> for both of reading and writing.</p>
  627. </div>
  628. <p>This simple serialization technique can handle lists and dictionaries, but
  629. serializing arbitrary class instances in JSON requires a bit of extra effort.
  630. The reference for the <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> module contains an explanation of this.</p>
  631. <div class="admonition seealso">
  632. <p class="admonition-title">See also</p>
  633. <p><a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> - the pickle module</p>
  634. <p>Contrary to <a class="reference internal" href="#tut-json"><span class="std std-ref">JSON</span></a>, <em>pickle</em> is a protocol which allows
  635. the serialization of arbitrarily complex Python objects. As such, it is
  636. specific to Python and cannot be used to communicate with applications
  637. written in other languages. It is also insecure by default:
  638. deserializing pickle data coming from an untrusted source can execute
  639. arbitrary code, if the data was crafted by a skilled attacker.</p>
  640. </div>
  641. </section>
  642. </section>
  643. </section>
  644. <div class="clearer"></div>
  645. </div>
  646. </div>
  647. </div>
  648. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  649. <div class="sphinxsidebarwrapper">
  650. <div>
  651. <h3><a href="../contents.html">Table of Contents</a></h3>
  652. <ul>
  653. <li><a class="reference internal" href="#">7. Input and Output</a><ul>
  654. <li><a class="reference internal" href="#fancier-output-formatting">7.1. Fancier Output Formatting</a><ul>
  655. <li><a class="reference internal" href="#formatted-string-literals">7.1.1. Formatted String Literals</a></li>
  656. <li><a class="reference internal" href="#the-string-format-method">7.1.2. The String format() Method</a></li>
  657. <li><a class="reference internal" href="#manual-string-formatting">7.1.3. Manual String Formatting</a></li>
  658. <li><a class="reference internal" href="#old-string-formatting">7.1.4. Old string formatting</a></li>
  659. </ul>
  660. </li>
  661. <li><a class="reference internal" href="#reading-and-writing-files">7.2. Reading and Writing Files</a><ul>
  662. <li><a class="reference internal" href="#methods-of-file-objects">7.2.1. Methods of File Objects</a></li>
  663. <li><a class="reference internal" href="#saving-structured-data-with-json">7.2.2. Saving structured data with <code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a></li>
  664. </ul>
  665. </li>
  666. </ul>
  667. </li>
  668. </ul>
  669. </div>
  670. <div>
  671. <h4>Previous topic</h4>
  672. <p class="topless"><a href="modules.html"
  673. title="previous chapter"><span class="section-number">6. </span>Modules</a></p>
  674. </div>
  675. <div>
  676. <h4>Next topic</h4>
  677. <p class="topless"><a href="errors.html"
  678. title="next chapter"><span class="section-number">8. </span>Errors and Exceptions</a></p>
  679. </div>
  680. <div role="note" aria-label="source link">
  681. <h3>This Page</h3>
  682. <ul class="this-page-menu">
  683. <li><a href="../bugs.html">Report a Bug</a></li>
  684. <li>
  685. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/inputoutput.rst"
  686. rel="nofollow">Show Source
  687. </a>
  688. </li>
  689. </ul>
  690. </div>
  691. </div>
  692. </div>
  693. <div class="clearer"></div>
  694. </div>
  695. <div class="related" role="navigation" aria-label="related navigation">
  696. <h3>Navigation</h3>
  697. <ul>
  698. <li class="right" style="margin-right: 10px">
  699. <a href="../genindex.html" title="General Index"
  700. >index</a></li>
  701. <li class="right" >
  702. <a href="../py-modindex.html" title="Python Module Index"
  703. >modules</a> |</li>
  704. <li class="right" >
  705. <a href="errors.html" title="8. Errors and Exceptions"
  706. >next</a> |</li>
  707. <li class="right" >
  708. <a href="modules.html" title="6. Modules"
  709. >previous</a> |</li>
  710. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  711. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  712. <li class="switchers">
  713. <div class="language_switcher_placeholder"></div>
  714. <div class="version_switcher_placeholder"></div>
  715. </li>
  716. <li>
  717. </li>
  718. <li id="cpython-language-and-version">
  719. <a href="../index.html">3.12.0 Documentation</a> &#187;
  720. </li>
  721. <li class="nav-item nav-item-1"><a href="index.html" >The Python Tutorial</a> &#187;</li>
  722. <li class="nav-item nav-item-this"><a href=""><span class="section-number">7. </span>Input and Output</a></li>
  723. <li class="right">
  724. <div class="inline-search" role="search">
  725. <form class="inline-search" action="../search.html" method="get">
  726. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  727. <input type="submit" value="Go" />
  728. </form>
  729. </div>
  730. |
  731. </li>
  732. <li class="right">
  733. <label class="theme-selector-label">
  734. Theme
  735. <select class="theme-selector" oninput="activateTheme(this.value)">
  736. <option value="auto" selected>Auto</option>
  737. <option value="light">Light</option>
  738. <option value="dark">Dark</option>
  739. </select>
  740. </label> |</li>
  741. </ul>
  742. </div>
  743. <div class="footer">
  744. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  745. <br />
  746. This page is licensed under the Python Software Foundation License Version 2.
  747. <br />
  748. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  749. <br />
  750. See <a href="/license.html">History and License</a> for more information.<br />
  751. <br />
  752. The Python Software Foundation is a non-profit corporation.
  753. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  754. <br />
  755. <br />
  756. Last updated on Oct 02, 2023.
  757. <a href="/bugs.html">Found a bug</a>?
  758. <br />
  759. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  760. </div>
  761. </body>
  762. </html>