Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
persistence.html441 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="Data Persistence" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/persistence.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="The modules described in this chapter support storing Python data in a persistent form on disk. The pickle and marshal modules can turn many Python data types into a stream of bytes and then recrea..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_persistence_bf769285.png" />15<meta property="og:image:alt" content="The modules described in this chapter support storing Python data in a persistent form on disk. The pickle and marshal modules can turn many Python data types into a stream of bytes and then recrea..." />16<meta name="description" content="The modules described in this chapter support storing Python data in a persistent form on disk. The pickle and marshal modules can turn many Python data types into a stream of bytes and then recrea..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>Data Persistence &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="pickle — Python object serialization" href="pickle.html" />43    <link rel="prev" title="shutil — High-level file operations" href="shutil.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/persistence.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <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>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h4>Previous topic</h4>106    <p class="topless"><a href="shutil.html"107                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> — High-level file operations</a></p>108  </div>109  <div>110    <h4>Next topic</h4>111    <p class="topless"><a href="pickle.html"112                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> — Python object serialization</a></p>113  </div>114  <script>115    document.addEventListener('DOMContentLoaded', () => {116        const title = document.querySelector('meta[property="og:title"]').content;117        const elements = document.querySelectorAll('.improvepage');118        const pageurl = window.location.href.split('?')[0];119        elements.forEach(element => {120            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));121            url.searchParams.set('pagetitle', title);122            url.searchParams.set('pageurl', pageurl);123            url.searchParams.set('pagesource', "library/persistence.rst");124            element.href = url.toString();125        });126    });127  </script>128  <div role="note" aria-label="source link">129    <h3>This page</h3>130    <ul class="this-page-menu">131      <li><a href="../bugs.html">Report a bug</a></li>132      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>133      <li>134        <a href="https://github.com/python/cpython/blob/main/Doc/library/persistence.rst?plain=1"135            rel="nofollow">Show source136        </a>137      </li>138      139    </ul>140  </div>141        </nav>142    </div>143</div>144 145  146    <div class="related" role="navigation" aria-label="Related">147      <h3>Navigation</h3>148      <ul>149        <li class="right" style="margin-right: 10px">150          <a href="../genindex.html" title="General Index"151             accesskey="I">index</a></li>152        <li class="right" >153          <a href="../py-modindex.html" title="Python Module Index"154             >modules</a> |</li>155        <li class="right" >156          <a href="pickle.html" title="pickle — Python object serialization"157             accesskey="N">next</a> |</li>158        <li class="right" >159          <a href="shutil.html" title="shutil — High-level file operations"160             accesskey="P">previous</a> |</li>161 162          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>163          <li><a href="https://www.python.org/">Python</a> &#187;</li>164          <li class="switchers">165            <div class="language_switcher_placeholder"></div>166            <div class="version_switcher_placeholder"></div>167          </li>168          <li>169              170          </li>171    <li id="cpython-language-and-version">172      <a href="../index.html">3.15.0a6 Documentation</a> &#187;173    </li>174 175          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Standard Library</a> &#187;</li>176        <li class="nav-item nav-item-this"><a href="">Data Persistence</a></li>177                <li class="right">178                    179 180    <div class="inline-search" role="search">181        <form class="inline-search" action="../search.html" method="get">182          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">183          <input type="submit" value="Go">184        </form>185    </div>186                     |187                </li>188            <li class="right">189<label class="theme-selector-label">190    Theme191    <select class="theme-selector" oninput="activateTheme(this.value)">192        <option value="auto" selected>Auto</option>193        <option value="light">Light</option>194        <option value="dark">Dark</option>195    </select>196</label> |</li>197            198      </ul>199    </div>    200 201    <div class="document">202      <div class="documentwrapper">203        <div class="bodywrapper">204          <div class="body" role="main">205            206  <section id="data-persistence">207<span id="persistence"></span><h1>Data Persistence<a class="headerlink" href="#data-persistence" title="Link to this heading">¶</a></h1>208<p>The modules described in this chapter support storing Python data in a209persistent form on disk.  The <a class="reference internal" href="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> and <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code></a> modules can turn210many Python data types into a stream of bytes and then recreate the objects from211the bytes.  The various DBM-related modules support a family of hash-based file212formats that store a mapping of strings to other strings.</p>213<p>The list of modules described in this chapter is:</p>214<div class="toctree-wrapper compound">215<ul>216<li class="toctree-l1"><a class="reference internal" href="pickle.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> — Python object serialization</a><ul>217<li class="toctree-l2"><a class="reference internal" href="pickle.html#relationship-to-other-python-modules">Relationship to other Python modules</a><ul>218<li class="toctree-l3"><a class="reference internal" href="pickle.html#comparison-with-marshal">Comparison with <code class="docutils literal notranslate"><span class="pre">marshal</span></code></a></li>219<li class="toctree-l3"><a class="reference internal" href="pickle.html#comparison-with-json">Comparison with <code class="docutils literal notranslate"><span class="pre">json</span></code></a></li>220</ul>221</li>222<li class="toctree-l2"><a class="reference internal" href="pickle.html#data-stream-format">Data stream format</a></li>223<li class="toctree-l2"><a class="reference internal" href="pickle.html#module-interface">Module Interface</a></li>224<li class="toctree-l2"><a class="reference internal" href="pickle.html#what-can-be-pickled-and-unpickled">What can be pickled and unpickled?</a></li>225<li class="toctree-l2"><a class="reference internal" href="pickle.html#pickling-class-instances">Pickling Class Instances</a><ul>226<li class="toctree-l3"><a class="reference internal" href="pickle.html#persistence-of-external-objects">Persistence of External Objects</a></li>227<li class="toctree-l3"><a class="reference internal" href="pickle.html#dispatch-tables">Dispatch Tables</a></li>228<li class="toctree-l3"><a class="reference internal" href="pickle.html#handling-stateful-objects">Handling Stateful Objects</a></li>229</ul>230</li>231<li class="toctree-l2"><a class="reference internal" href="pickle.html#custom-reduction-for-types-functions-and-other-objects">Custom Reduction for Types, Functions, and Other Objects</a></li>232<li class="toctree-l2"><a class="reference internal" href="pickle.html#out-of-band-buffers">Out-of-band Buffers</a><ul>233<li class="toctree-l3"><a class="reference internal" href="pickle.html#provider-api">Provider API</a></li>234<li class="toctree-l3"><a class="reference internal" href="pickle.html#consumer-api">Consumer API</a></li>235<li class="toctree-l3"><a class="reference internal" href="pickle.html#example">Example</a></li>236</ul>237</li>238<li class="toctree-l2"><a class="reference internal" href="pickle.html#restricting-globals">Restricting Globals</a></li>239<li class="toctree-l2"><a class="reference internal" href="pickle.html#performance">Performance</a></li>240<li class="toctree-l2"><a class="reference internal" href="pickle.html#examples">Examples</a></li>241<li class="toctree-l2"><a class="reference internal" href="pickle.html#command-line-interface">Command-line interface</a></li>242</ul>243</li>244<li class="toctree-l1"><a class="reference internal" href="copyreg.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copyreg</span></code> — Register <code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> support functions</a><ul>245<li class="toctree-l2"><a class="reference internal" href="copyreg.html#example">Example</a></li>246</ul>247</li>248<li class="toctree-l1"><a class="reference internal" href="shelve.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code> — Python object persistence</a><ul>249<li class="toctree-l2"><a class="reference internal" href="shelve.html#restrictions">Restrictions</a></li>250<li class="toctree-l2"><a class="reference internal" href="shelve.html#example">Example</a></li>251<li class="toctree-l2"><a class="reference internal" href="shelve.html#exceptions">Exceptions</a></li>252</ul>253</li>254<li class="toctree-l1"><a class="reference internal" href="marshal.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code> — Internal Python object serialization</a></li>255<li class="toctree-l1"><a class="reference internal" href="dbm.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code> — Interfaces to Unix “databases”</a><ul>256<li class="toctree-l2"><a class="reference internal" href="dbm.html#module-dbm.sqlite3"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.sqlite3</span></code> — SQLite backend for dbm</a></li>257<li class="toctree-l2"><a class="reference internal" href="dbm.html#module-dbm.gnu"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code> — GNU database manager</a></li>258<li class="toctree-l2"><a class="reference internal" href="dbm.html#module-dbm.ndbm"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code> — New Database Manager</a></li>259<li class="toctree-l2"><a class="reference internal" href="dbm.html#module-dbm.dumb"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code> — Portable DBM implementation</a></li>260</ul>261</li>262<li class="toctree-l1"><a class="reference internal" href="sqlite3.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> — DB-API 2.0 interface for SQLite databases</a><ul>263<li class="toctree-l2"><a class="reference internal" href="sqlite3.html#tutorial">Tutorial</a></li>264<li class="toctree-l2"><a class="reference internal" href="sqlite3.html#reference">Reference</a><ul>265<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#module-functions">Module functions</a></li>266<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#module-constants">Module constants</a></li>267<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#connection-objects">Connection objects</a></li>268<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#cursor-objects">Cursor objects</a></li>269<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#row-objects">Row objects</a></li>270<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#blob-objects">Blob objects</a></li>271<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#prepareprotocol-objects">PrepareProtocol objects</a></li>272<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#exceptions">Exceptions</a></li>273<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#sqlite-and-python-types">SQLite and Python types</a></li>274<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#default-adapters-and-converters-deprecated">Default adapters and converters (deprecated)</a></li>275<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#command-line-interface">Command-line interface</a></li>276</ul>277</li>278<li class="toctree-l2"><a class="reference internal" href="sqlite3.html#how-to-guides">How-to guides</a><ul>279<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#how-to-use-placeholders-to-bind-values-in-sql-queries">How to use placeholders to bind values in SQL queries</a></li>280<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#how-to-adapt-custom-python-types-to-sqlite-values">How to adapt custom Python types to SQLite values</a><ul>281<li class="toctree-l4"><a class="reference internal" href="sqlite3.html#how-to-write-adaptable-objects">How to write adaptable objects</a></li>282<li class="toctree-l4"><a class="reference internal" href="sqlite3.html#how-to-register-adapter-callables">How to register adapter callables</a></li>283</ul>284</li>285<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#how-to-convert-sqlite-values-to-custom-python-types">How to convert SQLite values to custom Python types</a></li>286<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#adapter-and-converter-recipes">Adapter and converter recipes</a></li>287<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#how-to-use-connection-shortcut-methods">How to use connection shortcut methods</a></li>288<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#how-to-use-the-connection-context-manager">How to use the connection context manager</a></li>289<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#how-to-work-with-sqlite-uris">How to work with SQLite URIs</a></li>290<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#how-to-create-and-use-row-factories">How to create and use row factories</a></li>291<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#how-to-handle-non-utf-8-text-encodings">How to handle non-UTF-8 text encodings</a></li>292</ul>293</li>294<li class="toctree-l2"><a class="reference internal" href="sqlite3.html#explanation">Explanation</a><ul>295<li class="toctree-l3"><a class="reference internal" href="sqlite3.html#transaction-control">Transaction control</a><ul>296<li class="toctree-l4"><a class="reference internal" href="sqlite3.html#transaction-control-via-the-autocommit-attribute">Transaction control via the <code class="docutils literal notranslate"><span class="pre">autocommit</span></code> attribute</a></li>297<li class="toctree-l4"><a class="reference internal" href="sqlite3.html#transaction-control-via-the-isolation-level-attribute">Transaction control via the <code class="docutils literal notranslate"><span class="pre">isolation_level</span></code> attribute</a></li>298</ul>299</li>300</ul>301</li>302</ul>303</li>304</ul>305</div>306</section>307 308 309            <div class="clearer"></div>310          </div>311        </div>312      </div>313      <div class="sphinxsidebar" role="navigation" aria-label="Main">314        <div class="sphinxsidebarwrapper">315  <div>316    <h4>Previous topic</h4>317    <p class="topless"><a href="shutil.html"318                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> — High-level file operations</a></p>319  </div>320  <div>321    <h4>Next topic</h4>322    <p class="topless"><a href="pickle.html"323                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> — Python object serialization</a></p>324  </div>325  <script>326    document.addEventListener('DOMContentLoaded', () => {327        const title = document.querySelector('meta[property="og:title"]').content;328        const elements = document.querySelectorAll('.improvepage');329        const pageurl = window.location.href.split('?')[0];330        elements.forEach(element => {331            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));332            url.searchParams.set('pagetitle', title);333            url.searchParams.set('pageurl', pageurl);334            url.searchParams.set('pagesource', "library/persistence.rst");335            element.href = url.toString();336        });337    });338  </script>339  <div role="note" aria-label="source link">340    <h3>This page</h3>341    <ul class="this-page-menu">342      <li><a href="../bugs.html">Report a bug</a></li>343      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>344      <li>345        <a href="https://github.com/python/cpython/blob/main/Doc/library/persistence.rst?plain=1"346            rel="nofollow">Show source347        </a>348      </li>349      350    </ul>351  </div>352        </div>353<div id="sidebarbutton" title="Collapse sidebar">354<span>«</span>355</div>356 357      </div>358      <div class="clearer"></div>359    </div>  360    <div class="related" role="navigation" aria-label="Related">361      <h3>Navigation</h3>362      <ul>363        <li class="right" style="margin-right: 10px">364          <a href="../genindex.html" title="General Index"365             >index</a></li>366        <li class="right" >367          <a href="../py-modindex.html" title="Python Module Index"368             >modules</a> |</li>369        <li class="right" >370          <a href="pickle.html" title="pickle — Python object serialization"371             >next</a> |</li>372        <li class="right" >373          <a href="shutil.html" title="shutil — High-level file operations"374             >previous</a> |</li>375 376          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>377          <li><a href="https://www.python.org/">Python</a> &#187;</li>378          <li class="switchers">379            <div class="language_switcher_placeholder"></div>380            <div class="version_switcher_placeholder"></div>381          </li>382          <li>383              384          </li>385    <li id="cpython-language-and-version">386      <a href="../index.html">3.15.0a6 Documentation</a> &#187;387    </li>388 389          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>390        <li class="nav-item nav-item-this"><a href="">Data Persistence</a></li>391                <li class="right">392                    393 394    <div class="inline-search" role="search">395        <form class="inline-search" action="../search.html" method="get">396          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">397          <input type="submit" value="Go">398        </form>399    </div>400                     |401                </li>402            <li class="right">403<label class="theme-selector-label">404    Theme405    <select class="theme-selector" oninput="activateTheme(this.value)">406        <option value="auto" selected>Auto</option>407        <option value="light">Light</option>408        <option value="dark">Dark</option>409    </select>410</label> |</li>411            412      </ul>413    </div>  414    <div class="footer">415    &copy; <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.416    <br>417    This page is licensed under the Python Software Foundation License Version 2.418    <br>419    Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.420    <br>421    422      See <a href="/license.html">History and License</a> for more information.<br>423    424    425    <br>426 427    The Python Software Foundation is a non-profit corporation.428<a href="https://www.python.org/psf/donations/">Please donate.</a>429<br>430    <br>431      Last updated on Mar 10, 2026 (08:58 UTC).432    433      <a href="/bugs.html">Found a bug</a>?434    435    <br>436 437    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.438    </div>439 440  </body>441</html>