gooderp18绿色标准版
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

721 lines
57KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="dbm — Interfaces to Unix “databases”" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/dbm.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/dbm/__init__.py dbm is a generic interface to variants of the DBM database — dbm.gnu or dbm.ndbm. If none of these modules is installed, the slow-but-simple implementation in modul..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/dbm/__init__.py dbm is a generic interface to variants of the DBM database — dbm.gnu or dbm.ndbm. If none of these modules is installed, the slow-but-simple implementation in modul..." />
  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>dbm — Interfaces to Unix “databases” &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="sqlite3 — DB-API 2.0 interface for SQLite databases" href="sqlite3.html" />
  33. <link rel="prev" title="marshal — Internal Python object serialization" href="marshal.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/dbm.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code> — Interfaces to Unix “databases”</a><ul>
  86. <li><a class="reference internal" href="#module-dbm.gnu"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code> — GNU database manager</a></li>
  87. <li><a class="reference internal" href="#module-dbm.ndbm"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code> — New Database Manager</a></li>
  88. <li><a class="reference internal" href="#module-dbm.dumb"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code> — Portable DBM implementation</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </div>
  93. <div>
  94. <h4>Previous topic</h4>
  95. <p class="topless"><a href="marshal.html"
  96. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code> — Internal Python object serialization</a></p>
  97. </div>
  98. <div>
  99. <h4>Next topic</h4>
  100. <p class="topless"><a href="sqlite3.html"
  101. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> — DB-API 2.0 interface for SQLite databases</a></p>
  102. </div>
  103. <div role="note" aria-label="source link">
  104. <h3>This Page</h3>
  105. <ul class="this-page-menu">
  106. <li><a href="../bugs.html">Report a Bug</a></li>
  107. <li>
  108. <a href="https://github.com/python/cpython/blob/main/Doc/library/dbm.rst"
  109. rel="nofollow">Show Source
  110. </a>
  111. </li>
  112. </ul>
  113. </div>
  114. </nav>
  115. </div>
  116. </div>
  117. <div class="related" role="navigation" aria-label="related navigation">
  118. <h3>Navigation</h3>
  119. <ul>
  120. <li class="right" style="margin-right: 10px">
  121. <a href="../genindex.html" title="General Index"
  122. accesskey="I">index</a></li>
  123. <li class="right" >
  124. <a href="../py-modindex.html" title="Python Module Index"
  125. >modules</a> |</li>
  126. <li class="right" >
  127. <a href="sqlite3.html" title="sqlite3 — DB-API 2.0 interface for SQLite databases"
  128. accesskey="N">next</a> |</li>
  129. <li class="right" >
  130. <a href="marshal.html" title="marshal — Internal Python object serialization"
  131. accesskey="P">previous</a> |</li>
  132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  134. <li class="switchers">
  135. <div class="language_switcher_placeholder"></div>
  136. <div class="version_switcher_placeholder"></div>
  137. </li>
  138. <li>
  139. </li>
  140. <li id="cpython-language-and-version">
  141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  142. </li>
  143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  144. <li class="nav-item nav-item-2"><a href="persistence.html" accesskey="U">Data Persistence</a> &#187;</li>
  145. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code> — Interfaces to Unix “databases”</a></li>
  146. <li class="right">
  147. <div class="inline-search" role="search">
  148. <form class="inline-search" action="../search.html" method="get">
  149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  150. <input type="submit" value="Go" />
  151. </form>
  152. </div>
  153. |
  154. </li>
  155. <li class="right">
  156. <label class="theme-selector-label">
  157. Theme
  158. <select class="theme-selector" oninput="activateTheme(this.value)">
  159. <option value="auto" selected>Auto</option>
  160. <option value="light">Light</option>
  161. <option value="dark">Dark</option>
  162. </select>
  163. </label> |</li>
  164. </ul>
  165. </div>
  166. <div class="document">
  167. <div class="documentwrapper">
  168. <div class="bodywrapper">
  169. <div class="body" role="main">
  170. <section id="module-dbm">
  171. <span id="dbm-interfaces-to-unix-databases"></span><h1><a class="reference internal" href="#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a> — Interfaces to Unix “databases”<a class="headerlink" href="#module-dbm" title="Link to this heading">¶</a></h1>
  172. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/dbm/__init__.py">Lib/dbm/__init__.py</a></p>
  173. <hr class="docutils" />
  174. <p><a class="reference internal" href="#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a> is a generic interface to variants of the DBM database —
  175. <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU database manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a> or <a class="reference internal" href="#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a>. If none of these modules is installed, the
  176. slow-but-simple implementation in module <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code></a> will be used. There
  177. is a <a class="reference external" href="https://www.jcea.es/programacion/pybsddb.htm">third party interface</a> to
  178. the Oracle Berkeley DB.</p>
  179. <dl class="py exception">
  180. <dt class="sig sig-object py" id="dbm.error">
  181. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">dbm.</span></span><span class="sig-name descname"><span class="pre">error</span></span><a class="headerlink" href="#dbm.error" title="Link to this definition">¶</a></dt>
  182. <dd><p>A tuple containing the exceptions that can be raised by each of the supported
  183. modules, with a unique exception also named <a class="reference internal" href="#dbm.error" title="dbm.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">dbm.error</span></code></a> as the first
  184. item — the latter is used when <a class="reference internal" href="#dbm.error" title="dbm.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">dbm.error</span></code></a> is raised.</p>
  185. </dd></dl>
  186. <dl class="py function">
  187. <dt class="sig sig-object py" id="dbm.whichdb">
  188. <span class="sig-prename descclassname"><span class="pre">dbm.</span></span><span class="sig-name descname"><span class="pre">whichdb</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dbm.whichdb" title="Link to this definition">¶</a></dt>
  189. <dd><p>This function attempts to guess which of the several simple database modules
  190. available — <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU database manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a>, <a class="reference internal" href="#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a> or <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code></a> — should
  191. be used to open a given file.</p>
  192. <p>Return one of the following values:</p>
  193. <ul class="simple">
  194. <li><p><code class="docutils literal notranslate"><span class="pre">None</span></code> if the file can’t be opened because it’s unreadable or doesn’t exist</p></li>
  195. <li><p>the empty string (<code class="docutils literal notranslate"><span class="pre">''</span></code>) if the file’s format can’t be guessed</p></li>
  196. <li><p>a string containing the required module name, such as <code class="docutils literal notranslate"><span class="pre">'dbm.ndbm'</span></code> or <code class="docutils literal notranslate"><span class="pre">'dbm.gnu'</span></code></p></li>
  197. </ul>
  198. <div class="versionchanged">
  199. <p><span class="versionmodified changed">Changed in version 3.11: </span><em>filename</em> accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  200. </div>
  201. </dd></dl>
  202. <dl class="py function">
  203. <dt class="sig sig-object py" id="dbm.open">
  204. <span class="sig-prename descclassname"><span class="pre">dbm.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0o666</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dbm.open" title="Link to this definition">¶</a></dt>
  205. <dd><p>Open a database and return the corresponding database object.</p>
  206. <dl class="field-list simple">
  207. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  208. <dd class="field-odd"><ul class="simple">
  209. <li><p><strong>file</strong> (<a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>) – <p>The database file to open.</p>
  210. <p>If the database file already exists, the <a class="reference internal" href="#dbm.whichdb" title="dbm.whichdb"><code class="xref py py-func docutils literal notranslate"><span class="pre">whichdb()</span></code></a> function is used to
  211. determine its type and the appropriate module is used; if it does not exist,
  212. the first submodule listed above that can be imported is used.</p>
  213. </p></li>
  214. <li><p><strong>flag</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – <ul>
  215. <li><p><code class="docutils literal notranslate"><span class="pre">'r'</span></code> (default): Open existing database for reading only.</p></li>
  216. <li><p><code class="docutils literal notranslate"><span class="pre">'w'</span></code>: Open existing database for reading and writing.</p></li>
  217. <li><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code>: Open database for reading and writing, creating it if it doesn’t exist.</p></li>
  218. <li><p><code class="docutils literal notranslate"><span class="pre">'n'</span></code>: Always create a new, empty database, open for reading and writing.</p></li>
  219. </ul>
  220. </p></li>
  221. <li><p><strong>mode</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The Unix file access mode of the file (default: octal <code class="docutils literal notranslate"><span class="pre">0o666</span></code>),
  222. used only when the database has to be created.</p></li>
  223. </ul>
  224. </dd>
  225. </dl>
  226. <div class="versionchanged">
  227. <p><span class="versionmodified changed">Changed in version 3.11: </span><em>file</em> accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  228. </div>
  229. </dd></dl>
  230. <p>The object returned by <a class="reference internal" href="#dbm.open" title="dbm.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> supports the same basic functionality as a
  231. <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>; keys and their corresponding values can be stored, retrieved, and
  232. deleted, and the <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> operator and the <code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code> method are
  233. available, as well as <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">setdefault()</span></code> methods.</p>
  234. <p>Key and values are always stored as <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>. This means that when
  235. strings are used they are implicitly converted to the default encoding before
  236. being stored.</p>
  237. <p>These objects also support being used in 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, which
  238. will automatically close them when done.</p>
  239. <div class="versionchanged">
  240. <p><span class="versionmodified changed">Changed in version 3.2: </span><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">setdefault()</span></code> methods are now available for all
  241. <a class="reference internal" href="#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a> backends.</p>
  242. </div>
  243. <div class="versionchanged">
  244. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added native support for the context management protocol to the objects
  245. returned by <a class="reference internal" href="#dbm.open" title="dbm.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>.</p>
  246. </div>
  247. <div class="versionchanged">
  248. <p><span class="versionmodified changed">Changed in version 3.8: </span>Deleting a key from a read-only database raises a database module specific exception
  249. instead of <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>.</p>
  250. </div>
  251. <p>The following example records some hostnames and a corresponding title, and
  252. then prints out the contents of the database:</p>
  253. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">dbm</span>
  254. <span class="c1"># Open database, creating it if necessary.</span>
  255. <span class="k">with</span> <span class="n">dbm</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;cache&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">db</span><span class="p">:</span>
  256. <span class="c1"># Record some values</span>
  257. <span class="n">db</span><span class="p">[</span><span class="sa">b</span><span class="s1">&#39;hello&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;there&#39;</span>
  258. <span class="n">db</span><span class="p">[</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Python Website&#39;</span>
  259. <span class="n">db</span><span class="p">[</span><span class="s1">&#39;www.cnn.com&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Cable News Network&#39;</span>
  260. <span class="c1"># Note that the keys are considered bytes now.</span>
  261. <span class="k">assert</span> <span class="n">db</span><span class="p">[</span><span class="sa">b</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="sa">b</span><span class="s1">&#39;Python Website&#39;</span>
  262. <span class="c1"># Notice how the value is now in bytes.</span>
  263. <span class="k">assert</span> <span class="n">db</span><span class="p">[</span><span class="s1">&#39;www.cnn.com&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="sa">b</span><span class="s1">&#39;Cable News Network&#39;</span>
  264. <span class="c1"># Often-used methods of the dict interface work too.</span>
  265. <span class="nb">print</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;python.org&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;not present&#39;</span><span class="p">))</span>
  266. <span class="c1"># Storing a non-string key or value will raise an exception (most</span>
  267. <span class="c1"># likely a TypeError).</span>
  268. <span class="n">db</span><span class="p">[</span><span class="s1">&#39;www.yahoo.com&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span>
  269. <span class="c1"># db is automatically closed when leaving the with statement.</span>
  270. </pre></div>
  271. </div>
  272. <div class="admonition seealso">
  273. <p class="admonition-title">See also</p>
  274. <dl class="simple">
  275. <dt>Module <a class="reference internal" href="shelve.html#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a></dt><dd><p>Persistence module which stores non-string data.</p>
  276. </dd>
  277. </dl>
  278. </div>
  279. <p>The individual submodules are described in the following sections.</p>
  280. <section id="module-dbm.gnu">
  281. <span id="dbm-gnu-gnu-database-manager"></span><h2><a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU database manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a> — GNU database manager<a class="headerlink" href="#module-dbm.gnu" title="Link to this heading">¶</a></h2>
  282. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/dbm/gnu.py">Lib/dbm/gnu.py</a></p>
  283. <hr class="docutils" />
  284. <p>The <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU database manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a> module provides an interface to the <abbr title="GNU dbm">GDBM</abbr>
  285. library, similar to the <a class="reference internal" href="#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a> module, but with additional
  286. functionality like crash tolerance.</p>
  287. <div class="admonition note">
  288. <p class="admonition-title">Note</p>
  289. <p>The file formats created by <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU database manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a> and <a class="reference internal" href="#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a> are incompatible
  290. and can not be used interchangeably.</p>
  291. </div>
  292. <dl class="py exception">
  293. <dt class="sig sig-object py" id="dbm.gnu.error">
  294. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">dbm.gnu.</span></span><span class="sig-name descname"><span class="pre">error</span></span><a class="headerlink" href="#dbm.gnu.error" title="Link to this definition">¶</a></dt>
  295. <dd><p>Raised on <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU database manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a>-specific errors, such as I/O errors. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> is
  296. raised for general mapping errors like specifying an incorrect key.</p>
  297. </dd></dl>
  298. <dl class="py function">
  299. <dt class="sig sig-object py" id="dbm.gnu.open">
  300. <span class="sig-prename descclassname"><span class="pre">dbm.gnu.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0o666</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dbm.gnu.open" title="Link to this definition">¶</a></dt>
  301. <dd><p>Open a GDBM database and return a <code class="xref py py-class docutils literal notranslate"><span class="pre">gdbm</span></code> object.</p>
  302. <dl class="field-list simple">
  303. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  304. <dd class="field-odd"><ul class="simple">
  305. <li><p><strong>filename</strong> (<a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>) – The database file to open.</p></li>
  306. <li><p><strong>flag</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – <ul>
  307. <li><p><code class="docutils literal notranslate"><span class="pre">'r'</span></code> (default): Open existing database for reading only.</p></li>
  308. <li><p><code class="docutils literal notranslate"><span class="pre">'w'</span></code>: Open existing database for reading and writing.</p></li>
  309. <li><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code>: Open database for reading and writing, creating it if it doesn’t exist.</p></li>
  310. <li><p><code class="docutils literal notranslate"><span class="pre">'n'</span></code>: Always create a new, empty database, open for reading and writing.</p></li>
  311. </ul>
  312. <p>The following additional characters may be appended
  313. to control how the database is opened:</p>
  314. <ul>
  315. <li><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code>: Open the database in fast mode.
  316. Writes to the database will not be synchronized.</p></li>
  317. <li><p><code class="docutils literal notranslate"><span class="pre">'s'</span></code>: Synchronized mode.
  318. Changes to the database will be written immediately to the file.</p></li>
  319. <li><p><code class="docutils literal notranslate"><span class="pre">'u'</span></code>: Do not lock database.</p></li>
  320. </ul>
  321. <p>Not all flags are valid for all versions of GDBM.
  322. See the <a class="reference internal" href="#dbm.gnu.open_flags" title="dbm.gnu.open_flags"><code class="xref py py-data docutils literal notranslate"><span class="pre">open_flags</span></code></a> member for a list of supported flag characters.</p>
  323. </p></li>
  324. <li><p><strong>mode</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The Unix file access mode of the file (default: octal <code class="docutils literal notranslate"><span class="pre">0o666</span></code>),
  325. used only when the database has to be created.</p></li>
  326. </ul>
  327. </dd>
  328. <dt class="field-even">Raises<span class="colon">:</span></dt>
  329. <dd class="field-even"><p><a class="reference internal" href="#dbm.gnu.error" title="dbm.gnu.error"><strong>error</strong></a> – If an invalid <em>flag</em> argument is passed.</p>
  330. </dd>
  331. </dl>
  332. <div class="versionchanged">
  333. <p><span class="versionmodified changed">Changed in version 3.11: </span><em>filename</em> accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  334. </div>
  335. <dl class="py data">
  336. <dt class="sig sig-object py" id="dbm.gnu.open_flags">
  337. <span class="sig-prename descclassname"><span class="pre">dbm.gnu.</span></span><span class="sig-name descname"><span class="pre">open_flags</span></span><a class="headerlink" href="#dbm.gnu.open_flags" title="Link to this definition">¶</a></dt>
  338. <dd><p>A string of characters the <em>flag</em> parameter of <a class="reference internal" href="#dbm.gnu.open" title="dbm.gnu.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code></a> supports.</p>
  339. </dd></dl>
  340. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">gdbm</span></code> objects behave similar to <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mappings</span></a>,
  341. but <code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">values()</span></code> methods are not supported.
  342. The following methods are also provided:</p>
  343. <dl class="py method">
  344. <dt class="sig sig-object py" id="dbm.gnu.gdbm.firstkey">
  345. <span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">firstkey</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#dbm.gnu.gdbm.firstkey" title="Link to this definition">¶</a></dt>
  346. <dd><p>It’s possible to loop over every key in the database using this method and the
  347. <a class="reference internal" href="#dbm.gnu.gdbm.nextkey" title="dbm.gnu.gdbm.nextkey"><code class="xref py py-meth docutils literal notranslate"><span class="pre">nextkey()</span></code></a> method. The traversal is ordered by GDBM’s internal
  348. hash values, and won’t be sorted by the key values. This method returns
  349. the starting key.</p>
  350. </dd></dl>
  351. <dl class="py method">
  352. <dt class="sig sig-object py" id="dbm.gnu.gdbm.nextkey">
  353. <span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">nextkey</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dbm.gnu.gdbm.nextkey" title="Link to this definition">¶</a></dt>
  354. <dd><p>Returns the key that follows <em>key</em> in the traversal. The following code prints
  355. every key in the database <code class="docutils literal notranslate"><span class="pre">db</span></code>, without having to create a list in memory that
  356. contains them all:</p>
  357. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">k</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">firstkey</span><span class="p">()</span>
  358. <span class="k">while</span> <span class="n">k</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  359. <span class="nb">print</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
  360. <span class="n">k</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">nextkey</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
  361. </pre></div>
  362. </div>
  363. </dd></dl>
  364. <dl class="py method">
  365. <dt class="sig sig-object py" id="dbm.gnu.gdbm.reorganize">
  366. <span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">reorganize</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#dbm.gnu.gdbm.reorganize" title="Link to this definition">¶</a></dt>
  367. <dd><p>If you have carried out a lot of deletions and would like to shrink the space
  368. used by the GDBM file, this routine will reorganize the database. <code class="xref py py-class docutils literal notranslate"><span class="pre">gdbm</span></code>
  369. objects will not shorten the length of a database file except by using this
  370. reorganization; otherwise, deleted file space will be kept and reused as new
  371. (key, value) pairs are added.</p>
  372. </dd></dl>
  373. <dl class="py method">
  374. <dt class="sig sig-object py" id="dbm.gnu.gdbm.sync">
  375. <span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">sync</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#dbm.gnu.gdbm.sync" title="Link to this definition">¶</a></dt>
  376. <dd><p>When the database has been opened in fast mode, this method forces any
  377. unwritten data to be written to the disk.</p>
  378. </dd></dl>
  379. <dl class="py method">
  380. <dt class="sig sig-object py" id="dbm.gnu.gdbm.close">
  381. <span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#dbm.gnu.gdbm.close" title="Link to this definition">¶</a></dt>
  382. <dd><p>Close the GDBM database.</p>
  383. </dd></dl>
  384. </dd></dl>
  385. </section>
  386. <section id="module-dbm.ndbm">
  387. <span id="dbm-ndbm-new-database-manager"></span><h2><a class="reference internal" href="#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a> — New Database Manager<a class="headerlink" href="#module-dbm.ndbm" title="Link to this heading">¶</a></h2>
  388. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/dbm/ndbm.py">Lib/dbm/ndbm.py</a></p>
  389. <hr class="docutils" />
  390. <p>The <a class="reference internal" href="#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a> module provides an interface to the
  391. <abbr title="New Database Manager">NDBM</abbr> library.
  392. This module can be used with the “classic” NDBM interface or the
  393. <abbr title="GNU dbm">GDBM</abbr> compatibility interface.</p>
  394. <div class="admonition note">
  395. <p class="admonition-title">Note</p>
  396. <p>The file formats created by <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU database manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a> and <a class="reference internal" href="#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a> are incompatible
  397. and can not be used interchangeably.</p>
  398. </div>
  399. <div class="admonition warning">
  400. <p class="admonition-title">Warning</p>
  401. <p>The NDBM library shipped as part of macOS has an undocumented limitation on the
  402. size of values, which can result in corrupted database files
  403. when storing values larger than this limit. Reading such corrupted files can
  404. result in a hard crash (segmentation fault).</p>
  405. </div>
  406. <dl class="py exception">
  407. <dt class="sig sig-object py" id="dbm.ndbm.error">
  408. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">dbm.ndbm.</span></span><span class="sig-name descname"><span class="pre">error</span></span><a class="headerlink" href="#dbm.ndbm.error" title="Link to this definition">¶</a></dt>
  409. <dd><p>Raised on <a class="reference internal" href="#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a>-specific errors, such as I/O errors. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> is raised
  410. for general mapping errors like specifying an incorrect key.</p>
  411. </dd></dl>
  412. <dl class="py data">
  413. <dt class="sig sig-object py" id="dbm.ndbm.library">
  414. <span class="sig-prename descclassname"><span class="pre">dbm.ndbm.</span></span><span class="sig-name descname"><span class="pre">library</span></span><a class="headerlink" href="#dbm.ndbm.library" title="Link to this definition">¶</a></dt>
  415. <dd><p>Name of the NDBM implementation library used.</p>
  416. </dd></dl>
  417. <dl class="py function">
  418. <dt class="sig sig-object py" id="dbm.ndbm.open">
  419. <span class="sig-prename descclassname"><span class="pre">dbm.ndbm.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0o666</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dbm.ndbm.open" title="Link to this definition">¶</a></dt>
  420. <dd><p>Open an NDBM database and return an <code class="xref py py-class docutils literal notranslate"><span class="pre">ndbm</span></code> object.</p>
  421. <dl class="field-list simple">
  422. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  423. <dd class="field-odd"><ul class="simple">
  424. <li><p><strong>filename</strong> (<a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>) – The basename of the database file
  425. (without the <code class="file docutils literal notranslate"><span class="pre">.dir</span></code> or <code class="file docutils literal notranslate"><span class="pre">.pag</span></code> extensions).</p></li>
  426. <li><p><strong>flag</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – <ul>
  427. <li><p><code class="docutils literal notranslate"><span class="pre">'r'</span></code> (default): Open existing database for reading only.</p></li>
  428. <li><p><code class="docutils literal notranslate"><span class="pre">'w'</span></code>: Open existing database for reading and writing.</p></li>
  429. <li><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code>: Open database for reading and writing, creating it if it doesn’t exist.</p></li>
  430. <li><p><code class="docutils literal notranslate"><span class="pre">'n'</span></code>: Always create a new, empty database, open for reading and writing.</p></li>
  431. </ul>
  432. </p></li>
  433. <li><p><strong>mode</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The Unix file access mode of the file (default: octal <code class="docutils literal notranslate"><span class="pre">0o666</span></code>),
  434. used only when the database has to be created.</p></li>
  435. </ul>
  436. </dd>
  437. </dl>
  438. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">ndbm</span></code> objects behave similar to <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mappings</span></a>,
  439. but <code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">values()</span></code> methods are not supported.
  440. The following methods are also provided:</p>
  441. <div class="versionchanged">
  442. <p><span class="versionmodified changed">Changed in version 3.11: </span>Accepts <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for filename.</p>
  443. </div>
  444. <dl class="py method">
  445. <dt class="sig sig-object py" id="dbm.ndbm.ndbm.close">
  446. <span class="sig-prename descclassname"><span class="pre">ndbm.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#dbm.ndbm.ndbm.close" title="Link to this definition">¶</a></dt>
  447. <dd><p>Close the NDBM database.</p>
  448. </dd></dl>
  449. </dd></dl>
  450. </section>
  451. <section id="module-dbm.dumb">
  452. <span id="dbm-dumb-portable-dbm-implementation"></span><h2><a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code></a> — Portable DBM implementation<a class="headerlink" href="#module-dbm.dumb" title="Link to this heading">¶</a></h2>
  453. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/dbm/dumb.py">Lib/dbm/dumb.py</a></p>
  454. <div class="admonition note" id="index-0">
  455. <p class="admonition-title">Note</p>
  456. <p>The <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code></a> module is intended as a last resort fallback for the
  457. <a class="reference internal" href="#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a> module when a more robust module is not available. The <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code></a>
  458. module is not written for speed and is not nearly as heavily used as the other
  459. database modules.</p>
  460. </div>
  461. <hr class="docutils" />
  462. <p>The <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code></a> module provides a persistent <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>-like
  463. interface which is written entirely in Python.
  464. Unlike other <a class="reference internal" href="#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a> backends, such as <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU database manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a>, no
  465. external library is required.</p>
  466. <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code> module defines the following:</p>
  467. <dl class="py exception">
  468. <dt class="sig sig-object py" id="dbm.dumb.error">
  469. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">dbm.dumb.</span></span><span class="sig-name descname"><span class="pre">error</span></span><a class="headerlink" href="#dbm.dumb.error" title="Link to this definition">¶</a></dt>
  470. <dd><p>Raised on <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code></a>-specific errors, such as I/O errors. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> is
  471. raised for general mapping errors like specifying an incorrect key.</p>
  472. </dd></dl>
  473. <dl class="py function">
  474. <dt class="sig sig-object py" id="dbm.dumb.open">
  475. <span class="sig-prename descclassname"><span class="pre">dbm.dumb.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'c'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0o666</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dbm.dumb.open" title="Link to this definition">¶</a></dt>
  476. <dd><p>Open a <code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code> database.
  477. The returned database object behaves similar to a <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mapping</span></a>,
  478. in addition to providing <a class="reference internal" href="#dbm.dumb.dumbdbm.sync" title="dbm.dumb.dumbdbm.sync"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sync()</span></code></a> and <a class="reference internal" href="#dbm.dumb.dumbdbm.close" title="dbm.dumb.dumbdbm.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a>
  479. methods.</p>
  480. <dl class="field-list simple">
  481. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  482. <dd class="field-odd"><ul class="simple">
  483. <li><p><strong>filename</strong> – <p>The basename of the database file (without extensions).
  484. A new database creates the following files:</p>
  485. <ul>
  486. <li><p><code class="file docutils literal notranslate"><em><span class="pre">filename</span></em><span class="pre">.dat</span></code></p></li>
  487. <li><p><code class="file docutils literal notranslate"><em><span class="pre">filename</span></em><span class="pre">.dir</span></code></p></li>
  488. </ul>
  489. </p></li>
  490. <li><p><strong>flag</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – <ul>
  491. <li><p><code class="docutils literal notranslate"><span class="pre">'r'</span></code>: Open existing database for reading only.</p></li>
  492. <li><p><code class="docutils literal notranslate"><span class="pre">'w'</span></code>: Open existing database for reading and writing.</p></li>
  493. <li><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code> (default): Open database for reading and writing, creating it if it doesn’t exist.</p></li>
  494. <li><p><code class="docutils literal notranslate"><span class="pre">'n'</span></code>: Always create a new, empty database, open for reading and writing.</p></li>
  495. </ul>
  496. </p></li>
  497. <li><p><strong>mode</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The Unix file access mode of the file (default: octal <code class="docutils literal notranslate"><span class="pre">0o666</span></code>),
  498. used only when the database has to be created.</p></li>
  499. </ul>
  500. </dd>
  501. </dl>
  502. <div class="admonition warning">
  503. <p class="admonition-title">Warning</p>
  504. <p>It is possible to crash the Python interpreter when loading a database
  505. with a sufficiently large/complex entry due to stack depth limitations in
  506. Python’s AST compiler.</p>
  507. </div>
  508. <div class="versionchanged">
  509. <p><span class="versionmodified changed">Changed in version 3.5: </span><a class="reference internal" href="#dbm.dumb.open" title="dbm.dumb.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> always creates a new database when <em>flag</em> is <code class="docutils literal notranslate"><span class="pre">'n'</span></code>.</p>
  510. </div>
  511. <div class="versionchanged">
  512. <p><span class="versionmodified changed">Changed in version 3.8: </span>A database opened read-only if <em>flag</em> is <code class="docutils literal notranslate"><span class="pre">'r'</span></code>.
  513. A database is not created if it does not exist if <em>flag</em> is <code class="docutils literal notranslate"><span class="pre">'r'</span></code> or <code class="docutils literal notranslate"><span class="pre">'w'</span></code>.</p>
  514. </div>
  515. <div class="versionchanged">
  516. <p><span class="versionmodified changed">Changed in version 3.11: </span><em>filename</em> accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  517. </div>
  518. <p>In addition to the methods provided by the
  519. <a class="reference internal" href="collections.abc.html#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.MutableMapping</span></code></a> class,
  520. the following methods are provided:</p>
  521. <dl class="py method">
  522. <dt class="sig sig-object py" id="dbm.dumb.dumbdbm.sync">
  523. <span class="sig-prename descclassname"><span class="pre">dumbdbm.</span></span><span class="sig-name descname"><span class="pre">sync</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#dbm.dumb.dumbdbm.sync" title="Link to this definition">¶</a></dt>
  524. <dd><p>Synchronize the on-disk directory and data files. This method is called
  525. by the <code class="xref py py-meth docutils literal notranslate"><span class="pre">Shelve.sync()</span></code> method.</p>
  526. </dd></dl>
  527. <dl class="py method">
  528. <dt class="sig sig-object py" id="dbm.dumb.dumbdbm.close">
  529. <span class="sig-prename descclassname"><span class="pre">dumbdbm.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#dbm.dumb.dumbdbm.close" title="Link to this definition">¶</a></dt>
  530. <dd><p>Close the database.</p>
  531. </dd></dl>
  532. </dd></dl>
  533. </section>
  534. </section>
  535. <div class="clearer"></div>
  536. </div>
  537. </div>
  538. </div>
  539. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  540. <div class="sphinxsidebarwrapper">
  541. <div>
  542. <h3><a href="../contents.html">Table of Contents</a></h3>
  543. <ul>
  544. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code> — Interfaces to Unix “databases”</a><ul>
  545. <li><a class="reference internal" href="#module-dbm.gnu"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code> — GNU database manager</a></li>
  546. <li><a class="reference internal" href="#module-dbm.ndbm"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code> — New Database Manager</a></li>
  547. <li><a class="reference internal" href="#module-dbm.dumb"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code> — Portable DBM implementation</a></li>
  548. </ul>
  549. </li>
  550. </ul>
  551. </div>
  552. <div>
  553. <h4>Previous topic</h4>
  554. <p class="topless"><a href="marshal.html"
  555. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code> — Internal Python object serialization</a></p>
  556. </div>
  557. <div>
  558. <h4>Next topic</h4>
  559. <p class="topless"><a href="sqlite3.html"
  560. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> — DB-API 2.0 interface for SQLite databases</a></p>
  561. </div>
  562. <div role="note" aria-label="source link">
  563. <h3>This Page</h3>
  564. <ul class="this-page-menu">
  565. <li><a href="../bugs.html">Report a Bug</a></li>
  566. <li>
  567. <a href="https://github.com/python/cpython/blob/main/Doc/library/dbm.rst"
  568. rel="nofollow">Show Source
  569. </a>
  570. </li>
  571. </ul>
  572. </div>
  573. </div>
  574. <div id="sidebarbutton" title="Collapse sidebar">
  575. <span>«</span>
  576. </div>
  577. </div>
  578. <div class="clearer"></div>
  579. </div>
  580. <div class="related" role="navigation" aria-label="related navigation">
  581. <h3>Navigation</h3>
  582. <ul>
  583. <li class="right" style="margin-right: 10px">
  584. <a href="../genindex.html" title="General Index"
  585. >index</a></li>
  586. <li class="right" >
  587. <a href="../py-modindex.html" title="Python Module Index"
  588. >modules</a> |</li>
  589. <li class="right" >
  590. <a href="sqlite3.html" title="sqlite3 — DB-API 2.0 interface for SQLite databases"
  591. >next</a> |</li>
  592. <li class="right" >
  593. <a href="marshal.html" title="marshal — Internal Python object serialization"
  594. >previous</a> |</li>
  595. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  596. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  597. <li class="switchers">
  598. <div class="language_switcher_placeholder"></div>
  599. <div class="version_switcher_placeholder"></div>
  600. </li>
  601. <li>
  602. </li>
  603. <li id="cpython-language-and-version">
  604. <a href="../index.html">3.12.3 Documentation</a> &#187;
  605. </li>
  606. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  607. <li class="nav-item nav-item-2"><a href="persistence.html" >Data Persistence</a> &#187;</li>
  608. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code> — Interfaces to Unix “databases”</a></li>
  609. <li class="right">
  610. <div class="inline-search" role="search">
  611. <form class="inline-search" action="../search.html" method="get">
  612. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  613. <input type="submit" value="Go" />
  614. </form>
  615. </div>
  616. |
  617. </li>
  618. <li class="right">
  619. <label class="theme-selector-label">
  620. Theme
  621. <select class="theme-selector" oninput="activateTheme(this.value)">
  622. <option value="auto" selected>Auto</option>
  623. <option value="light">Light</option>
  624. <option value="dark">Dark</option>
  625. </select>
  626. </label> |</li>
  627. </ul>
  628. </div>
  629. <div class="footer">
  630. &copy;
  631. <a href="../copyright.html">
  632. Copyright
  633. </a>
  634. 2001-2024, Python Software Foundation.
  635. <br />
  636. This page is licensed under the Python Software Foundation License Version 2.
  637. <br />
  638. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  639. <br />
  640. See <a href="/license.html">History and License</a> for more information.<br />
  641. <br />
  642. The Python Software Foundation is a non-profit corporation.
  643. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  644. <br />
  645. <br />
  646. Last updated on Apr 09, 2024 (13:47 UTC).
  647. <a href="/bugs.html">Found a bug</a>?
  648. <br />
  649. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  650. </div>
  651. </body>
  652. </html>
上海开阖软件有限公司 沪ICP备12045867号-1