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

6072 lines
656KB

  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="os — Miscellaneous operating system interfaces" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/os.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/os.py This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, s..." />
  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/os.py This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, s..." />
  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>os — Miscellaneous operating system interfaces &#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="io — Core tools for working with streams" href="io.html" />
  33. <link rel="prev" title="Generic Operating System Services" href="allos.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/os.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">os</span></code> — Miscellaneous operating system interfaces</a><ul>
  86. <li><a class="reference internal" href="#file-names-command-line-arguments-and-environment-variables">File Names, Command Line Arguments, and Environment Variables</a></li>
  87. <li><a class="reference internal" href="#python-utf-8-mode">Python UTF-8 Mode</a></li>
  88. <li><a class="reference internal" href="#process-parameters">Process Parameters</a></li>
  89. <li><a class="reference internal" href="#file-object-creation">File Object Creation</a></li>
  90. <li><a class="reference internal" href="#file-descriptor-operations">File Descriptor Operations</a><ul>
  91. <li><a class="reference internal" href="#querying-the-size-of-a-terminal">Querying the size of a terminal</a></li>
  92. <li><a class="reference internal" href="#inheritance-of-file-descriptors">Inheritance of File Descriptors</a></li>
  93. </ul>
  94. </li>
  95. <li><a class="reference internal" href="#files-and-directories">Files and Directories</a><ul>
  96. <li><a class="reference internal" href="#linux-extended-attributes">Linux extended attributes</a></li>
  97. </ul>
  98. </li>
  99. <li><a class="reference internal" href="#process-management">Process Management</a></li>
  100. <li><a class="reference internal" href="#interface-to-the-scheduler">Interface to the scheduler</a></li>
  101. <li><a class="reference internal" href="#miscellaneous-system-information">Miscellaneous System Information</a></li>
  102. <li><a class="reference internal" href="#random-numbers">Random numbers</a></li>
  103. </ul>
  104. </li>
  105. </ul>
  106. </div>
  107. <div>
  108. <h4>Previous topic</h4>
  109. <p class="topless"><a href="allos.html"
  110. title="previous chapter">Generic Operating System Services</a></p>
  111. </div>
  112. <div>
  113. <h4>Next topic</h4>
  114. <p class="topless"><a href="io.html"
  115. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code> — Core tools for working with streams</a></p>
  116. </div>
  117. <div role="note" aria-label="source link">
  118. <h3>This Page</h3>
  119. <ul class="this-page-menu">
  120. <li><a href="../bugs.html">Report a Bug</a></li>
  121. <li>
  122. <a href="https://github.com/python/cpython/blob/main/Doc/library/os.rst"
  123. rel="nofollow">Show Source
  124. </a>
  125. </li>
  126. </ul>
  127. </div>
  128. </nav>
  129. </div>
  130. </div>
  131. <div class="related" role="navigation" aria-label="related navigation">
  132. <h3>Navigation</h3>
  133. <ul>
  134. <li class="right" style="margin-right: 10px">
  135. <a href="../genindex.html" title="General Index"
  136. accesskey="I">index</a></li>
  137. <li class="right" >
  138. <a href="../py-modindex.html" title="Python Module Index"
  139. >modules</a> |</li>
  140. <li class="right" >
  141. <a href="io.html" title="io — Core tools for working with streams"
  142. accesskey="N">next</a> |</li>
  143. <li class="right" >
  144. <a href="allos.html" title="Generic Operating System Services"
  145. accesskey="P">previous</a> |</li>
  146. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  147. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  148. <li class="switchers">
  149. <div class="language_switcher_placeholder"></div>
  150. <div class="version_switcher_placeholder"></div>
  151. </li>
  152. <li>
  153. </li>
  154. <li id="cpython-language-and-version">
  155. <a href="../index.html">3.12.3 Documentation</a> &#187;
  156. </li>
  157. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  158. <li class="nav-item nav-item-2"><a href="allos.html" accesskey="U">Generic Operating System Services</a> &#187;</li>
  159. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code> — Miscellaneous operating system interfaces</a></li>
  160. <li class="right">
  161. <div class="inline-search" role="search">
  162. <form class="inline-search" action="../search.html" method="get">
  163. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  164. <input type="submit" value="Go" />
  165. </form>
  166. </div>
  167. |
  168. </li>
  169. <li class="right">
  170. <label class="theme-selector-label">
  171. Theme
  172. <select class="theme-selector" oninput="activateTheme(this.value)">
  173. <option value="auto" selected>Auto</option>
  174. <option value="light">Light</option>
  175. <option value="dark">Dark</option>
  176. </select>
  177. </label> |</li>
  178. </ul>
  179. </div>
  180. <div class="document">
  181. <div class="documentwrapper">
  182. <div class="bodywrapper">
  183. <div class="body" role="main">
  184. <section id="module-os">
  185. <span id="os-miscellaneous-operating-system-interfaces"></span><h1><a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> — Miscellaneous operating system interfaces<a class="headerlink" href="#module-os" title="Link to this heading">¶</a></h1>
  186. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/os.py">Lib/os.py</a></p>
  187. <hr class="docutils" />
  188. <p>This module provides a portable way of using operating system dependent
  189. functionality. If you just want to read or write a file see <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>, if
  190. you want to manipulate paths, see the <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a> module, and if you want to
  191. read all the lines in all the files on the command line see the <a class="reference internal" href="fileinput.html#module-fileinput" title="fileinput: Loop over standard input or a list of files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">fileinput</span></code></a>
  192. module. For creating temporary files and directories see the <a class="reference internal" href="tempfile.html#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code></a>
  193. module, and for high-level file and directory handling see the <a class="reference internal" href="shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a>
  194. module.</p>
  195. <p>Notes on the availability of these functions:</p>
  196. <ul class="simple">
  197. <li><p>The design of all built-in operating system dependent modules of Python is
  198. such that as long as the same functionality is available, it uses the same
  199. interface; for example, the function <code class="docutils literal notranslate"><span class="pre">os.stat(path)</span></code> returns stat
  200. information about <em>path</em> in the same format (which happens to have originated
  201. with the POSIX interface).</p></li>
  202. <li><p>Extensions peculiar to a particular operating system are also available
  203. through the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module, but using them is of course a threat to
  204. portability.</p></li>
  205. <li><p>All functions accepting path or file names accept both bytes and string
  206. objects, and result in an object of the same type, if a path or file name is
  207. returned.</p></li>
  208. <li><p>On VxWorks, os.popen, os.fork, os.execv and os.spawn*p* are not supported.</p></li>
  209. <li><p>On WebAssembly platforms <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>, large
  210. parts of the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module are not available or behave differently. API
  211. related to processes (e.g. <a class="reference internal" href="#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">fork()</span></code></a>, <a class="reference internal" href="#os.execve" title="os.execve"><code class="xref py py-func docutils literal notranslate"><span class="pre">execve()</span></code></a>), signals
  212. (e.g. <a class="reference internal" href="#os.kill" title="os.kill"><code class="xref py py-func docutils literal notranslate"><span class="pre">kill()</span></code></a>, <a class="reference internal" href="#os.wait" title="os.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a>), and resources
  213. (e.g. <a class="reference internal" href="#os.nice" title="os.nice"><code class="xref py py-func docutils literal notranslate"><span class="pre">nice()</span></code></a>) are not available. Others like <a class="reference internal" href="#os.getuid" title="os.getuid"><code class="xref py py-func docutils literal notranslate"><span class="pre">getuid()</span></code></a>
  214. and <a class="reference internal" href="#os.getpid" title="os.getpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">getpid()</span></code></a> are emulated or stubs.</p></li>
  215. </ul>
  216. <div class="admonition note">
  217. <p class="admonition-title">Note</p>
  218. <p>All functions in this module raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> (or subclasses thereof) in
  219. the case of invalid or inaccessible file names and paths, or other arguments
  220. that have the correct type, but are not accepted by the operating system.</p>
  221. </div>
  222. <dl class="py exception">
  223. <dt class="sig sig-object py" id="os.error">
  224. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">error</span></span><a class="headerlink" href="#os.error" title="Link to this definition">¶</a></dt>
  225. <dd><p>An alias for the built-in <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> exception.</p>
  226. </dd></dl>
  227. <dl class="py data">
  228. <dt class="sig sig-object py" id="os.name">
  229. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#os.name" title="Link to this definition">¶</a></dt>
  230. <dd><p>The name of the operating system dependent module imported. The following
  231. names have currently been registered: <code class="docutils literal notranslate"><span class="pre">'posix'</span></code>, <code class="docutils literal notranslate"><span class="pre">'nt'</span></code>,
  232. <code class="docutils literal notranslate"><span class="pre">'java'</span></code>.</p>
  233. <div class="admonition seealso">
  234. <p class="admonition-title">See also</p>
  235. <p><a class="reference internal" href="sys.html#sys.platform" title="sys.platform"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.platform</span></code></a> has a finer granularity. <a class="reference internal" href="#os.uname" title="os.uname"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.uname()</span></code></a> gives
  236. system-dependent version information.</p>
  237. <p>The <a class="reference internal" href="platform.html#module-platform" title="platform: Retrieves as much platform identifying data as possible."><code class="xref py py-mod docutils literal notranslate"><span class="pre">platform</span></code></a> module provides detailed checks for the
  238. system’s identity.</p>
  239. </div>
  240. </dd></dl>
  241. <section id="file-names-command-line-arguments-and-environment-variables">
  242. <span id="filesystem-encoding"></span><span id="os-filenames"></span><h2>File Names, Command Line Arguments, and Environment Variables<a class="headerlink" href="#file-names-command-line-arguments-and-environment-variables" title="Link to this heading">¶</a></h2>
  243. <p>In Python, file names, command line arguments, and environment variables are
  244. represented using the string type. On some systems, decoding these strings to
  245. and from bytes is necessary before passing them to the operating system. Python
  246. uses the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a> to perform this
  247. conversion (see <a class="reference internal" href="sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a>).</p>
  248. <p>The <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a> are configured at Python
  249. startup by the <a class="reference internal" href="../c-api/init_config.html#c.PyConfig_Read" title="PyConfig_Read"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_Read()</span></code></a> function: see
  250. <a class="reference internal" href="../c-api/init_config.html#c.PyConfig.filesystem_encoding" title="PyConfig.filesystem_encoding"><code class="xref c c-member docutils literal notranslate"><span class="pre">filesystem_encoding</span></code></a> and
  251. <a class="reference internal" href="../c-api/init_config.html#c.PyConfig.filesystem_errors" title="PyConfig.filesystem_errors"><code class="xref c c-member docutils literal notranslate"><span class="pre">filesystem_errors</span></code></a> members of <a class="reference internal" href="../c-api/init_config.html#c.PyConfig" title="PyConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyConfig</span></code></a>.</p>
  252. <div class="versionchanged">
  253. <p><span class="versionmodified changed">Changed in version 3.1: </span>On some systems, conversion using the file system encoding may fail. In this
  254. case, Python uses the <a class="reference internal" href="codecs.html#surrogateescape"><span class="std std-ref">surrogateescape encoding error handler</span></a>, which means that undecodable bytes are replaced by a
  255. Unicode character U+DC<em>xx</em> on decoding, and these are again
  256. translated to the original byte on encoding.</p>
  257. </div>
  258. <p>The <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">file system encoding</span></a> must
  259. guarantee to successfully decode all bytes below 128. If the file system
  260. encoding fails to provide this guarantee, API functions can raise
  261. <a class="reference internal" href="exceptions.html#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p>
  262. <p>See also the <a class="reference internal" href="../glossary.html#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a>.</p>
  263. </section>
  264. <section id="python-utf-8-mode">
  265. <span id="utf8-mode"></span><h2>Python UTF-8 Mode<a class="headerlink" href="#python-utf-8-mode" title="Link to this heading">¶</a></h2>
  266. <div class="versionadded">
  267. <p><span class="versionmodified added">New in version 3.7: </span>See <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0540/"><strong>PEP 540</strong></a> for more details.</p>
  268. </div>
  269. <p>The Python UTF-8 Mode ignores the <a class="reference internal" href="../glossary.html#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a> and forces the usage
  270. of the UTF-8 encoding:</p>
  271. <ul class="simple">
  272. <li><p>Use UTF-8 as the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding</span></a>.</p></li>
  273. <li><p><a class="reference internal" href="sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code>.</p></li>
  274. <li><p><a class="reference internal" href="locale.html#locale.getpreferredencoding" title="locale.getpreferredencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getpreferredencoding()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code> (the <em>do_setlocale</em>
  275. argument has no effect).</p></li>
  276. <li><p><a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>, <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a>, and <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> all use
  277. UTF-8 as their text encoding, with the <code class="docutils literal notranslate"><span class="pre">surrogateescape</span></code>
  278. <a class="reference internal" href="codecs.html#error-handlers"><span class="std std-ref">error handler</span></a> being enabled for <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>
  279. and <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> (<a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> continues to use
  280. <code class="docutils literal notranslate"><span class="pre">backslashreplace</span></code> as it does in the default locale-aware mode)</p></li>
  281. <li><p>On Unix, <a class="reference internal" href="#os.device_encoding" title="os.device_encoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.device_encoding()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code> rather than the
  282. device encoding.</p></li>
  283. </ul>
  284. <p>Note that the standard stream settings in UTF-8 mode can be overridden by
  285. <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONIOENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONIOENCODING</span></code></a> (just as they can be in the default locale-aware
  286. mode).</p>
  287. <p>As a consequence of the changes in those lower level APIs, other higher
  288. level APIs also exhibit different default behaviours:</p>
  289. <ul class="simple">
  290. <li><p>Command line arguments, environment variables and filenames are decoded
  291. to text using the UTF-8 encoding.</p></li>
  292. <li><p><a class="reference internal" href="#os.fsdecode" title="os.fsdecode"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fsdecode()</span></code></a> and <a class="reference internal" href="#os.fsencode" title="os.fsencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fsencode()</span></code></a> use the UTF-8 encoding.</p></li>
  293. <li><p><a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>, <a class="reference internal" href="io.html#io.open" title="io.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.open()</span></code></a>, and <a class="reference internal" href="codecs.html#codecs.open" title="codecs.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">codecs.open()</span></code></a> use the UTF-8
  294. encoding by default. However, they still use the strict error handler by
  295. default so that attempting to open a binary file in text mode is likely
  296. to raise an exception rather than producing nonsense data.</p></li>
  297. </ul>
  298. <p>The <a class="reference internal" href="#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> is enabled if the LC_CTYPE locale is
  299. <code class="docutils literal notranslate"><span class="pre">C</span></code> or <code class="docutils literal notranslate"><span class="pre">POSIX</span></code> at Python startup (see the <a class="reference internal" href="../c-api/init_config.html#c.PyConfig_Read" title="PyConfig_Read"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_Read()</span></code></a>
  300. function).</p>
  301. <p>It can be enabled or disabled using the <a class="reference internal" href="../using/cmdline.html#cmdoption-X"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code></a> command line
  302. option and the <span class="target" id="index-2"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONUTF8"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> environment variable.</p>
  303. <p>If the <span class="target" id="index-3"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONUTF8"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> environment variable is not set at all, then the
  304. interpreter defaults to using the current locale settings, <em>unless</em> the current
  305. locale is identified as a legacy ASCII-based locale (as described for
  306. <span class="target" id="index-4"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONCOERCECLOCALE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONCOERCECLOCALE</span></code></a>), and locale coercion is either disabled or
  307. fails. In such legacy locales, the interpreter will default to enabling UTF-8
  308. mode unless explicitly instructed not to do so.</p>
  309. <p>The Python UTF-8 Mode can only be enabled at the Python startup. Its value
  310. can be read from <a class="reference internal" href="sys.html#sys.flags" title="sys.flags"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.flags.utf8_mode</span></code></a>.</p>
  311. <p>See also the <a class="reference internal" href="../using/windows.html#win-utf8-mode"><span class="std std-ref">UTF-8 mode on Windows</span></a>
  312. and the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>.</p>
  313. <div class="admonition seealso">
  314. <p class="admonition-title">See also</p>
  315. <dl class="simple">
  316. <dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0686/"><strong>PEP 686</strong></a></dt><dd><p>Python 3.15 will make <a class="reference internal" href="#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> default.</p>
  317. </dd>
  318. </dl>
  319. </div>
  320. </section>
  321. <section id="process-parameters">
  322. <span id="os-procinfo"></span><h2>Process Parameters<a class="headerlink" href="#process-parameters" title="Link to this heading">¶</a></h2>
  323. <p>These functions and data items provide information and operate on the current
  324. process and user.</p>
  325. <dl class="py function">
  326. <dt class="sig sig-object py" id="os.ctermid">
  327. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">ctermid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.ctermid" title="Link to this definition">¶</a></dt>
  328. <dd><p>Return the filename corresponding to the controlling terminal of the process.</p>
  329. <div class="availability docutils container">
  330. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  331. </div>
  332. </dd></dl>
  333. <dl class="py data">
  334. <dt class="sig sig-object py" id="os.environ">
  335. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">environ</span></span><a class="headerlink" href="#os.environ" title="Link to this definition">¶</a></dt>
  336. <dd><p>A <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mapping</span></a> object where keys and values are strings that represent
  337. the process environment. For example, <code class="docutils literal notranslate"><span class="pre">environ['HOME']</span></code> is the pathname
  338. of your home directory (on some platforms), and is equivalent to
  339. <code class="docutils literal notranslate"><span class="pre">getenv(&quot;HOME&quot;)</span></code> in C.</p>
  340. <p>This mapping is captured the first time the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module is imported,
  341. typically during Python startup as part of processing <code class="file docutils literal notranslate"><span class="pre">site.py</span></code>. Changes
  342. to the environment made after this time are not reflected in <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>,
  343. except for changes made by modifying <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> directly.</p>
  344. <p>This mapping may be used to modify the environment as well as query the
  345. environment. <a class="reference internal" href="#os.putenv" title="os.putenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">putenv()</span></code></a> will be called automatically when the mapping
  346. is modified.</p>
  347. <p>On Unix, keys and values use <a class="reference internal" href="sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a> and
  348. <code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> error handler. Use <a class="reference internal" href="#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">environb</span></code></a> if you would like
  349. to use a different encoding.</p>
  350. <p>On Windows, the keys are converted to uppercase. This also applies when
  351. getting, setting, or deleting an item. For example,
  352. <code class="docutils literal notranslate"><span class="pre">environ['monty']</span> <span class="pre">=</span> <span class="pre">'python'</span></code> maps the key <code class="docutils literal notranslate"><span class="pre">'MONTY'</span></code> to the value
  353. <code class="docutils literal notranslate"><span class="pre">'python'</span></code>.</p>
  354. <div class="admonition note">
  355. <p class="admonition-title">Note</p>
  356. <p>Calling <a class="reference internal" href="#os.putenv" title="os.putenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">putenv()</span></code></a> directly does not change <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>, so it’s better
  357. to modify <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>.</p>
  358. </div>
  359. <div class="admonition note">
  360. <p class="admonition-title">Note</p>
  361. <p>On some platforms, including FreeBSD and macOS, setting <code class="docutils literal notranslate"><span class="pre">environ</span></code> may
  362. cause memory leaks. Refer to the system documentation for
  363. <code class="xref c c-func docutils literal notranslate"><span class="pre">putenv()</span></code>.</p>
  364. </div>
  365. <p>You can delete items in this mapping to unset environment variables.
  366. <a class="reference internal" href="#os.unsetenv" title="os.unsetenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">unsetenv()</span></code></a> will be called automatically when an item is deleted from
  367. <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>, and when one of the <code class="xref py py-meth docutils literal notranslate"><span class="pre">pop()</span></code> or <code class="xref py py-meth docutils literal notranslate"><span class="pre">clear()</span></code> methods is
  368. called.</p>
  369. <div class="versionchanged">
  370. <p><span class="versionmodified changed">Changed in version 3.9: </span>Updated to support <span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0584/"><strong>PEP 584</strong></a>’s merge (<code class="docutils literal notranslate"><span class="pre">|</span></code>) and update (<code class="docutils literal notranslate"><span class="pre">|=</span></code>) operators.</p>
  371. </div>
  372. </dd></dl>
  373. <dl class="py data">
  374. <dt class="sig sig-object py" id="os.environb">
  375. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">environb</span></span><a class="headerlink" href="#os.environb" title="Link to this definition">¶</a></dt>
  376. <dd><p>Bytes version of <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">environ</span></code></a>: a <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mapping</span></a> object where both keys
  377. and values are <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> objects representing the process environment.
  378. <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">environ</span></code></a> and <a class="reference internal" href="#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">environb</span></code></a> are synchronized (modifying
  379. <a class="reference internal" href="#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">environb</span></code></a> updates <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">environ</span></code></a>, and vice versa).</p>
  380. <p><a class="reference internal" href="#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">environb</span></code></a> is only available if <a class="reference internal" href="#os.supports_bytes_environ" title="os.supports_bytes_environ"><code class="xref py py-const docutils literal notranslate"><span class="pre">supports_bytes_environ</span></code></a> is
  381. <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  382. <div class="versionadded">
  383. <p><span class="versionmodified added">New in version 3.2.</span></p>
  384. </div>
  385. <div class="versionchanged">
  386. <p><span class="versionmodified changed">Changed in version 3.9: </span>Updated to support <span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0584/"><strong>PEP 584</strong></a>’s merge (<code class="docutils literal notranslate"><span class="pre">|</span></code>) and update (<code class="docutils literal notranslate"><span class="pre">|=</span></code>) operators.</p>
  387. </div>
  388. </dd></dl>
  389. <dl class="py function">
  390. <dt class="sig sig-object py">
  391. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">chdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span></dt>
  392. <dt class="sig sig-object py">
  393. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fchdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span></dt>
  394. <dt class="sig sig-object py">
  395. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getcwd</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
  396. <dd><p>These functions are described in <a class="reference internal" href="#os-file-dir"><span class="std std-ref">Files and Directories</span></a>.</p>
  397. </dd></dl>
  398. <dl class="py function">
  399. <dt class="sig sig-object py" id="os.fsencode">
  400. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fsencode</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="#os.fsencode" title="Link to this definition">¶</a></dt>
  401. <dd><p>Encode <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like</span></a> <em>filename</em> to the
  402. <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>; return <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>
  403. unchanged.</p>
  404. <p><a class="reference internal" href="#os.fsdecode" title="os.fsdecode"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsdecode()</span></code></a> is the reverse function.</p>
  405. <div class="versionadded">
  406. <p><span class="versionmodified added">New in version 3.2.</span></p>
  407. </div>
  408. <div class="versionchanged">
  409. <p><span class="versionmodified changed">Changed in version 3.6: </span>Support added to accept objects implementing the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.PathLike</span></code></a>
  410. interface.</p>
  411. </div>
  412. </dd></dl>
  413. <dl class="py function">
  414. <dt class="sig sig-object py" id="os.fsdecode">
  415. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fsdecode</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="#os.fsdecode" title="Link to this definition">¶</a></dt>
  416. <dd><p>Decode the <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like</span></a> <em>filename</em> from the
  417. <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>; return <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>
  418. unchanged.</p>
  419. <p><a class="reference internal" href="#os.fsencode" title="os.fsencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsencode()</span></code></a> is the reverse function.</p>
  420. <div class="versionadded">
  421. <p><span class="versionmodified added">New in version 3.2.</span></p>
  422. </div>
  423. <div class="versionchanged">
  424. <p><span class="versionmodified changed">Changed in version 3.6: </span>Support added to accept objects implementing the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.PathLike</span></code></a>
  425. interface.</p>
  426. </div>
  427. </dd></dl>
  428. <dl class="py function">
  429. <dt class="sig sig-object py" id="os.fspath">
  430. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fspath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.fspath" title="Link to this definition">¶</a></dt>
  431. <dd><p>Return the file system representation of the path.</p>
  432. <p>If <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> is passed in, it is returned unchanged.
  433. Otherwise <a class="reference internal" href="#os.PathLike.__fspath__" title="os.PathLike.__fspath__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__fspath__()</span></code></a> is called and its value is
  434. returned as long as it is a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object.
  435. In all other cases, <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p>
  436. <div class="versionadded">
  437. <p><span class="versionmodified added">New in version 3.6.</span></p>
  438. </div>
  439. </dd></dl>
  440. <dl class="py class">
  441. <dt class="sig sig-object py" id="os.PathLike">
  442. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">PathLike</span></span><a class="headerlink" href="#os.PathLike" title="Link to this definition">¶</a></dt>
  443. <dd><p>An <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> for objects representing a file system path,
  444. e.g. <a class="reference internal" href="pathlib.html#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">pathlib.PurePath</span></code></a>.</p>
  445. <div class="versionadded">
  446. <p><span class="versionmodified added">New in version 3.6.</span></p>
  447. </div>
  448. <dl class="py method">
  449. <dt class="sig sig-object py" id="os.PathLike.__fspath__">
  450. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">__fspath__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.PathLike.__fspath__" title="Link to this definition">¶</a></dt>
  451. <dd><p>Return the file system path representation of the object.</p>
  452. <p>The method should only return a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object,
  453. with the preference being for <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>.</p>
  454. </dd></dl>
  455. </dd></dl>
  456. <dl class="py function">
  457. <dt class="sig sig-object py" id="os.getenv">
  458. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getenv</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.getenv" title="Link to this definition">¶</a></dt>
  459. <dd><p>Return the value of the environment variable <em>key</em> as a string if it exists, or
  460. <em>default</em> if it doesn’t. <em>key</em> is a string. Note that
  461. since <a class="reference internal" href="#os.getenv" title="os.getenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">getenv()</span></code></a> uses <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>, the mapping of <a class="reference internal" href="#os.getenv" title="os.getenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">getenv()</span></code></a> is
  462. similarly also captured on import, and the function may not reflect
  463. future environment changes.</p>
  464. <p>On Unix, keys and values are decoded with <a class="reference internal" href="sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a>
  465. and <code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> error handler. Use <a class="reference internal" href="#os.getenvb" title="os.getenvb"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.getenvb()</span></code></a> if you
  466. would like to use a different encoding.</p>
  467. <div class="availability docutils container">
  468. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  469. </div>
  470. </dd></dl>
  471. <dl class="py function">
  472. <dt class="sig sig-object py" id="os.getenvb">
  473. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getenvb</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.getenvb" title="Link to this definition">¶</a></dt>
  474. <dd><p>Return the value of the environment variable <em>key</em> as bytes if it exists, or
  475. <em>default</em> if it doesn’t. <em>key</em> must be bytes. Note that
  476. since <a class="reference internal" href="#os.getenvb" title="os.getenvb"><code class="xref py py-func docutils literal notranslate"><span class="pre">getenvb()</span></code></a> uses <a class="reference internal" href="#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environb</span></code></a>, the mapping of <a class="reference internal" href="#os.getenvb" title="os.getenvb"><code class="xref py py-func docutils literal notranslate"><span class="pre">getenvb()</span></code></a> is
  477. similarly also captured on import, and the function may not reflect
  478. future environment changes.</p>
  479. <p><a class="reference internal" href="#os.getenvb" title="os.getenvb"><code class="xref py py-func docutils literal notranslate"><span class="pre">getenvb()</span></code></a> is only available if <a class="reference internal" href="#os.supports_bytes_environ" title="os.supports_bytes_environ"><code class="xref py py-const docutils literal notranslate"><span class="pre">supports_bytes_environ</span></code></a>
  480. is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  481. <div class="availability docutils container">
  482. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  483. </div>
  484. <div class="versionadded">
  485. <p><span class="versionmodified added">New in version 3.2.</span></p>
  486. </div>
  487. </dd></dl>
  488. <dl class="py function">
  489. <dt class="sig sig-object py" id="os.get_exec_path">
  490. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">get_exec_path</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">env</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.get_exec_path" title="Link to this definition">¶</a></dt>
  491. <dd><p>Returns the list of directories that will be searched for a named
  492. executable, similar to a shell, when launching a process.
  493. <em>env</em>, when specified, should be an environment variable dictionary
  494. to lookup the PATH in.
  495. By default, when <em>env</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">environ</span></code></a> is used.</p>
  496. <div class="versionadded">
  497. <p><span class="versionmodified added">New in version 3.2.</span></p>
  498. </div>
  499. </dd></dl>
  500. <dl class="py function">
  501. <dt class="sig sig-object py" id="os.getegid">
  502. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getegid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getegid" title="Link to this definition">¶</a></dt>
  503. <dd><p>Return the effective group id of the current process. This corresponds to the
  504. “set id” bit on the file being executed in the current process.</p>
  505. <div class="availability docutils container">
  506. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  507. </div>
  508. </dd></dl>
  509. <dl class="py function">
  510. <dt class="sig sig-object py" id="os.geteuid">
  511. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">geteuid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.geteuid" title="Link to this definition">¶</a></dt>
  512. <dd><p id="index-8">Return the current process’s effective user id.</p>
  513. <div class="availability docutils container">
  514. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  515. </div>
  516. </dd></dl>
  517. <dl class="py function">
  518. <dt class="sig sig-object py" id="os.getgid">
  519. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getgid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getgid" title="Link to this definition">¶</a></dt>
  520. <dd><p id="index-9">Return the real group id of the current process.</p>
  521. <div class="availability docutils container">
  522. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  523. <p>The function is a stub on Emscripten and WASI, see
  524. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  525. </div>
  526. </dd></dl>
  527. <dl class="py function">
  528. <dt class="sig sig-object py" id="os.getgrouplist">
  529. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getgrouplist</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">user</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">group</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="#os.getgrouplist" title="Link to this definition">¶</a></dt>
  530. <dd><p>Return list of group ids that <em>user</em> belongs to. If <em>group</em> is not in the
  531. list, it is included; typically, <em>group</em> is specified as the group ID
  532. field from the password record for <em>user</em>, because that group ID will
  533. otherwise be potentially omitted.</p>
  534. <div class="availability docutils container">
  535. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  536. </div>
  537. <div class="versionadded">
  538. <p><span class="versionmodified added">New in version 3.3.</span></p>
  539. </div>
  540. </dd></dl>
  541. <dl class="py function">
  542. <dt class="sig sig-object py" id="os.getgroups">
  543. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getgroups</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getgroups" title="Link to this definition">¶</a></dt>
  544. <dd><p>Return list of supplemental group ids associated with the current process.</p>
  545. <div class="availability docutils container">
  546. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  547. </div>
  548. <div class="admonition note">
  549. <p class="admonition-title">Note</p>
  550. <p>On macOS, <a class="reference internal" href="#os.getgroups" title="os.getgroups"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgroups()</span></code></a> behavior differs somewhat from
  551. other Unix platforms. If the Python interpreter was built with a
  552. deployment target of <code class="docutils literal notranslate"><span class="pre">10.5</span></code> or earlier, <a class="reference internal" href="#os.getgroups" title="os.getgroups"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgroups()</span></code></a> returns
  553. the list of effective group ids associated with the current user process;
  554. this list is limited to a system-defined number of entries, typically 16,
  555. and may be modified by calls to <a class="reference internal" href="#os.setgroups" title="os.setgroups"><code class="xref py py-func docutils literal notranslate"><span class="pre">setgroups()</span></code></a> if suitably privileged.
  556. If built with a deployment target greater than <code class="docutils literal notranslate"><span class="pre">10.5</span></code>,
  557. <a class="reference internal" href="#os.getgroups" title="os.getgroups"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgroups()</span></code></a> returns the current group access list for the user
  558. associated with the effective user id of the process; the group access
  559. list may change over the lifetime of the process, it is not affected by
  560. calls to <a class="reference internal" href="#os.setgroups" title="os.setgroups"><code class="xref py py-func docutils literal notranslate"><span class="pre">setgroups()</span></code></a>, and its length is not limited to 16. The
  561. deployment target value, <code class="xref py py-const docutils literal notranslate"><span class="pre">MACOSX_DEPLOYMENT_TARGET</span></code>, can be
  562. obtained with <a class="reference internal" href="sysconfig.html#sysconfig.get_config_var" title="sysconfig.get_config_var"><code class="xref py py-func docutils literal notranslate"><span class="pre">sysconfig.get_config_var()</span></code></a>.</p>
  563. </div>
  564. </dd></dl>
  565. <dl class="py function">
  566. <dt class="sig sig-object py" id="os.getlogin">
  567. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getlogin</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getlogin" title="Link to this definition">¶</a></dt>
  568. <dd><p>Return the name of the user logged in on the controlling terminal of the
  569. process. For most purposes, it is more useful to use
  570. <a class="reference internal" href="getpass.html#getpass.getuser" title="getpass.getuser"><code class="xref py py-func docutils literal notranslate"><span class="pre">getpass.getuser()</span></code></a> since the latter checks the environment variables
  571. <span class="target" id="index-10"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LOGNAME</span></code> or <span class="target" id="index-11"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">USERNAME</span></code> to find out who the user is, and
  572. falls back to <code class="docutils literal notranslate"><span class="pre">pwd.getpwuid(os.getuid())[0]</span></code> to get the login name of the
  573. current real user id.</p>
  574. <div class="availability docutils container">
  575. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p>
  576. </div>
  577. </dd></dl>
  578. <dl class="py function">
  579. <dt class="sig sig-object py" id="os.getpgid">
  580. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getpgid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.getpgid" title="Link to this definition">¶</a></dt>
  581. <dd><p>Return the process group id of the process with process id <em>pid</em>. If <em>pid</em> is 0,
  582. the process group id of the current process is returned.</p>
  583. <div class="availability docutils container">
  584. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  585. </div>
  586. </dd></dl>
  587. <dl class="py function">
  588. <dt class="sig sig-object py" id="os.getpgrp">
  589. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getpgrp</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getpgrp" title="Link to this definition">¶</a></dt>
  590. <dd><p id="index-12">Return the id of the current process group.</p>
  591. <div class="availability docutils container">
  592. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  593. </div>
  594. </dd></dl>
  595. <dl class="py function">
  596. <dt class="sig sig-object py" id="os.getpid">
  597. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getpid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getpid" title="Link to this definition">¶</a></dt>
  598. <dd><p id="index-13">Return the current process id.</p>
  599. <p>The function is a stub on Emscripten and WASI, see
  600. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  601. </dd></dl>
  602. <dl class="py function">
  603. <dt class="sig sig-object py" id="os.getppid">
  604. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getppid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getppid" title="Link to this definition">¶</a></dt>
  605. <dd><p id="index-14">Return the parent’s process id. When the parent process has exited, on Unix
  606. the id returned is the one of the init process (1), on Windows it is still
  607. the same id, which may be already reused by another process.</p>
  608. <div class="availability docutils container">
  609. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p>
  610. </div>
  611. <div class="versionchanged">
  612. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added support for Windows.</p>
  613. </div>
  614. </dd></dl>
  615. <dl class="py function">
  616. <dt class="sig sig-object py" id="os.getpriority">
  617. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getpriority</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">which</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">who</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.getpriority" title="Link to this definition">¶</a></dt>
  618. <dd><p id="index-15">Get program scheduling priority. The value <em>which</em> is one of
  619. <a class="reference internal" href="#os.PRIO_PROCESS" title="os.PRIO_PROCESS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_PROCESS</span></code></a>, <a class="reference internal" href="#os.PRIO_PGRP" title="os.PRIO_PGRP"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_PGRP</span></code></a>, or <a class="reference internal" href="#os.PRIO_USER" title="os.PRIO_USER"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_USER</span></code></a>, and <em>who</em>
  620. is interpreted relative to <em>which</em> (a process identifier for
  621. <a class="reference internal" href="#os.PRIO_PROCESS" title="os.PRIO_PROCESS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_PROCESS</span></code></a>, process group identifier for <a class="reference internal" href="#os.PRIO_PGRP" title="os.PRIO_PGRP"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_PGRP</span></code></a>, and a
  622. user ID for <a class="reference internal" href="#os.PRIO_USER" title="os.PRIO_USER"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_USER</span></code></a>). A zero value for <em>who</em> denotes
  623. (respectively) the calling process, the process group of the calling process,
  624. or the real user ID of the calling process.</p>
  625. <div class="availability docutils container">
  626. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  627. </div>
  628. <div class="versionadded">
  629. <p><span class="versionmodified added">New in version 3.3.</span></p>
  630. </div>
  631. </dd></dl>
  632. <dl class="py data">
  633. <dt class="sig sig-object py" id="os.PRIO_PROCESS">
  634. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">PRIO_PROCESS</span></span><a class="headerlink" href="#os.PRIO_PROCESS" title="Link to this definition">¶</a></dt>
  635. <dt class="sig sig-object py" id="os.PRIO_PGRP">
  636. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">PRIO_PGRP</span></span><a class="headerlink" href="#os.PRIO_PGRP" title="Link to this definition">¶</a></dt>
  637. <dt class="sig sig-object py" id="os.PRIO_USER">
  638. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">PRIO_USER</span></span><a class="headerlink" href="#os.PRIO_USER" title="Link to this definition">¶</a></dt>
  639. <dd><p>Parameters for the <a class="reference internal" href="#os.getpriority" title="os.getpriority"><code class="xref py py-func docutils literal notranslate"><span class="pre">getpriority()</span></code></a> and <a class="reference internal" href="#os.setpriority" title="os.setpriority"><code class="xref py py-func docutils literal notranslate"><span class="pre">setpriority()</span></code></a> functions.</p>
  640. <div class="availability docutils container">
  641. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  642. </div>
  643. <div class="versionadded">
  644. <p><span class="versionmodified added">New in version 3.3.</span></p>
  645. </div>
  646. </dd></dl>
  647. <dl class="py data">
  648. <dt class="sig sig-object py" id="os.PRIO_DARWIN_THREAD">
  649. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">PRIO_DARWIN_THREAD</span></span><a class="headerlink" href="#os.PRIO_DARWIN_THREAD" title="Link to this definition">¶</a></dt>
  650. <dt class="sig sig-object py" id="os.PRIO_DARWIN_PROCESS">
  651. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">PRIO_DARWIN_PROCESS</span></span><a class="headerlink" href="#os.PRIO_DARWIN_PROCESS" title="Link to this definition">¶</a></dt>
  652. <dt class="sig sig-object py" id="os.PRIO_DARWIN_BG">
  653. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">PRIO_DARWIN_BG</span></span><a class="headerlink" href="#os.PRIO_DARWIN_BG" title="Link to this definition">¶</a></dt>
  654. <dt class="sig sig-object py" id="os.PRIO_DARWIN_NONUI">
  655. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">PRIO_DARWIN_NONUI</span></span><a class="headerlink" href="#os.PRIO_DARWIN_NONUI" title="Link to this definition">¶</a></dt>
  656. <dd><p>Parameters for the <a class="reference internal" href="#os.getpriority" title="os.getpriority"><code class="xref py py-func docutils literal notranslate"><span class="pre">getpriority()</span></code></a> and <a class="reference internal" href="#os.setpriority" title="os.setpriority"><code class="xref py py-func docutils literal notranslate"><span class="pre">setpriority()</span></code></a> functions.</p>
  657. <div class="availability docutils container">
  658. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: macOS</p>
  659. </div>
  660. <div class="versionadded">
  661. <p><span class="versionmodified added">New in version 3.12.</span></p>
  662. </div>
  663. </dd></dl>
  664. <dl class="py function">
  665. <dt class="sig sig-object py" id="os.getresuid">
  666. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getresuid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getresuid" title="Link to this definition">¶</a></dt>
  667. <dd><p>Return a tuple (ruid, euid, suid) denoting the current process’s
  668. real, effective, and saved user ids.</p>
  669. <div class="availability docutils container">
  670. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  671. </div>
  672. <div class="versionadded">
  673. <p><span class="versionmodified added">New in version 3.2.</span></p>
  674. </div>
  675. </dd></dl>
  676. <dl class="py function">
  677. <dt class="sig sig-object py" id="os.getresgid">
  678. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getresgid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getresgid" title="Link to this definition">¶</a></dt>
  679. <dd><p>Return a tuple (rgid, egid, sgid) denoting the current process’s
  680. real, effective, and saved group ids.</p>
  681. <div class="availability docutils container">
  682. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  683. </div>
  684. <div class="versionadded">
  685. <p><span class="versionmodified added">New in version 3.2.</span></p>
  686. </div>
  687. </dd></dl>
  688. <dl class="py function">
  689. <dt class="sig sig-object py" id="os.getuid">
  690. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getuid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getuid" title="Link to this definition">¶</a></dt>
  691. <dd><p id="index-16">Return the current process’s real user id.</p>
  692. <div class="availability docutils container">
  693. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  694. <p>The function is a stub on Emscripten and WASI, see
  695. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  696. </div>
  697. </dd></dl>
  698. <dl class="py function">
  699. <dt class="sig sig-object py" id="os.initgroups">
  700. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">initgroups</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">username</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gid</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="#os.initgroups" title="Link to this definition">¶</a></dt>
  701. <dd><p>Call the system initgroups() to initialize the group access list with all of
  702. the groups of which the specified username is a member, plus the specified
  703. group id.</p>
  704. <div class="availability docutils container">
  705. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  706. </div>
  707. <div class="versionadded">
  708. <p><span class="versionmodified added">New in version 3.2.</span></p>
  709. </div>
  710. </dd></dl>
  711. <dl class="py function">
  712. <dt class="sig sig-object py" id="os.putenv">
  713. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">putenv</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</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="#os.putenv" title="Link to this definition">¶</a></dt>
  714. <dd><p id="index-17">Set the environment variable named <em>key</em> to the string <em>value</em>. Such
  715. changes to the environment affect subprocesses started with <a class="reference internal" href="#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a>,
  716. <a class="reference internal" href="#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">popen()</span></code></a> or <a class="reference internal" href="#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">fork()</span></code></a> and <a class="reference internal" href="#os.execv" title="os.execv"><code class="xref py py-func docutils literal notranslate"><span class="pre">execv()</span></code></a>.</p>
  717. <p>Assignments to items in <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> are automatically translated into
  718. corresponding calls to <a class="reference internal" href="#os.putenv" title="os.putenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">putenv()</span></code></a>; however, calls to <a class="reference internal" href="#os.putenv" title="os.putenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">putenv()</span></code></a>
  719. don’t update <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>, so it is actually preferable to assign to items
  720. of <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>. This also applies to <a class="reference internal" href="#os.getenv" title="os.getenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">getenv()</span></code></a> and <a class="reference internal" href="#os.getenvb" title="os.getenvb"><code class="xref py py-func docutils literal notranslate"><span class="pre">getenvb()</span></code></a>, which
  721. respectively use <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> and <a class="reference internal" href="#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environb</span></code></a> in their implementations.</p>
  722. <div class="admonition note">
  723. <p class="admonition-title">Note</p>
  724. <p>On some platforms, including FreeBSD and macOS, setting <code class="docutils literal notranslate"><span class="pre">environ</span></code> may
  725. cause memory leaks. Refer to the system documentation for <code class="xref c c-func docutils literal notranslate"><span class="pre">putenv()</span></code>.</p>
  726. </div>
  727. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.putenv</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">key</span></code>, <code class="docutils literal notranslate"><span class="pre">value</span></code>.</p>
  728. <div class="versionchanged">
  729. <p><span class="versionmodified changed">Changed in version 3.9: </span>The function is now always available.</p>
  730. </div>
  731. </dd></dl>
  732. <dl class="py function">
  733. <dt class="sig sig-object py" id="os.setegid">
  734. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setegid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">egid</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="#os.setegid" title="Link to this definition">¶</a></dt>
  735. <dd><p>Set the current process’s effective group id.</p>
  736. <div class="availability docutils container">
  737. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  738. </div>
  739. </dd></dl>
  740. <dl class="py function">
  741. <dt class="sig sig-object py" id="os.seteuid">
  742. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">seteuid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">euid</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="#os.seteuid" title="Link to this definition">¶</a></dt>
  743. <dd><p>Set the current process’s effective user id.</p>
  744. <div class="availability docutils container">
  745. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  746. </div>
  747. </dd></dl>
  748. <dl class="py function">
  749. <dt class="sig sig-object py" id="os.setgid">
  750. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setgid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gid</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="#os.setgid" title="Link to this definition">¶</a></dt>
  751. <dd><p>Set the current process’ group id.</p>
  752. <div class="availability docutils container">
  753. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  754. </div>
  755. </dd></dl>
  756. <dl class="py function">
  757. <dt class="sig sig-object py" id="os.setgroups">
  758. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setgroups</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">groups</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="#os.setgroups" title="Link to this definition">¶</a></dt>
  759. <dd><p>Set the list of supplemental group ids associated with the current process to
  760. <em>groups</em>. <em>groups</em> must be a sequence, and each element must be an integer
  761. identifying a group. This operation is typically available only to the superuser.</p>
  762. <div class="availability docutils container">
  763. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  764. </div>
  765. <div class="admonition note">
  766. <p class="admonition-title">Note</p>
  767. <p>On macOS, the length of <em>groups</em> may not exceed the
  768. system-defined maximum number of effective group ids, typically 16.
  769. See the documentation for <a class="reference internal" href="#os.getgroups" title="os.getgroups"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgroups()</span></code></a> for cases where it may not
  770. return the same group list set by calling setgroups().</p>
  771. </div>
  772. </dd></dl>
  773. <dl class="py function">
  774. <dt class="sig sig-object py" id="os.setns">
  775. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">nstype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.setns" title="Link to this definition">¶</a></dt>
  776. <dd><p>Reassociate the current thread with a Linux namespace.
  777. See the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/setns(2)">setns(2)</a></em> and <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/namespaces(7)">namespaces(7)</a></em> man pages for more
  778. details.</p>
  779. <p>If <em>fd</em> refers to a <code class="file docutils literal notranslate"><span class="pre">/proc/</span><em><span class="pre">pid</span></em><span class="pre">/ns/</span></code> link, <code class="docutils literal notranslate"><span class="pre">setns()</span></code> reassociates the
  780. calling thread with the namespace associated with that link,
  781. and <em>nstype</em> may be set to one of the
  782. <a class="reference internal" href="#os-unshare-clone-flags"><span class="std std-ref">CLONE_NEW* constants</span></a>
  783. to impose constraints on the operation
  784. (<code class="docutils literal notranslate"><span class="pre">0</span></code> means no constraints).</p>
  785. <p>Since Linux 5.8, <em>fd</em> may refer to a PID file descriptor obtained from
  786. <a class="reference internal" href="#os.pidfd_open" title="os.pidfd_open"><code class="xref py py-func docutils literal notranslate"><span class="pre">pidfd_open()</span></code></a>. In this case, <code class="docutils literal notranslate"><span class="pre">setns()</span></code> reassociates the calling thread
  787. into one or more of the same namespaces as the thread referred to by <em>fd</em>.
  788. This is subject to any constraints imposed by <em>nstype</em>,
  789. which is a bit mask combining one or more of the
  790. <a class="reference internal" href="#os-unshare-clone-flags"><span class="std std-ref">CLONE_NEW* constants</span></a>,
  791. e.g. <code class="docutils literal notranslate"><span class="pre">setns(fd,</span> <span class="pre">os.CLONE_NEWUTS</span> <span class="pre">|</span> <span class="pre">os.CLONE_NEWPID)</span></code>.
  792. The caller’s memberships in unspecified namespaces are left unchanged.</p>
  793. <p><em>fd</em> can be any object with a <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> method, or a raw file descriptor.</p>
  794. <p>This example reassociates the thread with the <code class="docutils literal notranslate"><span class="pre">init</span></code> process’s network namespace:</p>
  795. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">fd</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;/proc/1/ns/net&quot;</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">O_RDONLY</span><span class="p">)</span>
  796. <span class="n">os</span><span class="o">.</span><span class="n">setns</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">CLONE_NEWNET</span><span class="p">)</span>
  797. <span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
  798. </pre></div>
  799. </div>
  800. <div class="availability docutils container">
  801. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 3.0 with glibc &gt;= 2.14.</p>
  802. </div>
  803. <div class="versionadded">
  804. <p><span class="versionmodified added">New in version 3.12.</span></p>
  805. </div>
  806. <div class="admonition seealso">
  807. <p class="admonition-title">See also</p>
  808. <p>The <a class="reference internal" href="#os.unshare" title="os.unshare"><code class="xref py py-func docutils literal notranslate"><span class="pre">unshare()</span></code></a> function.</p>
  809. </div>
  810. </dd></dl>
  811. <dl class="py function">
  812. <dt class="sig sig-object py" id="os.setpgrp">
  813. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setpgrp</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.setpgrp" title="Link to this definition">¶</a></dt>
  814. <dd><p>Call the system call <code class="xref c c-func docutils literal notranslate"><span class="pre">setpgrp()</span></code> or <code class="docutils literal notranslate"><span class="pre">setpgrp(0,</span> <span class="pre">0)</span></code> depending on
  815. which version is implemented (if any). See the Unix manual for the semantics.</p>
  816. <div class="availability docutils container">
  817. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  818. </div>
  819. </dd></dl>
  820. <dl class="py function">
  821. <dt class="sig sig-object py" id="os.setpgid">
  822. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setpgid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pgrp</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="#os.setpgid" title="Link to this definition">¶</a></dt>
  823. <dd><p>Call the system call <code class="xref c c-func docutils literal notranslate"><span class="pre">setpgid()</span></code> to set the process group id of the
  824. process with id <em>pid</em> to the process group with id <em>pgrp</em>. See the Unix manual
  825. for the semantics.</p>
  826. <div class="availability docutils container">
  827. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  828. </div>
  829. </dd></dl>
  830. <dl class="py function">
  831. <dt class="sig sig-object py" id="os.setpriority">
  832. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setpriority</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">which</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">who</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">priority</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.setpriority" title="Link to this definition">¶</a></dt>
  833. <dd><p id="index-18">Set program scheduling priority. The value <em>which</em> is one of
  834. <a class="reference internal" href="#os.PRIO_PROCESS" title="os.PRIO_PROCESS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_PROCESS</span></code></a>, <a class="reference internal" href="#os.PRIO_PGRP" title="os.PRIO_PGRP"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_PGRP</span></code></a>, or <a class="reference internal" href="#os.PRIO_USER" title="os.PRIO_USER"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_USER</span></code></a>, and <em>who</em>
  835. is interpreted relative to <em>which</em> (a process identifier for
  836. <a class="reference internal" href="#os.PRIO_PROCESS" title="os.PRIO_PROCESS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_PROCESS</span></code></a>, process group identifier for <a class="reference internal" href="#os.PRIO_PGRP" title="os.PRIO_PGRP"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_PGRP</span></code></a>, and a
  837. user ID for <a class="reference internal" href="#os.PRIO_USER" title="os.PRIO_USER"><code class="xref py py-const docutils literal notranslate"><span class="pre">PRIO_USER</span></code></a>). A zero value for <em>who</em> denotes
  838. (respectively) the calling process, the process group of the calling process,
  839. or the real user ID of the calling process.
  840. <em>priority</em> is a value in the range -20 to 19. The default priority is 0;
  841. lower priorities cause more favorable scheduling.</p>
  842. <div class="availability docutils container">
  843. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  844. </div>
  845. <div class="versionadded">
  846. <p><span class="versionmodified added">New in version 3.3.</span></p>
  847. </div>
  848. </dd></dl>
  849. <dl class="py function">
  850. <dt class="sig sig-object py" id="os.setregid">
  851. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setregid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">rgid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">egid</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="#os.setregid" title="Link to this definition">¶</a></dt>
  852. <dd><p>Set the current process’s real and effective group ids.</p>
  853. <div class="availability docutils container">
  854. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  855. </div>
  856. </dd></dl>
  857. <dl class="py function">
  858. <dt class="sig sig-object py" id="os.setresgid">
  859. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setresgid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">rgid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">egid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sgid</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="#os.setresgid" title="Link to this definition">¶</a></dt>
  860. <dd><p>Set the current process’s real, effective, and saved group ids.</p>
  861. <div class="availability docutils container">
  862. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  863. </div>
  864. <div class="versionadded">
  865. <p><span class="versionmodified added">New in version 3.2.</span></p>
  866. </div>
  867. </dd></dl>
  868. <dl class="py function">
  869. <dt class="sig sig-object py" id="os.setresuid">
  870. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setresuid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ruid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">euid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">suid</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="#os.setresuid" title="Link to this definition">¶</a></dt>
  871. <dd><p>Set the current process’s real, effective, and saved user ids.</p>
  872. <div class="availability docutils container">
  873. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  874. </div>
  875. <div class="versionadded">
  876. <p><span class="versionmodified added">New in version 3.2.</span></p>
  877. </div>
  878. </dd></dl>
  879. <dl class="py function">
  880. <dt class="sig sig-object py" id="os.setreuid">
  881. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setreuid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ruid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">euid</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="#os.setreuid" title="Link to this definition">¶</a></dt>
  882. <dd><p>Set the current process’s real and effective user ids.</p>
  883. <div class="availability docutils container">
  884. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  885. </div>
  886. </dd></dl>
  887. <dl class="py function">
  888. <dt class="sig sig-object py" id="os.getsid">
  889. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getsid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</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="#os.getsid" title="Link to this definition">¶</a></dt>
  890. <dd><p>Call the system call <code class="xref c c-func docutils literal notranslate"><span class="pre">getsid()</span></code>. See the Unix manual for the semantics.</p>
  891. <div class="availability docutils container">
  892. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  893. </div>
  894. </dd></dl>
  895. <dl class="py function">
  896. <dt class="sig sig-object py" id="os.setsid">
  897. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setsid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.setsid" title="Link to this definition">¶</a></dt>
  898. <dd><p>Call the system call <code class="xref c c-func docutils literal notranslate"><span class="pre">setsid()</span></code>. See the Unix manual for the semantics.</p>
  899. <div class="availability docutils container">
  900. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  901. </div>
  902. </dd></dl>
  903. <dl class="py function">
  904. <dt class="sig sig-object py" id="os.setuid">
  905. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setuid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">uid</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="#os.setuid" title="Link to this definition">¶</a></dt>
  906. <dd><p id="index-19">Set the current process’s user id.</p>
  907. <div class="availability docutils container">
  908. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  909. </div>
  910. </dd></dl>
  911. <dl class="py function">
  912. <dt class="sig sig-object py" id="os.strerror">
  913. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">strerror</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">code</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="#os.strerror" title="Link to this definition">¶</a></dt>
  914. <dd><p>Return the error message corresponding to the error code in <em>code</em>.
  915. On platforms where <code class="xref c c-func docutils literal notranslate"><span class="pre">strerror()</span></code> returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> when given an unknown
  916. error number, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
  917. </dd></dl>
  918. <dl class="py data">
  919. <dt class="sig sig-object py" id="os.supports_bytes_environ">
  920. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">supports_bytes_environ</span></span><a class="headerlink" href="#os.supports_bytes_environ" title="Link to this definition">¶</a></dt>
  921. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the native OS type of the environment is bytes (eg. <code class="docutils literal notranslate"><span class="pre">False</span></code> on
  922. Windows).</p>
  923. <div class="versionadded">
  924. <p><span class="versionmodified added">New in version 3.2.</span></p>
  925. </div>
  926. </dd></dl>
  927. <dl class="py function">
  928. <dt class="sig sig-object py" id="os.umask">
  929. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">umask</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mask</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="#os.umask" title="Link to this definition">¶</a></dt>
  930. <dd><p>Set the current numeric umask and return the previous umask.</p>
  931. <p>The function is a stub on Emscripten and WASI, see
  932. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  933. </dd></dl>
  934. <dl class="py function">
  935. <dt class="sig sig-object py" id="os.uname">
  936. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">uname</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.uname" title="Link to this definition">¶</a></dt>
  937. <dd><p id="index-20">Returns information identifying the current operating system.
  938. The return value is an object with five attributes:</p>
  939. <ul class="simple">
  940. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">sysname</span></code> - operating system name</p></li>
  941. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">nodename</span></code> - name of machine on network (implementation-defined)</p></li>
  942. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">release</span></code> - operating system release</p></li>
  943. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">version</span></code> - operating system version</p></li>
  944. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">machine</span></code> - hardware identifier</p></li>
  945. </ul>
  946. <p>For backwards compatibility, this object is also iterable, behaving
  947. like a five-tuple containing <code class="xref py py-attr docutils literal notranslate"><span class="pre">sysname</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">nodename</span></code>,
  948. <code class="xref py py-attr docutils literal notranslate"><span class="pre">release</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">version</span></code>, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">machine</span></code>
  949. in that order.</p>
  950. <p>Some systems truncate <code class="xref py py-attr docutils literal notranslate"><span class="pre">nodename</span></code> to 8 characters or to the
  951. leading component; a better way to get the hostname is
  952. <a class="reference internal" href="socket.html#socket.gethostname" title="socket.gethostname"><code class="xref py py-func docutils literal notranslate"><span class="pre">socket.gethostname()</span></code></a> or even
  953. <code class="docutils literal notranslate"><span class="pre">socket.gethostbyaddr(socket.gethostname())</span></code>.</p>
  954. <div class="availability docutils container">
  955. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  956. </div>
  957. <div class="versionchanged">
  958. <p><span class="versionmodified changed">Changed in version 3.3: </span>Return type changed from a tuple to a tuple-like object
  959. with named attributes.</p>
  960. </div>
  961. </dd></dl>
  962. <dl class="py function">
  963. <dt class="sig sig-object py" id="os.unsetenv">
  964. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">unsetenv</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</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="#os.unsetenv" title="Link to this definition">¶</a></dt>
  965. <dd><p id="index-21">Unset (delete) the environment variable named <em>key</em>. Such changes to the
  966. environment affect subprocesses started with <a class="reference internal" href="#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a>, <a class="reference internal" href="#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">popen()</span></code></a> or
  967. <a class="reference internal" href="#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">fork()</span></code></a> and <a class="reference internal" href="#os.execv" title="os.execv"><code class="xref py py-func docutils literal notranslate"><span class="pre">execv()</span></code></a>.</p>
  968. <p>Deletion of items in <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> is automatically translated into a
  969. corresponding call to <a class="reference internal" href="#os.unsetenv" title="os.unsetenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">unsetenv()</span></code></a>; however, calls to <a class="reference internal" href="#os.unsetenv" title="os.unsetenv"><code class="xref py py-func docutils literal notranslate"><span class="pre">unsetenv()</span></code></a>
  970. don’t update <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>, so it is actually preferable to delete items of
  971. <a class="reference internal" href="#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>.</p>
  972. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.unsetenv</span></code> with argument <code class="docutils literal notranslate"><span class="pre">key</span></code>.</p>
  973. <div class="versionchanged">
  974. <p><span class="versionmodified changed">Changed in version 3.9: </span>The function is now always available and is also available on Windows.</p>
  975. </div>
  976. </dd></dl>
  977. <dl class="py function">
  978. <dt class="sig sig-object py" id="os.unshare">
  979. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">unshare</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.unshare" title="Link to this definition">¶</a></dt>
  980. <dd><p>Disassociate parts of the process execution context, and move them into a
  981. newly created namespace.
  982. See the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/unshare(2)">unshare(2)</a></em>
  983. man page for more details.
  984. The <em>flags</em> argument is a bit mask, combining zero or more of the
  985. <a class="reference internal" href="#os-unshare-clone-flags"><span class="std std-ref">CLONE_* constants</span></a>,
  986. that specifies which parts of the execution context should be
  987. unshared from their existing associations and moved to a new namespace.
  988. If the <em>flags</em> argument is <code class="docutils literal notranslate"><span class="pre">0</span></code>, no changes are made to the calling process’s
  989. execution context.</p>
  990. <div class="availability docutils container">
  991. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.16.</p>
  992. </div>
  993. <div class="versionadded">
  994. <p><span class="versionmodified added">New in version 3.12.</span></p>
  995. </div>
  996. <div class="admonition seealso">
  997. <p class="admonition-title">See also</p>
  998. <p>The <a class="reference internal" href="#os.setns" title="os.setns"><code class="xref py py-func docutils literal notranslate"><span class="pre">setns()</span></code></a> function.</p>
  999. </div>
  1000. </dd></dl>
  1001. <p id="os-unshare-clone-flags">Flags to the <a class="reference internal" href="#os.unshare" title="os.unshare"><code class="xref py py-func docutils literal notranslate"><span class="pre">unshare()</span></code></a> function, if the implementation supports them.
  1002. See <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/unshare(2)">unshare(2)</a></em> in the Linux manual
  1003. for their exact effect and availability.</p>
  1004. <dl class="py data">
  1005. <dt class="sig sig-object py" id="os.CLONE_FILES">
  1006. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_FILES</span></span><a class="headerlink" href="#os.CLONE_FILES" title="Link to this definition">¶</a></dt>
  1007. <dt class="sig sig-object py" id="os.CLONE_FS">
  1008. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_FS</span></span><a class="headerlink" href="#os.CLONE_FS" title="Link to this definition">¶</a></dt>
  1009. <dt class="sig sig-object py" id="os.CLONE_NEWCGROUP">
  1010. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_NEWCGROUP</span></span><a class="headerlink" href="#os.CLONE_NEWCGROUP" title="Link to this definition">¶</a></dt>
  1011. <dt class="sig sig-object py" id="os.CLONE_NEWIPC">
  1012. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_NEWIPC</span></span><a class="headerlink" href="#os.CLONE_NEWIPC" title="Link to this definition">¶</a></dt>
  1013. <dt class="sig sig-object py" id="os.CLONE_NEWNET">
  1014. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_NEWNET</span></span><a class="headerlink" href="#os.CLONE_NEWNET" title="Link to this definition">¶</a></dt>
  1015. <dt class="sig sig-object py" id="os.CLONE_NEWNS">
  1016. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_NEWNS</span></span><a class="headerlink" href="#os.CLONE_NEWNS" title="Link to this definition">¶</a></dt>
  1017. <dt class="sig sig-object py" id="os.CLONE_NEWPID">
  1018. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_NEWPID</span></span><a class="headerlink" href="#os.CLONE_NEWPID" title="Link to this definition">¶</a></dt>
  1019. <dt class="sig sig-object py" id="os.CLONE_NEWTIME">
  1020. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_NEWTIME</span></span><a class="headerlink" href="#os.CLONE_NEWTIME" title="Link to this definition">¶</a></dt>
  1021. <dt class="sig sig-object py" id="os.CLONE_NEWUSER">
  1022. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_NEWUSER</span></span><a class="headerlink" href="#os.CLONE_NEWUSER" title="Link to this definition">¶</a></dt>
  1023. <dt class="sig sig-object py" id="os.CLONE_NEWUTS">
  1024. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_NEWUTS</span></span><a class="headerlink" href="#os.CLONE_NEWUTS" title="Link to this definition">¶</a></dt>
  1025. <dt class="sig sig-object py" id="os.CLONE_SIGHAND">
  1026. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_SIGHAND</span></span><a class="headerlink" href="#os.CLONE_SIGHAND" title="Link to this definition">¶</a></dt>
  1027. <dt class="sig sig-object py" id="os.CLONE_SYSVSEM">
  1028. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_SYSVSEM</span></span><a class="headerlink" href="#os.CLONE_SYSVSEM" title="Link to this definition">¶</a></dt>
  1029. <dt class="sig sig-object py" id="os.CLONE_THREAD">
  1030. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_THREAD</span></span><a class="headerlink" href="#os.CLONE_THREAD" title="Link to this definition">¶</a></dt>
  1031. <dt class="sig sig-object py" id="os.CLONE_VM">
  1032. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLONE_VM</span></span><a class="headerlink" href="#os.CLONE_VM" title="Link to this definition">¶</a></dt>
  1033. <dd></dd></dl>
  1034. </section>
  1035. <section id="file-object-creation">
  1036. <span id="os-newstreams"></span><h2>File Object Creation<a class="headerlink" href="#file-object-creation" title="Link to this heading">¶</a></h2>
  1037. <p>These functions create new <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a>. (See also
  1038. <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> for opening file descriptors.)</p>
  1039. <dl class="py function">
  1040. <dt class="sig sig-object py" id="os.fdopen">
  1041. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fdopen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.fdopen" title="Link to this definition">¶</a></dt>
  1042. <dd><p>Return an open file object connected to the file descriptor <em>fd</em>. This is an
  1043. alias of the <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> built-in function and accepts the same arguments.
  1044. The only difference is that the first argument of <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">fdopen()</span></code></a> must always
  1045. be an integer.</p>
  1046. </dd></dl>
  1047. </section>
  1048. <section id="file-descriptor-operations">
  1049. <span id="os-fd-ops"></span><h2>File Descriptor Operations<a class="headerlink" href="#file-descriptor-operations" title="Link to this heading">¶</a></h2>
  1050. <p>These functions operate on I/O streams referenced using file descriptors.</p>
  1051. <p>File descriptors are small integers corresponding to a file that has been opened
  1052. by the current process. For example, standard input is usually file descriptor
  1053. 0, standard output is 1, and standard error is 2. Further files opened by a
  1054. process will then be assigned 3, 4, 5, and so forth. The name “file descriptor”
  1055. is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
  1056. by file descriptors.</p>
  1057. <p>The <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> method can be used to obtain the file descriptor
  1058. associated with a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> when required. Note that using the file
  1059. descriptor directly will bypass the file object methods, ignoring aspects such
  1060. as internal buffering of data.</p>
  1061. <dl class="py function">
  1062. <dt class="sig sig-object py" id="os.close">
  1063. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.close" title="Link to this definition">¶</a></dt>
  1064. <dd><p>Close file descriptor <em>fd</em>.</p>
  1065. <div class="admonition note">
  1066. <p class="admonition-title">Note</p>
  1067. <p>This function is intended for low-level I/O and must be applied to a file
  1068. descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a> or <a class="reference internal" href="#os.pipe" title="os.pipe"><code class="xref py py-func docutils literal notranslate"><span class="pre">pipe()</span></code></a>. To close a “file
  1069. object” returned by the built-in function <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> or by <a class="reference internal" href="#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">popen()</span></code></a> or
  1070. <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">fdopen()</span></code></a>, use its <a class="reference internal" href="io.html#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method.</p>
  1071. </div>
  1072. </dd></dl>
  1073. <dl class="py function">
  1074. <dt class="sig sig-object py" id="os.closerange">
  1075. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">closerange</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd_low</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fd_high</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="#os.closerange" title="Link to this definition">¶</a></dt>
  1076. <dd><p>Close all file descriptors from <em>fd_low</em> (inclusive) to <em>fd_high</em> (exclusive),
  1077. ignoring errors. Equivalent to (but much faster than):</p>
  1078. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">fd</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">fd_low</span><span class="p">,</span> <span class="n">fd_high</span><span class="p">):</span>
  1079. <span class="k">try</span><span class="p">:</span>
  1080. <span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
  1081. <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
  1082. <span class="k">pass</span>
  1083. </pre></div>
  1084. </div>
  1085. </dd></dl>
  1086. <dl class="py function">
  1087. <dt class="sig sig-object py" id="os.copy_file_range">
  1088. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">copy_file_range</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">count</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset_src</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset_dst</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.copy_file_range" title="Link to this definition">¶</a></dt>
  1089. <dd><p>Copy <em>count</em> bytes from file descriptor <em>src</em>, starting from offset
  1090. <em>offset_src</em>, to file descriptor <em>dst</em>, starting from offset <em>offset_dst</em>.
  1091. If <em>offset_src</em> is None, then <em>src</em> is read from the current position;
  1092. respectively for <em>offset_dst</em>.</p>
  1093. <p>In Linux kernel older than 5.3, the files pointed by <em>src</em> and <em>dst</em>
  1094. must reside in the same filesystem, otherwise an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is
  1095. raised with <a class="reference internal" href="exceptions.html#OSError.errno" title="OSError.errno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errno</span></code></a> set to <a class="reference internal" href="errno.html#errno.EXDEV" title="errno.EXDEV"><code class="xref py py-const docutils literal notranslate"><span class="pre">errno.EXDEV</span></code></a>.</p>
  1096. <p>This copy is done without the additional cost of transferring data
  1097. from the kernel to user space and then back into the kernel. Additionally,
  1098. some filesystems could implement extra optimizations, such as the use of
  1099. reflinks (i.e., two or more inodes that share pointers to the same
  1100. copy-on-write disk blocks; supported file systems include btrfs and XFS)
  1101. and server-side copy (in the case of NFS).</p>
  1102. <p>The function copies bytes between two file descriptors. Text options, like
  1103. the encoding and the line ending, are ignored.</p>
  1104. <p>The return value is the amount of bytes copied. This could be less than the
  1105. amount requested.</p>
  1106. <div class="admonition note">
  1107. <p class="admonition-title">Note</p>
  1108. <p>On Linux, <a class="reference internal" href="#os.copy_file_range" title="os.copy_file_range"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.copy_file_range()</span></code></a> should not be used for copying a
  1109. range of a pseudo file from a special filesystem like procfs and sysfs.
  1110. It will always copy no bytes and return 0 as if the file was empty
  1111. because of a known Linux kernel issue.</p>
  1112. </div>
  1113. <div class="availability docutils container">
  1114. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 4.5 with glibc &gt;= 2.27.</p>
  1115. </div>
  1116. <div class="versionadded">
  1117. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1118. </div>
  1119. </dd></dl>
  1120. <dl class="py function">
  1121. <dt class="sig sig-object py" id="os.device_encoding">
  1122. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">device_encoding</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.device_encoding" title="Link to this definition">¶</a></dt>
  1123. <dd><p>Return a string describing the encoding of the device associated with <em>fd</em>
  1124. if it is connected to a terminal; else return <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
  1125. <p>On Unix, if the <a class="reference internal" href="#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> is enabled, return
  1126. <code class="docutils literal notranslate"><span class="pre">'UTF-8'</span></code> rather than the device encoding.</p>
  1127. <div class="versionchanged">
  1128. <p><span class="versionmodified changed">Changed in version 3.10: </span>On Unix, the function now implements the Python UTF-8 Mode.</p>
  1129. </div>
  1130. </dd></dl>
  1131. <dl class="py function">
  1132. <dt class="sig sig-object py" id="os.dup">
  1133. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">dup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</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="#os.dup" title="Link to this definition">¶</a></dt>
  1134. <dd><p>Return a duplicate of file descriptor <em>fd</em>. The new file descriptor is
  1135. <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p>
  1136. <p>On Windows, when duplicating a standard stream (0: stdin, 1: stdout,
  1137. 2: stderr), the new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">inheritable</span></a>.</p>
  1138. <div class="availability docutils container">
  1139. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not WASI.</p>
  1140. </div>
  1141. <div class="versionchanged">
  1142. <p><span class="versionmodified changed">Changed in version 3.4: </span>The new file descriptor is now non-inheritable.</p>
  1143. </div>
  1144. </dd></dl>
  1145. <dl class="py function">
  1146. <dt class="sig sig-object py" id="os.dup2">
  1147. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">dup2</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fd2</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">inheritable</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.dup2" title="Link to this definition">¶</a></dt>
  1148. <dd><p>Duplicate file descriptor <em>fd</em> to <em>fd2</em>, closing the latter first if
  1149. necessary. Return <em>fd2</em>. The new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">inheritable</span></a> by default or non-inheritable if <em>inheritable</em>
  1150. is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  1151. <div class="availability docutils container">
  1152. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not WASI.</p>
  1153. </div>
  1154. <div class="versionchanged">
  1155. <p><span class="versionmodified changed">Changed in version 3.4: </span>Add the optional <em>inheritable</em> parameter.</p>
  1156. </div>
  1157. <div class="versionchanged">
  1158. <p><span class="versionmodified changed">Changed in version 3.7: </span>Return <em>fd2</em> on success. Previously, <code class="docutils literal notranslate"><span class="pre">None</span></code> was always returned.</p>
  1159. </div>
  1160. </dd></dl>
  1161. <dl class="py function">
  1162. <dt class="sig sig-object py" id="os.fchmod">
  1163. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fchmod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.fchmod" title="Link to this definition">¶</a></dt>
  1164. <dd><p>Change the mode of the file given by <em>fd</em> to the numeric <em>mode</em>. See the
  1165. docs for <a class="reference internal" href="#os.chmod" title="os.chmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">chmod()</span></code></a> for possible values of <em>mode</em>. As of Python 3.3, this
  1166. is equivalent to <code class="docutils literal notranslate"><span class="pre">os.chmod(fd,</span> <span class="pre">mode)</span></code>.</p>
  1167. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chmod</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">mode</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  1168. <div class="availability docutils container">
  1169. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1170. <p>The function is limited on Emscripten and WASI, see
  1171. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  1172. </div>
  1173. </dd></dl>
  1174. <dl class="py function">
  1175. <dt class="sig sig-object py" id="os.fchown">
  1176. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fchown</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gid</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.fchown" title="Link to this definition">¶</a></dt>
  1177. <dd><p>Change the owner and group id of the file given by <em>fd</em> to the numeric <em>uid</em>
  1178. and <em>gid</em>. To leave one of the ids unchanged, set it to -1. See
  1179. <a class="reference internal" href="#os.chown" title="os.chown"><code class="xref py py-func docutils literal notranslate"><span class="pre">chown()</span></code></a>. As of Python 3.3, this is equivalent to <code class="docutils literal notranslate"><span class="pre">os.chown(fd,</span> <span class="pre">uid,</span>
  1180. <span class="pre">gid)</span></code>.</p>
  1181. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chown</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">uid</span></code>, <code class="docutils literal notranslate"><span class="pre">gid</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  1182. <div class="availability docutils container">
  1183. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1184. <p>The function is limited on Emscripten and WASI, see
  1185. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  1186. </div>
  1187. </dd></dl>
  1188. <dl class="py function">
  1189. <dt class="sig sig-object py" id="os.fdatasync">
  1190. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fdatasync</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.fdatasync" title="Link to this definition">¶</a></dt>
  1191. <dd><p>Force write of file with filedescriptor <em>fd</em> to disk. Does not force update of
  1192. metadata.</p>
  1193. <div class="availability docutils container">
  1194. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1195. </div>
  1196. <div class="admonition note">
  1197. <p class="admonition-title">Note</p>
  1198. <p>This function is not available on MacOS.</p>
  1199. </div>
  1200. </dd></dl>
  1201. <dl class="py function">
  1202. <dt class="sig sig-object py" id="os.fpathconf">
  1203. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fpathconf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</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="#os.fpathconf" title="Link to this definition">¶</a></dt>
  1204. <dd><p>Return system configuration information relevant to an open file. <em>name</em>
  1205. specifies the configuration value to retrieve; it may be a string which is the
  1206. name of a defined system value; these names are specified in a number of
  1207. standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
  1208. additional names as well. The names known to the host operating system are
  1209. given in the <code class="docutils literal notranslate"><span class="pre">pathconf_names</span></code> dictionary. For configuration variables not
  1210. included in that mapping, passing an integer for <em>name</em> is also accepted.</p>
  1211. <p>If <em>name</em> is a string and is not known, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. If a
  1212. specific value for <em>name</em> is not supported by the host system, even if it is
  1213. included in <code class="docutils literal notranslate"><span class="pre">pathconf_names</span></code>, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised with
  1214. <a class="reference internal" href="errno.html#errno.EINVAL" title="errno.EINVAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">errno.EINVAL</span></code></a> for the error number.</p>
  1215. <p>As of Python 3.3, this is equivalent to <code class="docutils literal notranslate"><span class="pre">os.pathconf(fd,</span> <span class="pre">name)</span></code>.</p>
  1216. <div class="availability docutils container">
  1217. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1218. </div>
  1219. </dd></dl>
  1220. <dl class="py function">
  1221. <dt class="sig sig-object py" id="os.fstat">
  1222. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fstat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.fstat" title="Link to this definition">¶</a></dt>
  1223. <dd><p>Get the status of the file descriptor <em>fd</em>. Return a <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code class="xref py py-class docutils literal notranslate"><span class="pre">stat_result</span></code></a>
  1224. object.</p>
  1225. <p>As of Python 3.3, this is equivalent to <code class="docutils literal notranslate"><span class="pre">os.stat(fd)</span></code>.</p>
  1226. <div class="admonition seealso">
  1227. <p class="admonition-title">See also</p>
  1228. <p>The <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a> function.</p>
  1229. </div>
  1230. </dd></dl>
  1231. <dl class="py function">
  1232. <dt class="sig sig-object py" id="os.fstatvfs">
  1233. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fstatvfs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</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="#os.fstatvfs" title="Link to this definition">¶</a></dt>
  1234. <dd><p>Return information about the filesystem containing the file associated with
  1235. file descriptor <em>fd</em>, like <a class="reference internal" href="#os.statvfs" title="os.statvfs"><code class="xref py py-func docutils literal notranslate"><span class="pre">statvfs()</span></code></a>. As of Python 3.3, this is
  1236. equivalent to <code class="docutils literal notranslate"><span class="pre">os.statvfs(fd)</span></code>.</p>
  1237. <div class="availability docutils container">
  1238. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1239. </div>
  1240. </dd></dl>
  1241. <dl class="py function">
  1242. <dt class="sig sig-object py" id="os.fsync">
  1243. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fsync</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.fsync" title="Link to this definition">¶</a></dt>
  1244. <dd><p>Force write of file with filedescriptor <em>fd</em> to disk. On Unix, this calls the
  1245. native <code class="xref c c-func docutils literal notranslate"><span class="pre">fsync()</span></code> function; on Windows, the MS <code class="xref c c-func docutils literal notranslate"><span class="pre">_commit()</span></code> function.</p>
  1246. <p>If you’re starting with a buffered Python <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>f</em>, first do
  1247. <code class="docutils literal notranslate"><span class="pre">f.flush()</span></code>, and then do <code class="docutils literal notranslate"><span class="pre">os.fsync(f.fileno())</span></code>, to ensure that all internal
  1248. buffers associated with <em>f</em> are written to disk.</p>
  1249. <div class="availability docutils container">
  1250. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  1251. </div>
  1252. </dd></dl>
  1253. <dl class="py function">
  1254. <dt class="sig sig-object py" id="os.ftruncate">
  1255. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">ftruncate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">length</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="#os.ftruncate" title="Link to this definition">¶</a></dt>
  1256. <dd><p>Truncate the file corresponding to file descriptor <em>fd</em>, so that it is at
  1257. most <em>length</em> bytes in size. As of Python 3.3, this is equivalent to
  1258. <code class="docutils literal notranslate"><span class="pre">os.truncate(fd,</span> <span class="pre">length)</span></code>.</p>
  1259. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.truncate</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">fd</span></code>, <code class="docutils literal notranslate"><span class="pre">length</span></code>.</p>
  1260. <div class="availability docutils container">
  1261. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  1262. </div>
  1263. <div class="versionchanged">
  1264. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for Windows</p>
  1265. </div>
  1266. </dd></dl>
  1267. <dl class="py function">
  1268. <dt class="sig sig-object py" id="os.get_blocking">
  1269. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">get_blocking</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</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="#os.get_blocking" title="Link to this definition">¶</a></dt>
  1270. <dd><p>Get the blocking mode of the file descriptor: <code class="docutils literal notranslate"><span class="pre">False</span></code> if the
  1271. <a class="reference internal" href="#os.O_NONBLOCK" title="os.O_NONBLOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_NONBLOCK</span></code></a> flag is set, <code class="docutils literal notranslate"><span class="pre">True</span></code> if the flag is cleared.</p>
  1272. <p>See also <a class="reference internal" href="#os.set_blocking" title="os.set_blocking"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_blocking()</span></code></a> and <a class="reference internal" href="socket.html#socket.socket.setblocking" title="socket.socket.setblocking"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.socket.setblocking()</span></code></a>.</p>
  1273. <div class="availability docutils container">
  1274. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  1275. <p>The function is limited on Emscripten and WASI, see
  1276. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  1277. <p>On Windows, this function is limited to pipes.</p>
  1278. </div>
  1279. <div class="versionadded">
  1280. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1281. </div>
  1282. <div class="versionchanged">
  1283. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added support for pipes on Windows.</p>
  1284. </div>
  1285. </dd></dl>
  1286. <dl class="py function">
  1287. <dt class="sig sig-object py" id="os.isatty">
  1288. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">isatty</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</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="#os.isatty" title="Link to this definition">¶</a></dt>
  1289. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the file descriptor <em>fd</em> is open and connected to a
  1290. tty(-like) device, else <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  1291. </dd></dl>
  1292. <dl class="py function">
  1293. <dt class="sig sig-object py" id="os.lockf">
  1294. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">lockf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">len</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="#os.lockf" title="Link to this definition">¶</a></dt>
  1295. <dd><p>Apply, test or remove a POSIX lock on an open file descriptor.
  1296. <em>fd</em> is an open file descriptor.
  1297. <em>cmd</em> specifies the command to use - one of <a class="reference internal" href="#os.F_LOCK" title="os.F_LOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">F_LOCK</span></code></a>, <a class="reference internal" href="#os.F_TLOCK" title="os.F_TLOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">F_TLOCK</span></code></a>,
  1298. <a class="reference internal" href="#os.F_ULOCK" title="os.F_ULOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">F_ULOCK</span></code></a> or <a class="reference internal" href="#os.F_TEST" title="os.F_TEST"><code class="xref py py-data docutils literal notranslate"><span class="pre">F_TEST</span></code></a>.
  1299. <em>len</em> specifies the section of the file to lock.</p>
  1300. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.lockf</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">fd</span></code>, <code class="docutils literal notranslate"><span class="pre">cmd</span></code>, <code class="docutils literal notranslate"><span class="pre">len</span></code>.</p>
  1301. <div class="availability docutils container">
  1302. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1303. </div>
  1304. <div class="versionadded">
  1305. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1306. </div>
  1307. </dd></dl>
  1308. <dl class="py data">
  1309. <dt class="sig sig-object py" id="os.F_LOCK">
  1310. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">F_LOCK</span></span><a class="headerlink" href="#os.F_LOCK" title="Link to this definition">¶</a></dt>
  1311. <dt class="sig sig-object py" id="os.F_TLOCK">
  1312. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">F_TLOCK</span></span><a class="headerlink" href="#os.F_TLOCK" title="Link to this definition">¶</a></dt>
  1313. <dt class="sig sig-object py" id="os.F_ULOCK">
  1314. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">F_ULOCK</span></span><a class="headerlink" href="#os.F_ULOCK" title="Link to this definition">¶</a></dt>
  1315. <dt class="sig sig-object py" id="os.F_TEST">
  1316. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">F_TEST</span></span><a class="headerlink" href="#os.F_TEST" title="Link to this definition">¶</a></dt>
  1317. <dd><p>Flags that specify what action <a class="reference internal" href="#os.lockf" title="os.lockf"><code class="xref py py-func docutils literal notranslate"><span class="pre">lockf()</span></code></a> will take.</p>
  1318. <div class="availability docutils container">
  1319. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1320. </div>
  1321. <div class="versionadded">
  1322. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1323. </div>
  1324. </dd></dl>
  1325. <dl class="py function">
  1326. <dt class="sig sig-object py" id="os.login_tty">
  1327. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">login_tty</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</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="#os.login_tty" title="Link to this definition">¶</a></dt>
  1328. <dd><p>Prepare the tty of which fd is a file descriptor for a new login session.
  1329. Make the calling process a session leader; make the tty the controlling tty,
  1330. the stdin, the stdout, and the stderr of the calling process; close fd.</p>
  1331. <div class="availability docutils container">
  1332. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  1333. </div>
  1334. <div class="versionadded">
  1335. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1336. </div>
  1337. </dd></dl>
  1338. <dl class="py function">
  1339. <dt class="sig sig-object py" id="os.lseek">
  1340. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">lseek</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pos</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">whence</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="#os.lseek" title="Link to this definition">¶</a></dt>
  1341. <dd><p>Set the current position of file descriptor <em>fd</em> to position <em>pos</em>, modified
  1342. by <em>whence</em>, and return the new position in bytes relative to
  1343. the start of the file.
  1344. Valid values for <em>whence</em> are:</p>
  1345. <ul class="simple">
  1346. <li><p><a class="reference internal" href="#os.SEEK_SET" title="os.SEEK_SET"><code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_SET</span></code></a> or <code class="docutils literal notranslate"><span class="pre">0</span></code> – set <em>pos</em> relative to the beginning of the file</p></li>
  1347. <li><p><a class="reference internal" href="#os.SEEK_CUR" title="os.SEEK_CUR"><code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_CUR</span></code></a> or <code class="docutils literal notranslate"><span class="pre">1</span></code> – set <em>pos</em> relative to the current file position</p></li>
  1348. <li><p><a class="reference internal" href="#os.SEEK_END" title="os.SEEK_END"><code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_END</span></code></a> or <code class="docutils literal notranslate"><span class="pre">2</span></code> – set <em>pos</em> relative to the end of the file</p></li>
  1349. <li><p><a class="reference internal" href="#os.SEEK_HOLE" title="os.SEEK_HOLE"><code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_HOLE</span></code></a> – set <em>pos</em> to the next data location, relative to <em>pos</em></p></li>
  1350. <li><p><a class="reference internal" href="#os.SEEK_DATA" title="os.SEEK_DATA"><code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_DATA</span></code></a> – set <em>pos</em> to the next data hole, relative to <em>pos</em></p></li>
  1351. </ul>
  1352. <div class="versionchanged">
  1353. <p><span class="versionmodified changed">Changed in version 3.3: </span>Add support for <code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_HOLE</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_DATA</span></code>.</p>
  1354. </div>
  1355. </dd></dl>
  1356. <dl class="py data">
  1357. <dt class="sig sig-object py" id="os.SEEK_SET">
  1358. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SEEK_SET</span></span><a class="headerlink" href="#os.SEEK_SET" title="Link to this definition">¶</a></dt>
  1359. <dt class="sig sig-object py" id="os.SEEK_CUR">
  1360. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SEEK_CUR</span></span><a class="headerlink" href="#os.SEEK_CUR" title="Link to this definition">¶</a></dt>
  1361. <dt class="sig sig-object py" id="os.SEEK_END">
  1362. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SEEK_END</span></span><a class="headerlink" href="#os.SEEK_END" title="Link to this definition">¶</a></dt>
  1363. <dd><p>Parameters to the <a class="reference internal" href="#os.lseek" title="os.lseek"><code class="xref py py-func docutils literal notranslate"><span class="pre">lseek()</span></code></a> function and the <a class="reference internal" href="io.html#io.IOBase.seek" title="io.IOBase.seek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code></a>
  1364. method on <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file-like objects</span></a>,
  1365. for whence to adjust the file position indicator.</p>
  1366. <dl class="simple">
  1367. <dt><a class="reference internal" href="#os.SEEK_SET" title="os.SEEK_SET"><code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_SET</span></code></a></dt><dd><p>Adjust the file position relative to the beginning of the file.</p>
  1368. </dd>
  1369. <dt><a class="reference internal" href="#os.SEEK_CUR" title="os.SEEK_CUR"><code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_CUR</span></code></a></dt><dd><p>Adjust the file position relative to the current file position.</p>
  1370. </dd>
  1371. <dt><a class="reference internal" href="#os.SEEK_END" title="os.SEEK_END"><code class="xref py py-const docutils literal notranslate"><span class="pre">SEEK_END</span></code></a></dt><dd><p>Adjust the file position relative to the end of the file.</p>
  1372. </dd>
  1373. </dl>
  1374. <p>Their values are 0, 1, and 2, respectively.</p>
  1375. </dd></dl>
  1376. <dl class="py data">
  1377. <dt class="sig sig-object py" id="os.SEEK_HOLE">
  1378. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SEEK_HOLE</span></span><a class="headerlink" href="#os.SEEK_HOLE" title="Link to this definition">¶</a></dt>
  1379. <dt class="sig sig-object py" id="os.SEEK_DATA">
  1380. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SEEK_DATA</span></span><a class="headerlink" href="#os.SEEK_DATA" title="Link to this definition">¶</a></dt>
  1381. <dd><p>Parameters to the <a class="reference internal" href="#os.lseek" title="os.lseek"><code class="xref py py-func docutils literal notranslate"><span class="pre">lseek()</span></code></a> function and the <a class="reference internal" href="io.html#io.IOBase.seek" title="io.IOBase.seek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code></a>
  1382. method on <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file-like objects</span></a>,
  1383. for seeking file data and holes on sparsely allocated files.</p>
  1384. <dl class="simple">
  1385. <dt><code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_DATA</span></code></dt><dd><p>Adjust the file offset to the next location containing data,
  1386. relative to the seek position.</p>
  1387. </dd>
  1388. <dt><code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_HOLE</span></code></dt><dd><p>Adjust the file offset to the next location containing a hole,
  1389. relative to the seek position.
  1390. A hole is defined as a sequence of zeros.</p>
  1391. </dd>
  1392. </dl>
  1393. <div class="admonition note">
  1394. <p class="admonition-title">Note</p>
  1395. <p>These operations only make sense for filesystems that support them.</p>
  1396. </div>
  1397. <div class="availability docutils container">
  1398. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 3.1, macOS, Unix</p>
  1399. </div>
  1400. <div class="versionadded">
  1401. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1402. </div>
  1403. </dd></dl>
  1404. <dl class="py function">
  1405. <dt class="sig sig-object py" id="os.open">
  1406. <span class="sig-prename descclassname"><span class="pre">os.</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">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</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">0o777</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.open" title="Link to this definition">¶</a></dt>
  1407. <dd><p>Open the file <em>path</em> and set various flags according to <em>flags</em> and possibly
  1408. its mode according to <em>mode</em>. When computing <em>mode</em>, the current umask value
  1409. is first masked out. Return the file descriptor for the newly opened file.
  1410. The new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p>
  1411. <p>For a description of the flag and mode values, see the C run-time documentation;
  1412. flag constants (like <a class="reference internal" href="#os.O_RDONLY" title="os.O_RDONLY"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_RDONLY</span></code></a> and <a class="reference internal" href="#os.O_WRONLY" title="os.O_WRONLY"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_WRONLY</span></code></a>) are defined in
  1413. the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module. In particular, on Windows adding
  1414. <a class="reference internal" href="#os.O_BINARY" title="os.O_BINARY"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_BINARY</span></code></a> is needed to open files in binary mode.</p>
  1415. <p>This function can support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> with the <em>dir_fd</em> parameter.</p>
  1416. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">open</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">mode</span></code>, <code class="docutils literal notranslate"><span class="pre">flags</span></code>.</p>
  1417. <div class="versionchanged">
  1418. <p><span class="versionmodified changed">Changed in version 3.4: </span>The new file descriptor is now non-inheritable.</p>
  1419. </div>
  1420. <div class="admonition note">
  1421. <p class="admonition-title">Note</p>
  1422. <p>This function is intended for low-level I/O. For normal usage, use the
  1423. built-in function <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>, which returns a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> with
  1424. <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> methods (and many more). To
  1425. wrap a file descriptor in a file object, use <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">fdopen()</span></code></a>.</p>
  1426. </div>
  1427. <div class="versionchanged">
  1428. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p>
  1429. </div>
  1430. <div class="versionchanged">
  1431. <p><span class="versionmodified changed">Changed in version 3.5: </span>If the system call is interrupted and the signal handler does not raise an
  1432. exception, the function now retries the system call instead of raising an
  1433. <a class="reference internal" href="exceptions.html#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a> exception (see <span class="target" id="index-22"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p>
  1434. </div>
  1435. <div class="versionchanged">
  1436. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  1437. </div>
  1438. </dd></dl>
  1439. <p>The following constants are options for the <em>flags</em> parameter to the
  1440. <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function. They can be combined using the bitwise OR operator
  1441. <code class="docutils literal notranslate"><span class="pre">|</span></code>. Some of them are not available on all platforms. For descriptions of
  1442. their availability and use, consult the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/open(2)">open(2)</a></em> manual page on Unix
  1443. or <a class="reference external" href="https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx">the MSDN</a> on Windows.</p>
  1444. <dl class="py data">
  1445. <dt class="sig sig-object py" id="os.O_RDONLY">
  1446. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_RDONLY</span></span><a class="headerlink" href="#os.O_RDONLY" title="Link to this definition">¶</a></dt>
  1447. <dt class="sig sig-object py" id="os.O_WRONLY">
  1448. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_WRONLY</span></span><a class="headerlink" href="#os.O_WRONLY" title="Link to this definition">¶</a></dt>
  1449. <dt class="sig sig-object py" id="os.O_RDWR">
  1450. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_RDWR</span></span><a class="headerlink" href="#os.O_RDWR" title="Link to this definition">¶</a></dt>
  1451. <dt class="sig sig-object py" id="os.O_APPEND">
  1452. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_APPEND</span></span><a class="headerlink" href="#os.O_APPEND" title="Link to this definition">¶</a></dt>
  1453. <dt class="sig sig-object py" id="os.O_CREAT">
  1454. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_CREAT</span></span><a class="headerlink" href="#os.O_CREAT" title="Link to this definition">¶</a></dt>
  1455. <dt class="sig sig-object py" id="os.O_EXCL">
  1456. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_EXCL</span></span><a class="headerlink" href="#os.O_EXCL" title="Link to this definition">¶</a></dt>
  1457. <dt class="sig sig-object py" id="os.O_TRUNC">
  1458. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_TRUNC</span></span><a class="headerlink" href="#os.O_TRUNC" title="Link to this definition">¶</a></dt>
  1459. <dd><p>The above constants are available on Unix and Windows.</p>
  1460. </dd></dl>
  1461. <dl class="py data">
  1462. <dt class="sig sig-object py" id="os.O_DSYNC">
  1463. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_DSYNC</span></span><a class="headerlink" href="#os.O_DSYNC" title="Link to this definition">¶</a></dt>
  1464. <dt class="sig sig-object py" id="os.O_RSYNC">
  1465. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_RSYNC</span></span><a class="headerlink" href="#os.O_RSYNC" title="Link to this definition">¶</a></dt>
  1466. <dt class="sig sig-object py" id="os.O_SYNC">
  1467. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_SYNC</span></span><a class="headerlink" href="#os.O_SYNC" title="Link to this definition">¶</a></dt>
  1468. <dt class="sig sig-object py" id="os.O_NDELAY">
  1469. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_NDELAY</span></span><a class="headerlink" href="#os.O_NDELAY" title="Link to this definition">¶</a></dt>
  1470. <dt class="sig sig-object py" id="os.O_NONBLOCK">
  1471. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_NONBLOCK</span></span><a class="headerlink" href="#os.O_NONBLOCK" title="Link to this definition">¶</a></dt>
  1472. <dt class="sig sig-object py" id="os.O_NOCTTY">
  1473. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_NOCTTY</span></span><a class="headerlink" href="#os.O_NOCTTY" title="Link to this definition">¶</a></dt>
  1474. <dt class="sig sig-object py" id="os.O_CLOEXEC">
  1475. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_CLOEXEC</span></span><a class="headerlink" href="#os.O_CLOEXEC" title="Link to this definition">¶</a></dt>
  1476. <dd><p>The above constants are only available on Unix.</p>
  1477. <div class="versionchanged">
  1478. <p><span class="versionmodified changed">Changed in version 3.3: </span>Add <a class="reference internal" href="#os.O_CLOEXEC" title="os.O_CLOEXEC"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_CLOEXEC</span></code></a> constant.</p>
  1479. </div>
  1480. </dd></dl>
  1481. <dl class="py data">
  1482. <dt class="sig sig-object py" id="os.O_BINARY">
  1483. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_BINARY</span></span><a class="headerlink" href="#os.O_BINARY" title="Link to this definition">¶</a></dt>
  1484. <dt class="sig sig-object py" id="os.O_NOINHERIT">
  1485. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_NOINHERIT</span></span><a class="headerlink" href="#os.O_NOINHERIT" title="Link to this definition">¶</a></dt>
  1486. <dt class="sig sig-object py" id="os.O_SHORT_LIVED">
  1487. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_SHORT_LIVED</span></span><a class="headerlink" href="#os.O_SHORT_LIVED" title="Link to this definition">¶</a></dt>
  1488. <dt class="sig sig-object py" id="os.O_TEMPORARY">
  1489. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_TEMPORARY</span></span><a class="headerlink" href="#os.O_TEMPORARY" title="Link to this definition">¶</a></dt>
  1490. <dt class="sig sig-object py" id="os.O_RANDOM">
  1491. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_RANDOM</span></span><a class="headerlink" href="#os.O_RANDOM" title="Link to this definition">¶</a></dt>
  1492. <dt class="sig sig-object py" id="os.O_SEQUENTIAL">
  1493. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_SEQUENTIAL</span></span><a class="headerlink" href="#os.O_SEQUENTIAL" title="Link to this definition">¶</a></dt>
  1494. <dt class="sig sig-object py" id="os.O_TEXT">
  1495. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_TEXT</span></span><a class="headerlink" href="#os.O_TEXT" title="Link to this definition">¶</a></dt>
  1496. <dd><p>The above constants are only available on Windows.</p>
  1497. </dd></dl>
  1498. <dl class="py data">
  1499. <dt class="sig sig-object py" id="os.O_EVTONLY">
  1500. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_EVTONLY</span></span><a class="headerlink" href="#os.O_EVTONLY" title="Link to this definition">¶</a></dt>
  1501. <dt class="sig sig-object py" id="os.O_FSYNC">
  1502. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_FSYNC</span></span><a class="headerlink" href="#os.O_FSYNC" title="Link to this definition">¶</a></dt>
  1503. <dt class="sig sig-object py" id="os.O_SYMLINK">
  1504. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_SYMLINK</span></span><a class="headerlink" href="#os.O_SYMLINK" title="Link to this definition">¶</a></dt>
  1505. <dt class="sig sig-object py" id="os.O_NOFOLLOW_ANY">
  1506. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_NOFOLLOW_ANY</span></span><a class="headerlink" href="#os.O_NOFOLLOW_ANY" title="Link to this definition">¶</a></dt>
  1507. <dd><p>The above constants are only available on macOS.</p>
  1508. <div class="versionchanged">
  1509. <p><span class="versionmodified changed">Changed in version 3.10: </span>Add <a class="reference internal" href="#os.O_EVTONLY" title="os.O_EVTONLY"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_EVTONLY</span></code></a>, <a class="reference internal" href="#os.O_FSYNC" title="os.O_FSYNC"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_FSYNC</span></code></a>, <a class="reference internal" href="#os.O_SYMLINK" title="os.O_SYMLINK"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_SYMLINK</span></code></a>
  1510. and <a class="reference internal" href="#os.O_NOFOLLOW_ANY" title="os.O_NOFOLLOW_ANY"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_NOFOLLOW_ANY</span></code></a> constants.</p>
  1511. </div>
  1512. </dd></dl>
  1513. <dl class="py data">
  1514. <dt class="sig sig-object py" id="os.O_ASYNC">
  1515. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_ASYNC</span></span><a class="headerlink" href="#os.O_ASYNC" title="Link to this definition">¶</a></dt>
  1516. <dt class="sig sig-object py" id="os.O_DIRECT">
  1517. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_DIRECT</span></span><a class="headerlink" href="#os.O_DIRECT" title="Link to this definition">¶</a></dt>
  1518. <dt class="sig sig-object py" id="os.O_DIRECTORY">
  1519. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_DIRECTORY</span></span><a class="headerlink" href="#os.O_DIRECTORY" title="Link to this definition">¶</a></dt>
  1520. <dt class="sig sig-object py" id="os.O_NOFOLLOW">
  1521. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_NOFOLLOW</span></span><a class="headerlink" href="#os.O_NOFOLLOW" title="Link to this definition">¶</a></dt>
  1522. <dt class="sig sig-object py" id="os.O_NOATIME">
  1523. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_NOATIME</span></span><a class="headerlink" href="#os.O_NOATIME" title="Link to this definition">¶</a></dt>
  1524. <dt class="sig sig-object py" id="os.O_PATH">
  1525. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_PATH</span></span><a class="headerlink" href="#os.O_PATH" title="Link to this definition">¶</a></dt>
  1526. <dt class="sig sig-object py" id="os.O_TMPFILE">
  1527. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_TMPFILE</span></span><a class="headerlink" href="#os.O_TMPFILE" title="Link to this definition">¶</a></dt>
  1528. <dt class="sig sig-object py" id="os.O_SHLOCK">
  1529. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_SHLOCK</span></span><a class="headerlink" href="#os.O_SHLOCK" title="Link to this definition">¶</a></dt>
  1530. <dt class="sig sig-object py" id="os.O_EXLOCK">
  1531. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">O_EXLOCK</span></span><a class="headerlink" href="#os.O_EXLOCK" title="Link to this definition">¶</a></dt>
  1532. <dd><p>The above constants are extensions and not present if they are not defined by
  1533. the C library.</p>
  1534. <div class="versionchanged">
  1535. <p><span class="versionmodified changed">Changed in version 3.4: </span>Add <a class="reference internal" href="#os.O_PATH" title="os.O_PATH"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_PATH</span></code></a> on systems that support it.
  1536. Add <a class="reference internal" href="#os.O_TMPFILE" title="os.O_TMPFILE"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_TMPFILE</span></code></a>, only available on Linux Kernel 3.11
  1537. or newer.</p>
  1538. </div>
  1539. </dd></dl>
  1540. <dl class="py function">
  1541. <dt class="sig sig-object py" id="os.openpty">
  1542. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">openpty</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.openpty" title="Link to this definition">¶</a></dt>
  1543. <dd><p id="index-23">Open a new pseudo-terminal pair. Return a pair of file descriptors
  1544. <code class="docutils literal notranslate"><span class="pre">(master,</span> <span class="pre">slave)</span></code> for the pty and the tty, respectively. The new file
  1545. descriptors are <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>. For a (slightly) more
  1546. portable approach, use the <a class="reference internal" href="pty.html#module-pty" title="pty: Pseudo-Terminal Handling for Unix. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pty</span></code></a> module.</p>
  1547. <div class="availability docutils container">
  1548. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  1549. </div>
  1550. <div class="versionchanged">
  1551. <p><span class="versionmodified changed">Changed in version 3.4: </span>The new file descriptors are now non-inheritable.</p>
  1552. </div>
  1553. </dd></dl>
  1554. <dl class="py function">
  1555. <dt class="sig sig-object py" id="os.pipe">
  1556. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pipe</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.pipe" title="Link to this definition">¶</a></dt>
  1557. <dd><p>Create a pipe. Return a pair of file descriptors <code class="docutils literal notranslate"><span class="pre">(r,</span> <span class="pre">w)</span></code> usable for
  1558. reading and writing, respectively. The new file descriptor is
  1559. <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p>
  1560. <div class="availability docutils container">
  1561. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  1562. </div>
  1563. <div class="versionchanged">
  1564. <p><span class="versionmodified changed">Changed in version 3.4: </span>The new file descriptors are now non-inheritable.</p>
  1565. </div>
  1566. </dd></dl>
  1567. <dl class="py function">
  1568. <dt class="sig sig-object py" id="os.pipe2">
  1569. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pipe2</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flags</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="#os.pipe2" title="Link to this definition">¶</a></dt>
  1570. <dd><p>Create a pipe with <em>flags</em> set atomically.
  1571. <em>flags</em> can be constructed by ORing together one or more of these values:
  1572. <a class="reference internal" href="#os.O_NONBLOCK" title="os.O_NONBLOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_NONBLOCK</span></code></a>, <a class="reference internal" href="#os.O_CLOEXEC" title="os.O_CLOEXEC"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_CLOEXEC</span></code></a>.
  1573. Return a pair of file descriptors <code class="docutils literal notranslate"><span class="pre">(r,</span> <span class="pre">w)</span></code> usable for reading and writing,
  1574. respectively.</p>
  1575. <div class="availability docutils container">
  1576. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  1577. </div>
  1578. <div class="versionadded">
  1579. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1580. </div>
  1581. </dd></dl>
  1582. <dl class="py function">
  1583. <dt class="sig sig-object py" id="os.posix_fallocate">
  1584. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">posix_fallocate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">len</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="#os.posix_fallocate" title="Link to this definition">¶</a></dt>
  1585. <dd><p>Ensures that enough disk space is allocated for the file specified by <em>fd</em>
  1586. starting from <em>offset</em> and continuing for <em>len</em> bytes.</p>
  1587. <div class="availability docutils container">
  1588. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten.</p>
  1589. </div>
  1590. <div class="versionadded">
  1591. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1592. </div>
  1593. </dd></dl>
  1594. <dl class="py function">
  1595. <dt class="sig sig-object py" id="os.posix_fadvise">
  1596. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">posix_fadvise</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">len</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">advice</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="#os.posix_fadvise" title="Link to this definition">¶</a></dt>
  1597. <dd><p>Announces an intention to access data in a specific pattern thus allowing
  1598. the kernel to make optimizations.
  1599. The advice applies to the region of the file specified by <em>fd</em> starting at
  1600. <em>offset</em> and continuing for <em>len</em> bytes.
  1601. <em>advice</em> is one of <a class="reference internal" href="#os.POSIX_FADV_NORMAL" title="os.POSIX_FADV_NORMAL"><code class="xref py py-data docutils literal notranslate"><span class="pre">POSIX_FADV_NORMAL</span></code></a>, <a class="reference internal" href="#os.POSIX_FADV_SEQUENTIAL" title="os.POSIX_FADV_SEQUENTIAL"><code class="xref py py-data docutils literal notranslate"><span class="pre">POSIX_FADV_SEQUENTIAL</span></code></a>,
  1602. <a class="reference internal" href="#os.POSIX_FADV_RANDOM" title="os.POSIX_FADV_RANDOM"><code class="xref py py-data docutils literal notranslate"><span class="pre">POSIX_FADV_RANDOM</span></code></a>, <a class="reference internal" href="#os.POSIX_FADV_NOREUSE" title="os.POSIX_FADV_NOREUSE"><code class="xref py py-data docutils literal notranslate"><span class="pre">POSIX_FADV_NOREUSE</span></code></a>,
  1603. <a class="reference internal" href="#os.POSIX_FADV_WILLNEED" title="os.POSIX_FADV_WILLNEED"><code class="xref py py-data docutils literal notranslate"><span class="pre">POSIX_FADV_WILLNEED</span></code></a> or <a class="reference internal" href="#os.POSIX_FADV_DONTNEED" title="os.POSIX_FADV_DONTNEED"><code class="xref py py-data docutils literal notranslate"><span class="pre">POSIX_FADV_DONTNEED</span></code></a>.</p>
  1604. <div class="availability docutils container">
  1605. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1606. </div>
  1607. <div class="versionadded">
  1608. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1609. </div>
  1610. </dd></dl>
  1611. <dl class="py data">
  1612. <dt class="sig sig-object py" id="os.POSIX_FADV_NORMAL">
  1613. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">POSIX_FADV_NORMAL</span></span><a class="headerlink" href="#os.POSIX_FADV_NORMAL" title="Link to this definition">¶</a></dt>
  1614. <dt class="sig sig-object py" id="os.POSIX_FADV_SEQUENTIAL">
  1615. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">POSIX_FADV_SEQUENTIAL</span></span><a class="headerlink" href="#os.POSIX_FADV_SEQUENTIAL" title="Link to this definition">¶</a></dt>
  1616. <dt class="sig sig-object py" id="os.POSIX_FADV_RANDOM">
  1617. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">POSIX_FADV_RANDOM</span></span><a class="headerlink" href="#os.POSIX_FADV_RANDOM" title="Link to this definition">¶</a></dt>
  1618. <dt class="sig sig-object py" id="os.POSIX_FADV_NOREUSE">
  1619. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">POSIX_FADV_NOREUSE</span></span><a class="headerlink" href="#os.POSIX_FADV_NOREUSE" title="Link to this definition">¶</a></dt>
  1620. <dt class="sig sig-object py" id="os.POSIX_FADV_WILLNEED">
  1621. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">POSIX_FADV_WILLNEED</span></span><a class="headerlink" href="#os.POSIX_FADV_WILLNEED" title="Link to this definition">¶</a></dt>
  1622. <dt class="sig sig-object py" id="os.POSIX_FADV_DONTNEED">
  1623. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">POSIX_FADV_DONTNEED</span></span><a class="headerlink" href="#os.POSIX_FADV_DONTNEED" title="Link to this definition">¶</a></dt>
  1624. <dd><p>Flags that can be used in <em>advice</em> in <a class="reference internal" href="#os.posix_fadvise" title="os.posix_fadvise"><code class="xref py py-func docutils literal notranslate"><span class="pre">posix_fadvise()</span></code></a> that specify
  1625. the access pattern that is likely to be used.</p>
  1626. <div class="availability docutils container">
  1627. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1628. </div>
  1629. <div class="versionadded">
  1630. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1631. </div>
  1632. </dd></dl>
  1633. <dl class="py function">
  1634. <dt class="sig sig-object py" id="os.pread">
  1635. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pread</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</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="#os.pread" title="Link to this definition">¶</a></dt>
  1636. <dd><p>Read at most <em>n</em> bytes from file descriptor <em>fd</em> at a position of <em>offset</em>,
  1637. leaving the file offset unchanged.</p>
  1638. <p>Return a bytestring containing the bytes read. If the end of the file
  1639. referred to by <em>fd</em> has been reached, an empty bytes object is returned.</p>
  1640. <div class="availability docutils container">
  1641. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1642. </div>
  1643. <div class="versionadded">
  1644. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1645. </div>
  1646. </dd></dl>
  1647. <dl class="py function">
  1648. <dt class="sig sig-object py" id="os.preadv">
  1649. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">preadv</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffers</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.preadv" title="Link to this definition">¶</a></dt>
  1650. <dd><p>Read from a file descriptor <em>fd</em> at a position of <em>offset</em> into mutable
  1651. <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> <em>buffers</em>, leaving the file
  1652. offset unchanged. Transfer data into each buffer until it is full and then
  1653. move on to the next buffer in the sequence to hold the rest of the data.</p>
  1654. <p>The flags argument contains a bitwise OR of zero or more of the following
  1655. flags:</p>
  1656. <ul class="simple">
  1657. <li><p><a class="reference internal" href="#os.RWF_HIPRI" title="os.RWF_HIPRI"><code class="xref py py-data docutils literal notranslate"><span class="pre">RWF_HIPRI</span></code></a></p></li>
  1658. <li><p><a class="reference internal" href="#os.RWF_NOWAIT" title="os.RWF_NOWAIT"><code class="xref py py-data docutils literal notranslate"><span class="pre">RWF_NOWAIT</span></code></a></p></li>
  1659. </ul>
  1660. <p>Return the total number of bytes actually read which can be less than the
  1661. total capacity of all the objects.</p>
  1662. <p>The operating system may set a limit (<a class="reference internal" href="#os.sysconf" title="os.sysconf"><code class="xref py py-func docutils literal notranslate"><span class="pre">sysconf()</span></code></a> value
  1663. <code class="docutils literal notranslate"><span class="pre">'SC_IOV_MAX'</span></code>) on the number of buffers that can be used.</p>
  1664. <p>Combine the functionality of <a class="reference internal" href="#os.readv" title="os.readv"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.readv()</span></code></a> and <a class="reference internal" href="#os.pread" title="os.pread"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.pread()</span></code></a>.</p>
  1665. <div class="availability docutils container">
  1666. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.30, FreeBSD &gt;= 6.0, OpenBSD &gt;= 2.7, AIX &gt;= 7.1.</p>
  1667. <p>Using flags requires Linux &gt;= 4.6.</p>
  1668. </div>
  1669. <div class="versionadded">
  1670. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1671. </div>
  1672. </dd></dl>
  1673. <dl class="py data">
  1674. <dt class="sig sig-object py" id="os.RWF_NOWAIT">
  1675. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RWF_NOWAIT</span></span><a class="headerlink" href="#os.RWF_NOWAIT" title="Link to this definition">¶</a></dt>
  1676. <dd><p>Do not wait for data which is not immediately available. If this flag is
  1677. specified, the system call will return instantly if it would have to read
  1678. data from the backing storage or wait for a lock.</p>
  1679. <p>If some data was successfully read, it will return the number of bytes read.
  1680. If no bytes were read, it will return <code class="docutils literal notranslate"><span class="pre">-1</span></code> and set errno to
  1681. <a class="reference internal" href="errno.html#errno.EAGAIN" title="errno.EAGAIN"><code class="xref py py-const docutils literal notranslate"><span class="pre">errno.EAGAIN</span></code></a>.</p>
  1682. <div class="availability docutils container">
  1683. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 4.14.</p>
  1684. </div>
  1685. <div class="versionadded">
  1686. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1687. </div>
  1688. </dd></dl>
  1689. <dl class="py data">
  1690. <dt class="sig sig-object py" id="os.RWF_HIPRI">
  1691. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RWF_HIPRI</span></span><a class="headerlink" href="#os.RWF_HIPRI" title="Link to this definition">¶</a></dt>
  1692. <dd><p>High priority read/write. Allows block-based filesystems to use polling
  1693. of the device, which provides lower latency, but may use additional
  1694. resources.</p>
  1695. <p>Currently, on Linux, this feature is usable only on a file descriptor opened
  1696. using the <a class="reference internal" href="#os.O_DIRECT" title="os.O_DIRECT"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_DIRECT</span></code></a> flag.</p>
  1697. <div class="availability docutils container">
  1698. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 4.6.</p>
  1699. </div>
  1700. <div class="versionadded">
  1701. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1702. </div>
  1703. </dd></dl>
  1704. <dl class="py function">
  1705. <dt class="sig sig-object py" id="os.pwrite">
  1706. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pwrite</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</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="#os.pwrite" title="Link to this definition">¶</a></dt>
  1707. <dd><p>Write the bytestring in <em>str</em> to file descriptor <em>fd</em> at position of
  1708. <em>offset</em>, leaving the file offset unchanged.</p>
  1709. <p>Return the number of bytes actually written.</p>
  1710. <div class="availability docutils container">
  1711. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1712. </div>
  1713. <div class="versionadded">
  1714. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1715. </div>
  1716. </dd></dl>
  1717. <dl class="py function">
  1718. <dt class="sig sig-object py" id="os.pwritev">
  1719. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pwritev</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffers</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.pwritev" title="Link to this definition">¶</a></dt>
  1720. <dd><p>Write the <em>buffers</em> contents to file descriptor <em>fd</em> at a offset <em>offset</em>,
  1721. leaving the file offset unchanged. <em>buffers</em> must be a sequence of
  1722. <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>. Buffers are processed in
  1723. array order. Entire contents of the first buffer is written before
  1724. proceeding to the second, and so on.</p>
  1725. <p>The flags argument contains a bitwise OR of zero or more of the following
  1726. flags:</p>
  1727. <ul class="simple">
  1728. <li><p><a class="reference internal" href="#os.RWF_DSYNC" title="os.RWF_DSYNC"><code class="xref py py-data docutils literal notranslate"><span class="pre">RWF_DSYNC</span></code></a></p></li>
  1729. <li><p><a class="reference internal" href="#os.RWF_SYNC" title="os.RWF_SYNC"><code class="xref py py-data docutils literal notranslate"><span class="pre">RWF_SYNC</span></code></a></p></li>
  1730. <li><p><a class="reference internal" href="#os.RWF_APPEND" title="os.RWF_APPEND"><code class="xref py py-data docutils literal notranslate"><span class="pre">RWF_APPEND</span></code></a></p></li>
  1731. </ul>
  1732. <p>Return the total number of bytes actually written.</p>
  1733. <p>The operating system may set a limit (<a class="reference internal" href="#os.sysconf" title="os.sysconf"><code class="xref py py-func docutils literal notranslate"><span class="pre">sysconf()</span></code></a> value
  1734. <code class="docutils literal notranslate"><span class="pre">'SC_IOV_MAX'</span></code>) on the number of buffers that can be used.</p>
  1735. <p>Combine the functionality of <a class="reference internal" href="#os.writev" title="os.writev"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.writev()</span></code></a> and <a class="reference internal" href="#os.pwrite" title="os.pwrite"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.pwrite()</span></code></a>.</p>
  1736. <div class="availability docutils container">
  1737. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.30, FreeBSD &gt;= 6.0, OpenBSD &gt;= 2.7, AIX &gt;= 7.1.</p>
  1738. <p>Using flags requires Linux &gt;= 4.6.</p>
  1739. </div>
  1740. <div class="versionadded">
  1741. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1742. </div>
  1743. </dd></dl>
  1744. <dl class="py data">
  1745. <dt class="sig sig-object py" id="os.RWF_DSYNC">
  1746. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RWF_DSYNC</span></span><a class="headerlink" href="#os.RWF_DSYNC" title="Link to this definition">¶</a></dt>
  1747. <dd><p>Provide a per-write equivalent of the <a class="reference internal" href="#os.O_DSYNC" title="os.O_DSYNC"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_DSYNC</span></code></a> <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a> flag.
  1748. This flag effect applies only to the data range written by the system call.</p>
  1749. <div class="availability docutils container">
  1750. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 4.7.</p>
  1751. </div>
  1752. <div class="versionadded">
  1753. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1754. </div>
  1755. </dd></dl>
  1756. <dl class="py data">
  1757. <dt class="sig sig-object py" id="os.RWF_SYNC">
  1758. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RWF_SYNC</span></span><a class="headerlink" href="#os.RWF_SYNC" title="Link to this definition">¶</a></dt>
  1759. <dd><p>Provide a per-write equivalent of the <a class="reference internal" href="#os.O_SYNC" title="os.O_SYNC"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_SYNC</span></code></a> <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a> flag.
  1760. This flag effect applies only to the data range written by the system call.</p>
  1761. <div class="availability docutils container">
  1762. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 4.7.</p>
  1763. </div>
  1764. <div class="versionadded">
  1765. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1766. </div>
  1767. </dd></dl>
  1768. <dl class="py data">
  1769. <dt class="sig sig-object py" id="os.RWF_APPEND">
  1770. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RWF_APPEND</span></span><a class="headerlink" href="#os.RWF_APPEND" title="Link to this definition">¶</a></dt>
  1771. <dd><p>Provide a per-write equivalent of the <a class="reference internal" href="#os.O_APPEND" title="os.O_APPEND"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_APPEND</span></code></a> <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a>
  1772. flag. This flag is meaningful only for <a class="reference internal" href="#os.pwritev" title="os.pwritev"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.pwritev()</span></code></a>, and its
  1773. effect applies only to the data range written by the system call. The
  1774. <em>offset</em> argument does not affect the write operation; the data is always
  1775. appended to the end of the file. However, if the <em>offset</em> argument is
  1776. <code class="docutils literal notranslate"><span class="pre">-1</span></code>, the current file <em>offset</em> is updated.</p>
  1777. <div class="availability docutils container">
  1778. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 4.16.</p>
  1779. </div>
  1780. <div class="versionadded">
  1781. <p><span class="versionmodified added">New in version 3.10.</span></p>
  1782. </div>
  1783. </dd></dl>
  1784. <dl class="py function">
  1785. <dt class="sig sig-object py" id="os.read">
  1786. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</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="#os.read" title="Link to this definition">¶</a></dt>
  1787. <dd><p>Read at most <em>n</em> bytes from file descriptor <em>fd</em>.</p>
  1788. <p>Return a bytestring containing the bytes read. If the end of the file
  1789. referred to by <em>fd</em> has been reached, an empty bytes object is returned.</p>
  1790. <div class="admonition note">
  1791. <p class="admonition-title">Note</p>
  1792. <p>This function is intended for low-level I/O and must be applied to a file
  1793. descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a> or <a class="reference internal" href="#os.pipe" title="os.pipe"><code class="xref py py-func docutils literal notranslate"><span class="pre">pipe()</span></code></a>. To read a
  1794. “file object” returned by the built-in function <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> or by
  1795. <a class="reference internal" href="#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">popen()</span></code></a> or <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">fdopen()</span></code></a>, or <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>, use its
  1796. <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> or <code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code> methods.</p>
  1797. </div>
  1798. <div class="versionchanged">
  1799. <p><span class="versionmodified changed">Changed in version 3.5: </span>If the system call is interrupted and the signal handler does not raise an
  1800. exception, the function now retries the system call instead of raising an
  1801. <a class="reference internal" href="exceptions.html#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a> exception (see <span class="target" id="index-24"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p>
  1802. </div>
  1803. </dd></dl>
  1804. <dl class="py function">
  1805. <dt class="sig sig-object py" id="os.sendfile">
  1806. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sendfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out_fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">in_fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">count</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.sendfile" title="Link to this definition">¶</a></dt>
  1807. <dt class="sig sig-object py">
  1808. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sendfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out_fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">in_fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">count</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">trailers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span></dt>
  1809. <dd><p>Copy <em>count</em> bytes from file descriptor <em>in_fd</em> to file descriptor <em>out_fd</em>
  1810. starting at <em>offset</em>.
  1811. Return the number of bytes sent. When EOF is reached return <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
  1812. <p>The first function notation is supported by all platforms that define
  1813. <a class="reference internal" href="#os.sendfile" title="os.sendfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sendfile()</span></code></a>.</p>
  1814. <p>On Linux, if <em>offset</em> is given as <code class="docutils literal notranslate"><span class="pre">None</span></code>, the bytes are read from the
  1815. current position of <em>in_fd</em> and the position of <em>in_fd</em> is updated.</p>
  1816. <p>The second case may be used on macOS and FreeBSD where <em>headers</em> and
  1817. <em>trailers</em> are arbitrary sequences of buffers that are written before and
  1818. after the data from <em>in_fd</em> is written. It returns the same as the first case.</p>
  1819. <p>On macOS and FreeBSD, a value of <code class="docutils literal notranslate"><span class="pre">0</span></code> for <em>count</em> specifies to send until
  1820. the end of <em>in_fd</em> is reached.</p>
  1821. <p>All platforms support sockets as <em>out_fd</em> file descriptor, and some platforms
  1822. allow other types (e.g. regular file, pipe) as well.</p>
  1823. <p>Cross-platform applications should not use <em>headers</em>, <em>trailers</em> and <em>flags</em>
  1824. arguments.</p>
  1825. <div class="availability docutils container">
  1826. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  1827. </div>
  1828. <div class="admonition note">
  1829. <p class="admonition-title">Note</p>
  1830. <p>For a higher-level wrapper of <a class="reference internal" href="#os.sendfile" title="os.sendfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sendfile()</span></code></a>, see
  1831. <a class="reference internal" href="socket.html#socket.socket.sendfile" title="socket.socket.sendfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.socket.sendfile()</span></code></a>.</p>
  1832. </div>
  1833. <div class="versionadded">
  1834. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1835. </div>
  1836. <div class="versionchanged">
  1837. <p><span class="versionmodified changed">Changed in version 3.9: </span>Parameters <em>out</em> and <em>in</em> was renamed to <em>out_fd</em> and <em>in_fd</em>.</p>
  1838. </div>
  1839. </dd></dl>
  1840. <dl class="py data">
  1841. <dt class="sig sig-object py" id="os.SF_NODISKIO">
  1842. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SF_NODISKIO</span></span><a class="headerlink" href="#os.SF_NODISKIO" title="Link to this definition">¶</a></dt>
  1843. <dt class="sig sig-object py" id="os.SF_MNOWAIT">
  1844. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SF_MNOWAIT</span></span><a class="headerlink" href="#os.SF_MNOWAIT" title="Link to this definition">¶</a></dt>
  1845. <dt class="sig sig-object py" id="os.SF_SYNC">
  1846. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SF_SYNC</span></span><a class="headerlink" href="#os.SF_SYNC" title="Link to this definition">¶</a></dt>
  1847. <dd><p>Parameters to the <a class="reference internal" href="#os.sendfile" title="os.sendfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sendfile()</span></code></a> function, if the implementation supports
  1848. them.</p>
  1849. <div class="availability docutils container">
  1850. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  1851. </div>
  1852. <div class="versionadded">
  1853. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1854. </div>
  1855. </dd></dl>
  1856. <dl class="py data">
  1857. <dt class="sig sig-object py" id="os.SF_NOCACHE">
  1858. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SF_NOCACHE</span></span><a class="headerlink" href="#os.SF_NOCACHE" title="Link to this definition">¶</a></dt>
  1859. <dd><p>Parameter to the <a class="reference internal" href="#os.sendfile" title="os.sendfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sendfile()</span></code></a> function, if the implementation supports
  1860. it. The data won’t be cached in the virtual memory and will be freed afterwards.</p>
  1861. <div class="availability docutils container">
  1862. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  1863. </div>
  1864. <div class="versionadded">
  1865. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1866. </div>
  1867. </dd></dl>
  1868. <dl class="py function">
  1869. <dt class="sig sig-object py" id="os.set_blocking">
  1870. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">set_blocking</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">blocking</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="#os.set_blocking" title="Link to this definition">¶</a></dt>
  1871. <dd><p>Set the blocking mode of the specified file descriptor. Set the
  1872. <a class="reference internal" href="#os.O_NONBLOCK" title="os.O_NONBLOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_NONBLOCK</span></code></a> flag if blocking is <code class="docutils literal notranslate"><span class="pre">False</span></code>, clear the flag otherwise.</p>
  1873. <p>See also <a class="reference internal" href="#os.get_blocking" title="os.get_blocking"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_blocking()</span></code></a> and <a class="reference internal" href="socket.html#socket.socket.setblocking" title="socket.socket.setblocking"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.socket.setblocking()</span></code></a>.</p>
  1874. <div class="availability docutils container">
  1875. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  1876. <p>The function is limited on Emscripten and WASI, see
  1877. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  1878. <p>On Windows, this function is limited to pipes.</p>
  1879. </div>
  1880. <div class="versionadded">
  1881. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1882. </div>
  1883. <div class="versionchanged">
  1884. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added support for pipes on Windows.</p>
  1885. </div>
  1886. </dd></dl>
  1887. <dl class="py function">
  1888. <dt class="sig sig-object py" id="os.splice">
  1889. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">splice</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">count</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset_src</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset_dst</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.splice" title="Link to this definition">¶</a></dt>
  1890. <dd><p>Transfer <em>count</em> bytes from file descriptor <em>src</em>, starting from offset
  1891. <em>offset_src</em>, to file descriptor <em>dst</em>, starting from offset <em>offset_dst</em>.
  1892. At least one of the file descriptors must refer to a pipe. If <em>offset_src</em>
  1893. is None, then <em>src</em> is read from the current position; respectively for
  1894. <em>offset_dst</em>. The offset associated to the file descriptor that refers to a
  1895. pipe must be <code class="docutils literal notranslate"><span class="pre">None</span></code>. The files pointed by <em>src</em> and <em>dst</em> must reside in
  1896. the same filesystem, otherwise an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised with
  1897. <a class="reference internal" href="exceptions.html#OSError.errno" title="OSError.errno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errno</span></code></a> set to <a class="reference internal" href="errno.html#errno.EXDEV" title="errno.EXDEV"><code class="xref py py-const docutils literal notranslate"><span class="pre">errno.EXDEV</span></code></a>.</p>
  1898. <p>This copy is done without the additional cost of transferring data
  1899. from the kernel to user space and then back into the kernel. Additionally,
  1900. some filesystems could implement extra optimizations. The copy is done as if
  1901. both files are opened as binary.</p>
  1902. <p>Upon successful completion, returns the number of bytes spliced to or from
  1903. the pipe. A return value of 0 means end of input. If <em>src</em> refers to a
  1904. pipe, then this means that there was no data to transfer, and it would not
  1905. make sense to block because there are no writers connected to the write end
  1906. of the pipe.</p>
  1907. <div class="availability docutils container">
  1908. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.17 with glibc &gt;= 2.5</p>
  1909. </div>
  1910. <div class="versionadded">
  1911. <p><span class="versionmodified added">New in version 3.10.</span></p>
  1912. </div>
  1913. </dd></dl>
  1914. <dl class="py data">
  1915. <dt class="sig sig-object py" id="os.SPLICE_F_MOVE">
  1916. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SPLICE_F_MOVE</span></span><a class="headerlink" href="#os.SPLICE_F_MOVE" title="Link to this definition">¶</a></dt>
  1917. <dt class="sig sig-object py" id="os.SPLICE_F_NONBLOCK">
  1918. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SPLICE_F_NONBLOCK</span></span><a class="headerlink" href="#os.SPLICE_F_NONBLOCK" title="Link to this definition">¶</a></dt>
  1919. <dt class="sig sig-object py" id="os.SPLICE_F_MORE">
  1920. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SPLICE_F_MORE</span></span><a class="headerlink" href="#os.SPLICE_F_MORE" title="Link to this definition">¶</a></dt>
  1921. <dd><div class="versionadded">
  1922. <p><span class="versionmodified added">New in version 3.10.</span></p>
  1923. </div>
  1924. </dd></dl>
  1925. <dl class="py function">
  1926. <dt class="sig sig-object py" id="os.readv">
  1927. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">readv</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffers</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="#os.readv" title="Link to this definition">¶</a></dt>
  1928. <dd><p>Read from a file descriptor <em>fd</em> into a number of mutable <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like
  1929. objects</span></a> <em>buffers</em>. Transfer data into each buffer until
  1930. it is full and then move on to the next buffer in the sequence to hold the
  1931. rest of the data.</p>
  1932. <p>Return the total number of bytes actually read which can be less than the
  1933. total capacity of all the objects.</p>
  1934. <p>The operating system may set a limit (<a class="reference internal" href="#os.sysconf" title="os.sysconf"><code class="xref py py-func docutils literal notranslate"><span class="pre">sysconf()</span></code></a> value
  1935. <code class="docutils literal notranslate"><span class="pre">'SC_IOV_MAX'</span></code>) on the number of buffers that can be used.</p>
  1936. <div class="availability docutils container">
  1937. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1938. </div>
  1939. <div class="versionadded">
  1940. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1941. </div>
  1942. </dd></dl>
  1943. <dl class="py function">
  1944. <dt class="sig sig-object py" id="os.tcgetpgrp">
  1945. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">tcgetpgrp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</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="#os.tcgetpgrp" title="Link to this definition">¶</a></dt>
  1946. <dd><p>Return the process group associated with the terminal given by <em>fd</em> (an open
  1947. file descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a>).</p>
  1948. <div class="availability docutils container">
  1949. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not WASI.</p>
  1950. </div>
  1951. </dd></dl>
  1952. <dl class="py function">
  1953. <dt class="sig sig-object py" id="os.tcsetpgrp">
  1954. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">tcsetpgrp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pg</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="#os.tcsetpgrp" title="Link to this definition">¶</a></dt>
  1955. <dd><p>Set the process group associated with the terminal given by <em>fd</em> (an open file
  1956. descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a>) to <em>pg</em>.</p>
  1957. <div class="availability docutils container">
  1958. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not WASI.</p>
  1959. </div>
  1960. </dd></dl>
  1961. <dl class="py function">
  1962. <dt class="sig sig-object py" id="os.ttyname">
  1963. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">ttyname</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</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="#os.ttyname" title="Link to this definition">¶</a></dt>
  1964. <dd><p>Return a string which specifies the terminal device associated with
  1965. file descriptor <em>fd</em>. If <em>fd</em> is not associated with a terminal device, an
  1966. exception is raised.</p>
  1967. <div class="availability docutils container">
  1968. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1969. </div>
  1970. </dd></dl>
  1971. <dl class="py function">
  1972. <dt class="sig sig-object py" id="os.write">
  1973. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">str</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="#os.write" title="Link to this definition">¶</a></dt>
  1974. <dd><p>Write the bytestring in <em>str</em> to file descriptor <em>fd</em>.</p>
  1975. <p>Return the number of bytes actually written.</p>
  1976. <div class="admonition note">
  1977. <p class="admonition-title">Note</p>
  1978. <p>This function is intended for low-level I/O and must be applied to a file
  1979. descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a> or <a class="reference internal" href="#os.pipe" title="os.pipe"><code class="xref py py-func docutils literal notranslate"><span class="pre">pipe()</span></code></a>. To write a “file
  1980. object” returned by the built-in function <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> or by <a class="reference internal" href="#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">popen()</span></code></a> or
  1981. <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">fdopen()</span></code></a>, or <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> or <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>, use its
  1982. <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> method.</p>
  1983. </div>
  1984. <div class="versionchanged">
  1985. <p><span class="versionmodified changed">Changed in version 3.5: </span>If the system call is interrupted and the signal handler does not raise an
  1986. exception, the function now retries the system call instead of raising an
  1987. <a class="reference internal" href="exceptions.html#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a> exception (see <span class="target" id="index-25"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p>
  1988. </div>
  1989. </dd></dl>
  1990. <dl class="py function">
  1991. <dt class="sig sig-object py" id="os.writev">
  1992. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">writev</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffers</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="#os.writev" title="Link to this definition">¶</a></dt>
  1993. <dd><p>Write the contents of <em>buffers</em> to file descriptor <em>fd</em>. <em>buffers</em> must be
  1994. a sequence of <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>. Buffers are
  1995. processed in array order. Entire contents of the first buffer is written
  1996. before proceeding to the second, and so on.</p>
  1997. <p>Returns the total number of bytes actually written.</p>
  1998. <p>The operating system may set a limit (<a class="reference internal" href="#os.sysconf" title="os.sysconf"><code class="xref py py-func docutils literal notranslate"><span class="pre">sysconf()</span></code></a> value
  1999. <code class="docutils literal notranslate"><span class="pre">'SC_IOV_MAX'</span></code>) on the number of buffers that can be used.</p>
  2000. <div class="availability docutils container">
  2001. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  2002. </div>
  2003. <div class="versionadded">
  2004. <p><span class="versionmodified added">New in version 3.3.</span></p>
  2005. </div>
  2006. </dd></dl>
  2007. <section id="querying-the-size-of-a-terminal">
  2008. <span id="terminal-size"></span><h3>Querying the size of a terminal<a class="headerlink" href="#querying-the-size-of-a-terminal" title="Link to this heading">¶</a></h3>
  2009. <div class="versionadded">
  2010. <p><span class="versionmodified added">New in version 3.3.</span></p>
  2011. </div>
  2012. <dl class="py function">
  2013. <dt class="sig sig-object py" id="os.get_terminal_size">
  2014. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">get_terminal_size</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">STDOUT_FILENO</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="#os.get_terminal_size" title="Link to this definition">¶</a></dt>
  2015. <dd><p>Return the size of the terminal window as <code class="docutils literal notranslate"><span class="pre">(columns,</span> <span class="pre">lines)</span></code>,
  2016. tuple of type <a class="reference internal" href="#os.terminal_size" title="os.terminal_size"><code class="xref py py-class docutils literal notranslate"><span class="pre">terminal_size</span></code></a>.</p>
  2017. <p>The optional argument <code class="docutils literal notranslate"><span class="pre">fd</span></code> (default <code class="docutils literal notranslate"><span class="pre">STDOUT_FILENO</span></code>, or standard
  2018. output) specifies which file descriptor should be queried.</p>
  2019. <p>If the file descriptor is not connected to a terminal, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>
  2020. is raised.</p>
  2021. <p><a class="reference internal" href="shutil.html#shutil.get_terminal_size" title="shutil.get_terminal_size"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.get_terminal_size()</span></code></a> is the high-level function which
  2022. should normally be used, <code class="docutils literal notranslate"><span class="pre">os.get_terminal_size</span></code> is the low-level
  2023. implementation.</p>
  2024. <div class="availability docutils container">
  2025. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  2026. </div>
  2027. </dd></dl>
  2028. <dl class="py class">
  2029. <dt class="sig sig-object py" id="os.terminal_size">
  2030. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">terminal_size</span></span><a class="headerlink" href="#os.terminal_size" title="Link to this definition">¶</a></dt>
  2031. <dd><p>A subclass of tuple, holding <code class="docutils literal notranslate"><span class="pre">(columns,</span> <span class="pre">lines)</span></code> of the terminal window size.</p>
  2032. <dl class="py attribute">
  2033. <dt class="sig sig-object py" id="os.terminal_size.columns">
  2034. <span class="sig-name descname"><span class="pre">columns</span></span><a class="headerlink" href="#os.terminal_size.columns" title="Link to this definition">¶</a></dt>
  2035. <dd><p>Width of the terminal window in characters.</p>
  2036. </dd></dl>
  2037. <dl class="py attribute">
  2038. <dt class="sig sig-object py" id="os.terminal_size.lines">
  2039. <span class="sig-name descname"><span class="pre">lines</span></span><a class="headerlink" href="#os.terminal_size.lines" title="Link to this definition">¶</a></dt>
  2040. <dd><p>Height of the terminal window in characters.</p>
  2041. </dd></dl>
  2042. </dd></dl>
  2043. </section>
  2044. <section id="inheritance-of-file-descriptors">
  2045. <span id="fd-inheritance"></span><h3>Inheritance of File Descriptors<a class="headerlink" href="#inheritance-of-file-descriptors" title="Link to this heading">¶</a></h3>
  2046. <div class="versionadded">
  2047. <p><span class="versionmodified added">New in version 3.4.</span></p>
  2048. </div>
  2049. <p>A file descriptor has an “inheritable” flag which indicates if the file descriptor
  2050. can be inherited by child processes. Since Python 3.4, file descriptors
  2051. created by Python are non-inheritable by default.</p>
  2052. <p>On UNIX, non-inheritable file descriptors are closed in child processes at the
  2053. execution of a new program, other file descriptors are inherited.</p>
  2054. <p>On Windows, non-inheritable handles and file descriptors are closed in child
  2055. processes, except for standard streams (file descriptors 0, 1 and 2: stdin, stdout
  2056. and stderr), which are always inherited. Using <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*</span></code></a> functions,
  2057. all inheritable handles and all inheritable file descriptors are inherited.
  2058. Using the <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module, all file descriptors except standard
  2059. streams are closed, and inheritable handles are only inherited if the
  2060. <em>close_fds</em> parameter is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  2061. <p>On WebAssembly platforms <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>, the file
  2062. descriptor cannot be modified.</p>
  2063. <dl class="py function">
  2064. <dt class="sig sig-object py" id="os.get_inheritable">
  2065. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">get_inheritable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</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="#os.get_inheritable" title="Link to this definition">¶</a></dt>
  2066. <dd><p>Get the “inheritable” flag of the specified file descriptor (a boolean).</p>
  2067. </dd></dl>
  2068. <dl class="py function">
  2069. <dt class="sig sig-object py" id="os.set_inheritable">
  2070. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">set_inheritable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">inheritable</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="#os.set_inheritable" title="Link to this definition">¶</a></dt>
  2071. <dd><p>Set the “inheritable” flag of the specified file descriptor.</p>
  2072. </dd></dl>
  2073. <dl class="py function">
  2074. <dt class="sig sig-object py" id="os.get_handle_inheritable">
  2075. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">get_handle_inheritable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">handle</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="#os.get_handle_inheritable" title="Link to this definition">¶</a></dt>
  2076. <dd><p>Get the “inheritable” flag of the specified handle (a boolean).</p>
  2077. <div class="availability docutils container">
  2078. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  2079. </div>
  2080. </dd></dl>
  2081. <dl class="py function">
  2082. <dt class="sig sig-object py" id="os.set_handle_inheritable">
  2083. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">set_handle_inheritable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">handle</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">inheritable</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="#os.set_handle_inheritable" title="Link to this definition">¶</a></dt>
  2084. <dd><p>Set the “inheritable” flag of the specified handle.</p>
  2085. <div class="availability docutils container">
  2086. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  2087. </div>
  2088. </dd></dl>
  2089. </section>
  2090. </section>
  2091. <section id="files-and-directories">
  2092. <span id="os-file-dir"></span><h2>Files and Directories<a class="headerlink" href="#files-and-directories" title="Link to this heading">¶</a></h2>
  2093. <p>On some Unix platforms, many of these functions support one or more of these
  2094. features:</p>
  2095. <ul id="path-fd">
  2096. <li><p><strong>specifying a file descriptor:</strong>
  2097. Normally the <em>path</em> argument provided to functions in the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module
  2098. must be a string specifying a file path. However, some functions now
  2099. alternatively accept an open file descriptor for their <em>path</em> argument.
  2100. The function will then operate on the file referred to by the descriptor.
  2101. (For POSIX systems, Python will call the variant of the function prefixed
  2102. with <code class="docutils literal notranslate"><span class="pre">f</span></code> (e.g. call <code class="docutils literal notranslate"><span class="pre">fchdir</span></code> instead of <code class="docutils literal notranslate"><span class="pre">chdir</span></code>).)</p>
  2103. <p>You can check whether or not <em>path</em> can be specified as a file descriptor
  2104. for a particular function on your platform using <a class="reference internal" href="#os.supports_fd" title="os.supports_fd"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.supports_fd</span></code></a>.
  2105. If this functionality is unavailable, using it will raise a
  2106. <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  2107. <p>If the function also supports <em>dir_fd</em> or <em>follow_symlinks</em> arguments, it’s
  2108. an error to specify one of those when supplying <em>path</em> as a file descriptor.</p>
  2109. </li>
  2110. </ul>
  2111. <ul id="dir-fd">
  2112. <li><p><strong>paths relative to directory descriptors:</strong> If <em>dir_fd</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it
  2113. should be a file descriptor referring to a directory, and the path to operate
  2114. on should be relative; path will then be relative to that directory. If the
  2115. path is absolute, <em>dir_fd</em> is ignored. (For POSIX systems, Python will call
  2116. the variant of the function with an <code class="docutils literal notranslate"><span class="pre">at</span></code> suffix and possibly prefixed with
  2117. <code class="docutils literal notranslate"><span class="pre">f</span></code> (e.g. call <code class="docutils literal notranslate"><span class="pre">faccessat</span></code> instead of <code class="docutils literal notranslate"><span class="pre">access</span></code>).</p>
  2118. <p>You can check whether or not <em>dir_fd</em> is supported for a particular function
  2119. on your platform using <a class="reference internal" href="#os.supports_dir_fd" title="os.supports_dir_fd"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.supports_dir_fd</span></code></a>. If it’s unavailable,
  2120. using it will raise a <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  2121. </li>
  2122. </ul>
  2123. <ul id="follow-symlinks">
  2124. <li><p><strong>not following symlinks:</strong> If <em>follow_symlinks</em> is
  2125. <code class="docutils literal notranslate"><span class="pre">False</span></code>, and the last element of the path to operate on is a symbolic link,
  2126. the function will operate on the symbolic link itself rather than the file
  2127. pointed to by the link. (For POSIX systems, Python will call the <code class="docutils literal notranslate"><span class="pre">l...</span></code>
  2128. variant of the function.)</p>
  2129. <p>You can check whether or not <em>follow_symlinks</em> is supported for a particular
  2130. function on your platform using <a class="reference internal" href="#os.supports_follow_symlinks" title="os.supports_follow_symlinks"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.supports_follow_symlinks</span></code></a>.
  2131. If it’s unavailable, using it will raise a <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  2132. </li>
  2133. </ul>
  2134. <dl class="py function">
  2135. <dt class="sig sig-object py" id="os.access">
  2136. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">access</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">effective_ids</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.access" title="Link to this definition">¶</a></dt>
  2137. <dd><p>Use the real uid/gid to test for access to <em>path</em>. Note that most operations
  2138. will use the effective uid/gid, therefore this routine can be used in a
  2139. suid/sgid environment to test if the invoking user has the specified access to
  2140. <em>path</em>. <em>mode</em> should be <a class="reference internal" href="#os.F_OK" title="os.F_OK"><code class="xref py py-const docutils literal notranslate"><span class="pre">F_OK</span></code></a> to test the existence of <em>path</em>, or it
  2141. can be the inclusive OR of one or more of <a class="reference internal" href="#os.R_OK" title="os.R_OK"><code class="xref py py-const docutils literal notranslate"><span class="pre">R_OK</span></code></a>, <a class="reference internal" href="#os.W_OK" title="os.W_OK"><code class="xref py py-const docutils literal notranslate"><span class="pre">W_OK</span></code></a>, and
  2142. <a class="reference internal" href="#os.X_OK" title="os.X_OK"><code class="xref py py-const docutils literal notranslate"><span class="pre">X_OK</span></code></a> to test permissions. Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if access is allowed,
  2143. <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> if not. See the Unix man page <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/access(2)">access(2)</a></em> for more
  2144. information.</p>
  2145. <p>This function can support specifying <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory
  2146. descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p>
  2147. <p>If <em>effective_ids</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, <a class="reference internal" href="#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">access()</span></code></a> will perform its access
  2148. checks using the effective uid/gid instead of the real uid/gid.
  2149. <em>effective_ids</em> may not be supported on your platform; you can check whether
  2150. or not it is available using <a class="reference internal" href="#os.supports_effective_ids" title="os.supports_effective_ids"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.supports_effective_ids</span></code></a>. If it is
  2151. unavailable, using it will raise a <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  2152. <div class="admonition note">
  2153. <p class="admonition-title">Note</p>
  2154. <p>Using <a class="reference internal" href="#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">access()</span></code></a> to check if a user is authorized to e.g. open a file
  2155. before actually doing so using <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> creates a security hole,
  2156. because the user might exploit the short time interval between checking
  2157. and opening the file to manipulate it. It’s preferable to use <a class="reference internal" href="../glossary.html#term-EAFP"><span class="xref std std-term">EAFP</span></a>
  2158. techniques. For example:</p>
  2159. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">access</span><span class="p">(</span><span class="s2">&quot;myfile&quot;</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">R_OK</span><span class="p">):</span>
  2160. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;myfile&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
  2161. <span class="k">return</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  2162. <span class="k">return</span> <span class="s2">&quot;some default data&quot;</span>
  2163. </pre></div>
  2164. </div>
  2165. <p>is better written as:</p>
  2166. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
  2167. <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;myfile&quot;</span><span class="p">)</span>
  2168. <span class="k">except</span> <span class="ne">PermissionError</span><span class="p">:</span>
  2169. <span class="k">return</span> <span class="s2">&quot;some default data&quot;</span>
  2170. <span class="k">else</span><span class="p">:</span>
  2171. <span class="k">with</span> <span class="n">fp</span><span class="p">:</span>
  2172. <span class="k">return</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  2173. </pre></div>
  2174. </div>
  2175. </div>
  2176. <div class="admonition note">
  2177. <p class="admonition-title">Note</p>
  2178. <p>I/O operations may fail even when <a class="reference internal" href="#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">access()</span></code></a> indicates that they would
  2179. succeed, particularly for operations on network filesystems which may have
  2180. permissions semantics beyond the usual POSIX permission-bit model.</p>
  2181. </div>
  2182. <div class="versionchanged">
  2183. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em>, <em>effective_ids</em>, and <em>follow_symlinks</em> parameters.</p>
  2184. </div>
  2185. <div class="versionchanged">
  2186. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2187. </div>
  2188. </dd></dl>
  2189. <dl class="py data">
  2190. <dt class="sig sig-object py" id="os.F_OK">
  2191. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">F_OK</span></span><a class="headerlink" href="#os.F_OK" title="Link to this definition">¶</a></dt>
  2192. <dt class="sig sig-object py" id="os.R_OK">
  2193. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">R_OK</span></span><a class="headerlink" href="#os.R_OK" title="Link to this definition">¶</a></dt>
  2194. <dt class="sig sig-object py" id="os.W_OK">
  2195. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">W_OK</span></span><a class="headerlink" href="#os.W_OK" title="Link to this definition">¶</a></dt>
  2196. <dt class="sig sig-object py" id="os.X_OK">
  2197. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">X_OK</span></span><a class="headerlink" href="#os.X_OK" title="Link to this definition">¶</a></dt>
  2198. <dd><p>Values to pass as the <em>mode</em> parameter of <a class="reference internal" href="#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">access()</span></code></a> to test the
  2199. existence, readability, writability and executability of <em>path</em>,
  2200. respectively.</p>
  2201. </dd></dl>
  2202. <dl class="py function">
  2203. <dt class="sig sig-object py" id="os.chdir">
  2204. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">chdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.chdir" title="Link to this definition">¶</a></dt>
  2205. <dd><p id="index-26">Change the current working directory to <em>path</em>.</p>
  2206. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>. The
  2207. descriptor must refer to an opened directory, not an open file.</p>
  2208. <p>This function can raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> and subclasses such as
  2209. <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a>, <a class="reference internal" href="exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a>, and <a class="reference internal" href="exceptions.html#NotADirectoryError" title="NotADirectoryError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotADirectoryError</span></code></a>.</p>
  2210. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chdir</span></code> with argument <code class="docutils literal notranslate"><span class="pre">path</span></code>.</p>
  2211. <div class="versionchanged">
  2212. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added support for specifying <em>path</em> as a file descriptor
  2213. on some platforms.</p>
  2214. </div>
  2215. <div class="versionchanged">
  2216. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2217. </div>
  2218. </dd></dl>
  2219. <dl class="py function">
  2220. <dt class="sig sig-object py" id="os.chflags">
  2221. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">chflags</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.chflags" title="Link to this definition">¶</a></dt>
  2222. <dd><p>Set the flags of <em>path</em> to the numeric <em>flags</em>. <em>flags</em> may take a combination
  2223. (bitwise OR) of the following values (as defined in the <a class="reference internal" href="stat.html#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code class="xref py py-mod docutils literal notranslate"><span class="pre">stat</span></code></a> module):</p>
  2224. <ul class="simple">
  2225. <li><p><a class="reference internal" href="stat.html#stat.UF_NODUMP" title="stat.UF_NODUMP"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.UF_NODUMP</span></code></a></p></li>
  2226. <li><p><a class="reference internal" href="stat.html#stat.UF_IMMUTABLE" title="stat.UF_IMMUTABLE"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.UF_IMMUTABLE</span></code></a></p></li>
  2227. <li><p><a class="reference internal" href="stat.html#stat.UF_APPEND" title="stat.UF_APPEND"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.UF_APPEND</span></code></a></p></li>
  2228. <li><p><a class="reference internal" href="stat.html#stat.UF_OPAQUE" title="stat.UF_OPAQUE"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.UF_OPAQUE</span></code></a></p></li>
  2229. <li><p><a class="reference internal" href="stat.html#stat.UF_NOUNLINK" title="stat.UF_NOUNLINK"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.UF_NOUNLINK</span></code></a></p></li>
  2230. <li><p><a class="reference internal" href="stat.html#stat.UF_COMPRESSED" title="stat.UF_COMPRESSED"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.UF_COMPRESSED</span></code></a></p></li>
  2231. <li><p><a class="reference internal" href="stat.html#stat.UF_HIDDEN" title="stat.UF_HIDDEN"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.UF_HIDDEN</span></code></a></p></li>
  2232. <li><p><a class="reference internal" href="stat.html#stat.SF_ARCHIVED" title="stat.SF_ARCHIVED"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.SF_ARCHIVED</span></code></a></p></li>
  2233. <li><p><a class="reference internal" href="stat.html#stat.SF_IMMUTABLE" title="stat.SF_IMMUTABLE"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.SF_IMMUTABLE</span></code></a></p></li>
  2234. <li><p><a class="reference internal" href="stat.html#stat.SF_APPEND" title="stat.SF_APPEND"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.SF_APPEND</span></code></a></p></li>
  2235. <li><p><a class="reference internal" href="stat.html#stat.SF_NOUNLINK" title="stat.SF_NOUNLINK"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.SF_NOUNLINK</span></code></a></p></li>
  2236. <li><p><a class="reference internal" href="stat.html#stat.SF_SNAPSHOT" title="stat.SF_SNAPSHOT"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.SF_SNAPSHOT</span></code></a></p></li>
  2237. </ul>
  2238. <p>This function can support <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p>
  2239. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chflags</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">flags</span></code>.</p>
  2240. <div class="availability docutils container">
  2241. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  2242. </div>
  2243. <div class="versionchanged">
  2244. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>follow_symlinks</em> parameter.</p>
  2245. </div>
  2246. <div class="versionchanged">
  2247. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2248. </div>
  2249. </dd></dl>
  2250. <dl class="py function">
  2251. <dt class="sig sig-object py" id="os.chmod">
  2252. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">chmod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.chmod" title="Link to this definition">¶</a></dt>
  2253. <dd><p>Change the mode of <em>path</em> to the numeric <em>mode</em>. <em>mode</em> may take one of the
  2254. following values (as defined in the <a class="reference internal" href="stat.html#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code class="xref py py-mod docutils literal notranslate"><span class="pre">stat</span></code></a> module) or bitwise ORed
  2255. combinations of them:</p>
  2256. <ul class="simple">
  2257. <li><p><a class="reference internal" href="stat.html#stat.S_ISUID" title="stat.S_ISUID"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_ISUID</span></code></a></p></li>
  2258. <li><p><a class="reference internal" href="stat.html#stat.S_ISGID" title="stat.S_ISGID"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_ISGID</span></code></a></p></li>
  2259. <li><p><a class="reference internal" href="stat.html#stat.S_ENFMT" title="stat.S_ENFMT"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_ENFMT</span></code></a></p></li>
  2260. <li><p><a class="reference internal" href="stat.html#stat.S_ISVTX" title="stat.S_ISVTX"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_ISVTX</span></code></a></p></li>
  2261. <li><p><a class="reference internal" href="stat.html#stat.S_IREAD" title="stat.S_IREAD"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IREAD</span></code></a></p></li>
  2262. <li><p><a class="reference internal" href="stat.html#stat.S_IWRITE" title="stat.S_IWRITE"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IWRITE</span></code></a></p></li>
  2263. <li><p><a class="reference internal" href="stat.html#stat.S_IEXEC" title="stat.S_IEXEC"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IEXEC</span></code></a></p></li>
  2264. <li><p><a class="reference internal" href="stat.html#stat.S_IRWXU" title="stat.S_IRWXU"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IRWXU</span></code></a></p></li>
  2265. <li><p><a class="reference internal" href="stat.html#stat.S_IRUSR" title="stat.S_IRUSR"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IRUSR</span></code></a></p></li>
  2266. <li><p><a class="reference internal" href="stat.html#stat.S_IWUSR" title="stat.S_IWUSR"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IWUSR</span></code></a></p></li>
  2267. <li><p><a class="reference internal" href="stat.html#stat.S_IXUSR" title="stat.S_IXUSR"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IXUSR</span></code></a></p></li>
  2268. <li><p><a class="reference internal" href="stat.html#stat.S_IRWXG" title="stat.S_IRWXG"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IRWXG</span></code></a></p></li>
  2269. <li><p><a class="reference internal" href="stat.html#stat.S_IRGRP" title="stat.S_IRGRP"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IRGRP</span></code></a></p></li>
  2270. <li><p><a class="reference internal" href="stat.html#stat.S_IWGRP" title="stat.S_IWGRP"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IWGRP</span></code></a></p></li>
  2271. <li><p><a class="reference internal" href="stat.html#stat.S_IXGRP" title="stat.S_IXGRP"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IXGRP</span></code></a></p></li>
  2272. <li><p><a class="reference internal" href="stat.html#stat.S_IRWXO" title="stat.S_IRWXO"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IRWXO</span></code></a></p></li>
  2273. <li><p><a class="reference internal" href="stat.html#stat.S_IROTH" title="stat.S_IROTH"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IROTH</span></code></a></p></li>
  2274. <li><p><a class="reference internal" href="stat.html#stat.S_IWOTH" title="stat.S_IWOTH"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IWOTH</span></code></a></p></li>
  2275. <li><p><a class="reference internal" href="stat.html#stat.S_IXOTH" title="stat.S_IXOTH"><code class="xref py py-const docutils literal notranslate"><span class="pre">stat.S_IXOTH</span></code></a></p></li>
  2276. </ul>
  2277. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>,
  2278. <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not
  2279. following symlinks</span></a>.</p>
  2280. <div class="admonition note">
  2281. <p class="admonition-title">Note</p>
  2282. <p>Although Windows supports <a class="reference internal" href="#os.chmod" title="os.chmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">chmod()</span></code></a>, you can only set the file’s
  2283. read-only flag with it (via the <code class="docutils literal notranslate"><span class="pre">stat.S_IWRITE</span></code> and <code class="docutils literal notranslate"><span class="pre">stat.S_IREAD</span></code>
  2284. constants or a corresponding integer value). All other bits are ignored.</p>
  2285. <p>The function is limited on Emscripten and WASI, see
  2286. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  2287. </div>
  2288. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chmod</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">mode</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  2289. <div class="versionadded">
  2290. <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor,
  2291. and the <em>dir_fd</em> and <em>follow_symlinks</em> arguments.</p>
  2292. </div>
  2293. <div class="versionchanged">
  2294. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2295. </div>
  2296. </dd></dl>
  2297. <dl class="py function">
  2298. <dt class="sig sig-object py" id="os.chown">
  2299. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">chown</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gid</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.chown" title="Link to this definition">¶</a></dt>
  2300. <dd><p>Change the owner and group id of <em>path</em> to the numeric <em>uid</em> and <em>gid</em>. To
  2301. leave one of the ids unchanged, set it to -1.</p>
  2302. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>,
  2303. <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not
  2304. following symlinks</span></a>.</p>
  2305. <p>See <a class="reference internal" href="shutil.html#shutil.chown" title="shutil.chown"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.chown()</span></code></a> for a higher-level function that accepts names in
  2306. addition to numeric ids.</p>
  2307. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chown</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">uid</span></code>, <code class="docutils literal notranslate"><span class="pre">gid</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  2308. <div class="availability docutils container">
  2309. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  2310. <p>The function is limited on Emscripten and WASI, see
  2311. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  2312. </div>
  2313. <div class="versionadded">
  2314. <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor,
  2315. and the <em>dir_fd</em> and <em>follow_symlinks</em> arguments.</p>
  2316. </div>
  2317. <div class="versionchanged">
  2318. <p><span class="versionmodified changed">Changed in version 3.6: </span>Supports a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  2319. </div>
  2320. </dd></dl>
  2321. <dl class="py function">
  2322. <dt class="sig sig-object py" id="os.chroot">
  2323. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">chroot</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.chroot" title="Link to this definition">¶</a></dt>
  2324. <dd><p>Change the root directory of the current process to <em>path</em>.</p>
  2325. <div class="availability docutils container">
  2326. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  2327. </div>
  2328. <div class="versionchanged">
  2329. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2330. </div>
  2331. </dd></dl>
  2332. <dl class="py function">
  2333. <dt class="sig sig-object py" id="os.fchdir">
  2334. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fchdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.fchdir" title="Link to this definition">¶</a></dt>
  2335. <dd><p>Change the current working directory to the directory represented by the file
  2336. descriptor <em>fd</em>. The descriptor must refer to an opened directory, not an
  2337. open file. As of Python 3.3, this is equivalent to <code class="docutils literal notranslate"><span class="pre">os.chdir(fd)</span></code>.</p>
  2338. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chdir</span></code> with argument <code class="docutils literal notranslate"><span class="pre">path</span></code>.</p>
  2339. <div class="availability docutils container">
  2340. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  2341. </div>
  2342. </dd></dl>
  2343. <dl class="py function">
  2344. <dt class="sig sig-object py" id="os.getcwd">
  2345. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getcwd</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getcwd" title="Link to this definition">¶</a></dt>
  2346. <dd><p>Return a string representing the current working directory.</p>
  2347. </dd></dl>
  2348. <dl class="py function">
  2349. <dt class="sig sig-object py" id="os.getcwdb">
  2350. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getcwdb</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getcwdb" title="Link to this definition">¶</a></dt>
  2351. <dd><p>Return a bytestring representing the current working directory.</p>
  2352. <div class="versionchanged">
  2353. <p><span class="versionmodified changed">Changed in version 3.8: </span>The function now uses the UTF-8 encoding on Windows, rather than the ANSI
  2354. code page: see <span class="target" id="index-27"></span><a class="pep reference external" href="https://peps.python.org/pep-0529/"><strong>PEP 529</strong></a> for the rationale. The function is no longer
  2355. deprecated on Windows.</p>
  2356. </div>
  2357. </dd></dl>
  2358. <dl class="py function">
  2359. <dt class="sig sig-object py" id="os.lchflags">
  2360. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">lchflags</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.lchflags" title="Link to this definition">¶</a></dt>
  2361. <dd><p>Set the flags of <em>path</em> to the numeric <em>flags</em>, like <a class="reference internal" href="#os.chflags" title="os.chflags"><code class="xref py py-func docutils literal notranslate"><span class="pre">chflags()</span></code></a>, but do
  2362. not follow symbolic links. As of Python 3.3, this is equivalent to
  2363. <code class="docutils literal notranslate"><span class="pre">os.chflags(path,</span> <span class="pre">flags,</span> <span class="pre">follow_symlinks=False)</span></code>.</p>
  2364. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chflags</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">flags</span></code>.</p>
  2365. <div class="availability docutils container">
  2366. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  2367. </div>
  2368. <div class="versionchanged">
  2369. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2370. </div>
  2371. </dd></dl>
  2372. <dl class="py function">
  2373. <dt class="sig sig-object py" id="os.lchmod">
  2374. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">lchmod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.lchmod" title="Link to this definition">¶</a></dt>
  2375. <dd><p>Change the mode of <em>path</em> to the numeric <em>mode</em>. If path is a symlink, this
  2376. affects the symlink rather than the target. See the docs for <a class="reference internal" href="#os.chmod" title="os.chmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">chmod()</span></code></a>
  2377. for possible values of <em>mode</em>. As of Python 3.3, this is equivalent to
  2378. <code class="docutils literal notranslate"><span class="pre">os.chmod(path,</span> <span class="pre">mode,</span> <span class="pre">follow_symlinks=False)</span></code>.</p>
  2379. <p><code class="docutils literal notranslate"><span class="pre">lchmod()</span></code> is not part of POSIX, but Unix implementations may have it if
  2380. changing the mode of symbolic links is supported.</p>
  2381. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chmod</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">mode</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  2382. <div class="availability docutils container">
  2383. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Linux, FreeBSD &gt;= 1.3, NetBSD &gt;= 1.3, not OpenBSD</p>
  2384. </div>
  2385. <div class="versionchanged">
  2386. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2387. </div>
  2388. </dd></dl>
  2389. <dl class="py function">
  2390. <dt class="sig sig-object py" id="os.lchown">
  2391. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">lchown</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gid</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.lchown" title="Link to this definition">¶</a></dt>
  2392. <dd><p>Change the owner and group id of <em>path</em> to the numeric <em>uid</em> and <em>gid</em>. This
  2393. function will not follow symbolic links. As of Python 3.3, this is equivalent
  2394. to <code class="docutils literal notranslate"><span class="pre">os.chown(path,</span> <span class="pre">uid,</span> <span class="pre">gid,</span> <span class="pre">follow_symlinks=False)</span></code>.</p>
  2395. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.chown</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">uid</span></code>, <code class="docutils literal notranslate"><span class="pre">gid</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  2396. <div class="availability docutils container">
  2397. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  2398. </div>
  2399. <div class="versionchanged">
  2400. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2401. </div>
  2402. </dd></dl>
  2403. <dl class="py function">
  2404. <dt class="sig sig-object py" id="os.link">
  2405. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">link</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">src_dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst_dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.link" title="Link to this definition">¶</a></dt>
  2406. <dd><p>Create a hard link pointing to <em>src</em> named <em>dst</em>.</p>
  2407. <p>This function can support specifying <em>src_dir_fd</em> and/or <em>dst_dir_fd</em> to
  2408. supply <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>, and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not
  2409. following symlinks</span></a>.</p>
  2410. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.link</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>, <code class="docutils literal notranslate"><span class="pre">src_dir_fd</span></code>, <code class="docutils literal notranslate"><span class="pre">dst_dir_fd</span></code>.</p>
  2411. <div class="availability docutils container">
  2412. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten.</p>
  2413. </div>
  2414. <div class="versionchanged">
  2415. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added Windows support.</p>
  2416. </div>
  2417. <div class="versionchanged">
  2418. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>src_dir_fd</em>, <em>dst_dir_fd</em>, and <em>follow_symlinks</em> parameters.</p>
  2419. </div>
  2420. <div class="versionchanged">
  2421. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>src</em> and <em>dst</em>.</p>
  2422. </div>
  2423. </dd></dl>
  2424. <dl class="py function">
  2425. <dt class="sig sig-object py" id="os.listdir">
  2426. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">listdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'.'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.listdir" title="Link to this definition">¶</a></dt>
  2427. <dd><p>Return a list containing the names of the entries in the directory given by
  2428. <em>path</em>. The list is in arbitrary order, and does not include the special
  2429. entries <code class="docutils literal notranslate"><span class="pre">'.'</span></code> and <code class="docutils literal notranslate"><span class="pre">'..'</span></code> even if they are present in the directory.
  2430. If a file is removed from or added to the directory during the call of
  2431. this function, whether a name for that file be included is unspecified.</p>
  2432. <p><em>path</em> may be a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>. If <em>path</em> is of type <code class="docutils literal notranslate"><span class="pre">bytes</span></code>
  2433. (directly or indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathLike</span></code></a> interface),
  2434. the filenames returned will also be of type <code class="docutils literal notranslate"><span class="pre">bytes</span></code>;
  2435. in all other circumstances, they will be of type <code class="docutils literal notranslate"><span class="pre">str</span></code>.</p>
  2436. <p>This function can also support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>; the file descriptor must refer to a directory.</p>
  2437. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.listdir</span></code> with argument <code class="docutils literal notranslate"><span class="pre">path</span></code>.</p>
  2438. <div class="admonition note">
  2439. <p class="admonition-title">Note</p>
  2440. <p>To encode <code class="docutils literal notranslate"><span class="pre">str</span></code> filenames to <code class="docutils literal notranslate"><span class="pre">bytes</span></code>, use <a class="reference internal" href="#os.fsencode" title="os.fsencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsencode()</span></code></a>.</p>
  2441. </div>
  2442. <div class="admonition seealso">
  2443. <p class="admonition-title">See also</p>
  2444. <p>The <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> function returns directory entries along with
  2445. file attribute information, giving better performance for many
  2446. common use cases.</p>
  2447. </div>
  2448. <div class="versionchanged">
  2449. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>path</em> parameter became optional.</p>
  2450. </div>
  2451. <div class="versionadded">
  2452. <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor.</p>
  2453. </div>
  2454. <div class="versionchanged">
  2455. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2456. </div>
  2457. </dd></dl>
  2458. <dl class="py function">
  2459. <dt class="sig sig-object py" id="os.listdrives">
  2460. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">listdrives</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.listdrives" title="Link to this definition">¶</a></dt>
  2461. <dd><p>Return a list containing the names of drives on a Windows system.</p>
  2462. <p>A drive name typically looks like <code class="docutils literal notranslate"><span class="pre">'C:\\'</span></code>. Not every drive name
  2463. will be associated with a volume, and some may be inaccessible for
  2464. a variety of reasons, including permissions, network connectivity
  2465. or missing media. This function does not test for access.</p>
  2466. <p>May raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if an error occurs collecting the drive
  2467. names.</p>
  2468. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.listdrives</span></code> with no arguments.</p>
  2469. <div class="availability docutils container">
  2470. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows</p>
  2471. </div>
  2472. <div class="versionadded">
  2473. <p><span class="versionmodified added">New in version 3.12.</span></p>
  2474. </div>
  2475. </dd></dl>
  2476. <dl class="py function">
  2477. <dt class="sig sig-object py" id="os.listmounts">
  2478. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">listmounts</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">volume</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.listmounts" title="Link to this definition">¶</a></dt>
  2479. <dd><p>Return a list containing the mount points for a volume on a Windows
  2480. system.</p>
  2481. <p><em>volume</em> must be represented as a GUID path, like those returned by
  2482. <a class="reference internal" href="#os.listvolumes" title="os.listvolumes"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listvolumes()</span></code></a>. Volumes may be mounted in multiple locations
  2483. or not at all. In the latter case, the list will be empty. Mount
  2484. points that are not associated with a volume will not be returned by
  2485. this function.</p>
  2486. <p>The mount points return by this function will be absolute paths, and
  2487. may be longer than the drive name.</p>
  2488. <p>Raises <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if the volume is not recognized or if an error
  2489. occurs collecting the paths.</p>
  2490. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.listmounts</span></code> with argument <code class="docutils literal notranslate"><span class="pre">volume</span></code>.</p>
  2491. <div class="availability docutils container">
  2492. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows</p>
  2493. </div>
  2494. <div class="versionadded">
  2495. <p><span class="versionmodified added">New in version 3.12.</span></p>
  2496. </div>
  2497. </dd></dl>
  2498. <dl class="py function">
  2499. <dt class="sig sig-object py" id="os.listvolumes">
  2500. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">listvolumes</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.listvolumes" title="Link to this definition">¶</a></dt>
  2501. <dd><p>Return a list containing the volumes in the system.</p>
  2502. <p>Volumes are typically represented as a GUID path that looks like
  2503. <code class="docutils literal notranslate"><span class="pre">\\?\Volume{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\</span></code>. Files can
  2504. usually be accessed through a GUID path, permissions allowing.
  2505. However, users are generally not familiar with them, and so the
  2506. recommended use of this function is to retrieve mount points
  2507. using <a class="reference internal" href="#os.listmounts" title="os.listmounts"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listmounts()</span></code></a>.</p>
  2508. <p>May raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if an error occurs collecting the volumes.</p>
  2509. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.listvolumes</span></code> with no arguments.</p>
  2510. <div class="availability docutils container">
  2511. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows</p>
  2512. </div>
  2513. <div class="versionadded">
  2514. <p><span class="versionmodified added">New in version 3.12.</span></p>
  2515. </div>
  2516. </dd></dl>
  2517. <dl class="py function">
  2518. <dt class="sig sig-object py" id="os.lstat">
  2519. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">lstat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.lstat" title="Link to this definition">¶</a></dt>
  2520. <dd><p>Perform the equivalent of an <code class="xref c c-func docutils literal notranslate"><span class="pre">lstat()</span></code> system call on the given path.
  2521. Similar to <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a>, but does not follow symbolic links. Return a
  2522. <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code class="xref py py-class docutils literal notranslate"><span class="pre">stat_result</span></code></a> object.</p>
  2523. <p>On platforms that do not support symbolic links, this is an alias for
  2524. <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a>.</p>
  2525. <p>As of Python 3.3, this is equivalent to <code class="docutils literal notranslate"><span class="pre">os.stat(path,</span> <span class="pre">dir_fd=dir_fd,</span>
  2526. <span class="pre">follow_symlinks=False)</span></code>.</p>
  2527. <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  2528. <div class="admonition seealso">
  2529. <p class="admonition-title">See also</p>
  2530. <p>The <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a> function.</p>
  2531. </div>
  2532. <div class="versionchanged">
  2533. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added support for Windows 6.0 (Vista) symbolic links.</p>
  2534. </div>
  2535. <div class="versionchanged">
  2536. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p>
  2537. </div>
  2538. <div class="versionchanged">
  2539. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2540. </div>
  2541. <div class="versionchanged">
  2542. <p><span class="versionmodified changed">Changed in version 3.8: </span>On Windows, now opens reparse points that represent another path
  2543. (name surrogates), including symbolic links and directory junctions.
  2544. Other kinds of reparse points are resolved by the operating system as
  2545. for <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a>.</p>
  2546. </div>
  2547. </dd></dl>
  2548. <dl class="py function">
  2549. <dt class="sig sig-object py" id="os.mkdir">
  2550. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">mkdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</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">0o777</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.mkdir" title="Link to this definition">¶</a></dt>
  2551. <dd><p>Create a directory named <em>path</em> with numeric mode <em>mode</em>.</p>
  2552. <p>If the directory already exists, <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> is raised. If a parent
  2553. directory in the path does not exist, <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> is raised.</p>
  2554. <p id="mkdir-modebits">On some systems, <em>mode</em> is ignored. Where it is used, the current umask
  2555. value is first masked out. If bits other than the last 9 (i.e. the last 3
  2556. digits of the octal representation of the <em>mode</em>) are set, their meaning is
  2557. platform-dependent. On some platforms, they are ignored and you should call
  2558. <a class="reference internal" href="#os.chmod" title="os.chmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">chmod()</span></code></a> explicitly to set them.</p>
  2559. <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  2560. <p>It is also possible to create temporary directories; see the
  2561. <a class="reference internal" href="tempfile.html#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code></a> module’s <a class="reference internal" href="tempfile.html#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">tempfile.mkdtemp()</span></code></a> function.</p>
  2562. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.mkdir</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">mode</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  2563. <div class="versionchanged">
  2564. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p>
  2565. </div>
  2566. <div class="versionchanged">
  2567. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2568. </div>
  2569. </dd></dl>
  2570. <dl class="py function">
  2571. <dt class="sig sig-object py" id="os.makedirs">
  2572. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">makedirs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0o777</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exist_ok</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.makedirs" title="Link to this definition">¶</a></dt>
  2573. <dd><p id="index-28">Recursive directory creation function. Like <a class="reference internal" href="#os.mkdir" title="os.mkdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkdir()</span></code></a>, but makes all
  2574. intermediate-level directories needed to contain the leaf directory.</p>
  2575. <p>The <em>mode</em> parameter is passed to <a class="reference internal" href="#os.mkdir" title="os.mkdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkdir()</span></code></a> for creating the leaf
  2576. directory; see <a class="reference internal" href="#mkdir-modebits"><span class="std std-ref">the mkdir() description</span></a> for how it
  2577. is interpreted. To set the file permission bits of any newly created parent
  2578. directories you can set the umask before invoking <a class="reference internal" href="#os.makedirs" title="os.makedirs"><code class="xref py py-func docutils literal notranslate"><span class="pre">makedirs()</span></code></a>. The
  2579. file permission bits of existing parent directories are not changed.</p>
  2580. <p>If <em>exist_ok</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (the default), a <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> is
  2581. raised if the target directory already exists.</p>
  2582. <div class="admonition note">
  2583. <p class="admonition-title">Note</p>
  2584. <p><a class="reference internal" href="#os.makedirs" title="os.makedirs"><code class="xref py py-func docutils literal notranslate"><span class="pre">makedirs()</span></code></a> will become confused if the path elements to create
  2585. include <a class="reference internal" href="#os.pardir" title="os.pardir"><code class="xref py py-data docutils literal notranslate"><span class="pre">pardir</span></code></a> (eg. “..” on UNIX systems).</p>
  2586. </div>
  2587. <p>This function handles UNC paths correctly.</p>
  2588. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.mkdir</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">mode</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  2589. <div class="versionchanged">
  2590. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>exist_ok</em> parameter.</p>
  2591. </div>
  2592. <div class="versionchanged">
  2593. <p><span class="versionmodified changed">Changed in version 3.4.1: </span>Before Python 3.4.1, if <em>exist_ok</em> was <code class="docutils literal notranslate"><span class="pre">True</span></code> and the directory existed,
  2594. <a class="reference internal" href="#os.makedirs" title="os.makedirs"><code class="xref py py-func docutils literal notranslate"><span class="pre">makedirs()</span></code></a> would still raise an error if <em>mode</em> did not match the
  2595. mode of the existing directory. Since this behavior was impossible to
  2596. implement safely, it was removed in Python 3.4.1. See <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21082">bpo-21082</a>.</p>
  2597. </div>
  2598. <div class="versionchanged">
  2599. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2600. </div>
  2601. <div class="versionchanged">
  2602. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>mode</em> argument no longer affects the file permission bits of
  2603. newly created intermediate-level directories.</p>
  2604. </div>
  2605. </dd></dl>
  2606. <dl class="py function">
  2607. <dt class="sig sig-object py" id="os.mkfifo">
  2608. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">mkfifo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</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>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.mkfifo" title="Link to this definition">¶</a></dt>
  2609. <dd><p>Create a FIFO (a named pipe) named <em>path</em> with numeric mode <em>mode</em>.
  2610. The current umask value is first masked out from the mode.</p>
  2611. <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  2612. <p>FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
  2613. are deleted (for example with <a class="reference internal" href="#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.unlink()</span></code></a>). Generally, FIFOs are used as
  2614. rendezvous between “client” and “server” type processes: the server opens the
  2615. FIFO for reading, and the client opens it for writing. Note that <a class="reference internal" href="#os.mkfifo" title="os.mkfifo"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkfifo()</span></code></a>
  2616. doesn’t open the FIFO — it just creates the rendezvous point.</p>
  2617. <div class="availability docutils container">
  2618. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  2619. </div>
  2620. <div class="versionchanged">
  2621. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p>
  2622. </div>
  2623. <div class="versionchanged">
  2624. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2625. </div>
  2626. </dd></dl>
  2627. <dl class="py function">
  2628. <dt class="sig sig-object py" id="os.mknod">
  2629. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">mknod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</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">0o600</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">device</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.mknod" title="Link to this definition">¶</a></dt>
  2630. <dd><p>Create a filesystem node (file, device special file or named pipe) named
  2631. <em>path</em>. <em>mode</em> specifies both the permissions to use and the type of node
  2632. to be created, being combined (bitwise OR) with one of <code class="docutils literal notranslate"><span class="pre">stat.S_IFREG</span></code>,
  2633. <code class="docutils literal notranslate"><span class="pre">stat.S_IFCHR</span></code>, <code class="docutils literal notranslate"><span class="pre">stat.S_IFBLK</span></code>, and <code class="docutils literal notranslate"><span class="pre">stat.S_IFIFO</span></code> (those constants are
  2634. available in <a class="reference internal" href="stat.html#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code class="xref py py-mod docutils literal notranslate"><span class="pre">stat</span></code></a>). For <code class="docutils literal notranslate"><span class="pre">stat.S_IFCHR</span></code> and <code class="docutils literal notranslate"><span class="pre">stat.S_IFBLK</span></code>,
  2635. <em>device</em> defines the newly created device special file (probably using
  2636. <a class="reference internal" href="#os.makedev" title="os.makedev"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.makedev()</span></code></a>), otherwise it is ignored.</p>
  2637. <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  2638. <div class="availability docutils container">
  2639. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  2640. </div>
  2641. <div class="versionchanged">
  2642. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p>
  2643. </div>
  2644. <div class="versionchanged">
  2645. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2646. </div>
  2647. </dd></dl>
  2648. <dl class="py function">
  2649. <dt class="sig sig-object py" id="os.major">
  2650. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">major</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">device</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="#os.major" title="Link to this definition">¶</a></dt>
  2651. <dd><p>Extract the device major number from a raw device number (usually the
  2652. <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_dev</span></code> or <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_rdev</span></code> field from <code class="xref c c-struct docutils literal notranslate"><span class="pre">stat</span></code>).</p>
  2653. </dd></dl>
  2654. <dl class="py function">
  2655. <dt class="sig sig-object py" id="os.minor">
  2656. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">minor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">device</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="#os.minor" title="Link to this definition">¶</a></dt>
  2657. <dd><p>Extract the device minor number from a raw device number (usually the
  2658. <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_dev</span></code> or <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_rdev</span></code> field from <code class="xref c c-struct docutils literal notranslate"><span class="pre">stat</span></code>).</p>
  2659. </dd></dl>
  2660. <dl class="py function">
  2661. <dt class="sig sig-object py" id="os.makedev">
  2662. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">makedev</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">major</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">minor</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="#os.makedev" title="Link to this definition">¶</a></dt>
  2663. <dd><p>Compose a raw device number from the major and minor device numbers.</p>
  2664. </dd></dl>
  2665. <dl class="py function">
  2666. <dt class="sig sig-object py" id="os.pathconf">
  2667. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pathconf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.pathconf" title="Link to this definition">¶</a></dt>
  2668. <dd><p>Return system configuration information relevant to a named file. <em>name</em>
  2669. specifies the configuration value to retrieve; it may be a string which is the
  2670. name of a defined system value; these names are specified in a number of
  2671. standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
  2672. additional names as well. The names known to the host operating system are
  2673. given in the <code class="docutils literal notranslate"><span class="pre">pathconf_names</span></code> dictionary. For configuration variables not
  2674. included in that mapping, passing an integer for <em>name</em> is also accepted.</p>
  2675. <p>If <em>name</em> is a string and is not known, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. If a
  2676. specific value for <em>name</em> is not supported by the host system, even if it is
  2677. included in <code class="docutils literal notranslate"><span class="pre">pathconf_names</span></code>, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised with
  2678. <a class="reference internal" href="errno.html#errno.EINVAL" title="errno.EINVAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">errno.EINVAL</span></code></a> for the error number.</p>
  2679. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>.</p>
  2680. <div class="availability docutils container">
  2681. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  2682. </div>
  2683. <div class="versionchanged">
  2684. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2685. </div>
  2686. </dd></dl>
  2687. <dl class="py data">
  2688. <dt class="sig sig-object py" id="os.pathconf_names">
  2689. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pathconf_names</span></span><a class="headerlink" href="#os.pathconf_names" title="Link to this definition">¶</a></dt>
  2690. <dd><p>Dictionary mapping names accepted by <a class="reference internal" href="#os.pathconf" title="os.pathconf"><code class="xref py py-func docutils literal notranslate"><span class="pre">pathconf()</span></code></a> and <a class="reference internal" href="#os.fpathconf" title="os.fpathconf"><code class="xref py py-func docutils literal notranslate"><span class="pre">fpathconf()</span></code></a> to
  2691. the integer values defined for those names by the host operating system. This
  2692. can be used to determine the set of names known to the system.</p>
  2693. <div class="availability docutils container">
  2694. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  2695. </div>
  2696. </dd></dl>
  2697. <dl class="py function">
  2698. <dt class="sig sig-object py" id="os.readlink">
  2699. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">readlink</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.readlink" title="Link to this definition">¶</a></dt>
  2700. <dd><p>Return a string representing the path to which the symbolic link points. The
  2701. result may be either an absolute or relative pathname; if it is relative, it
  2702. may be converted to an absolute pathname using
  2703. <code class="docutils literal notranslate"><span class="pre">os.path.join(os.path.dirname(path),</span> <span class="pre">result)</span></code>.</p>
  2704. <p>If the <em>path</em> is a string object (directly or indirectly through a
  2705. <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathLike</span></code></a> interface), the result will also be a string object,
  2706. and the call may raise a UnicodeDecodeError. If the <em>path</em> is a bytes
  2707. object (direct or indirectly), the result will be a bytes object.</p>
  2708. <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  2709. <p>When trying to resolve a path that may contain links, use
  2710. <a class="reference internal" href="os.path.html#os.path.realpath" title="os.path.realpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">realpath()</span></code></a> to properly handle recursion and platform
  2711. differences.</p>
  2712. <div class="availability docutils container">
  2713. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  2714. </div>
  2715. <div class="versionchanged">
  2716. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added support for Windows 6.0 (Vista) symbolic links.</p>
  2717. </div>
  2718. <div class="versionchanged">
  2719. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p>
  2720. </div>
  2721. <div class="versionchanged">
  2722. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> on Unix.</p>
  2723. </div>
  2724. <div class="versionchanged">
  2725. <p><span class="versionmodified changed">Changed in version 3.8: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> and a bytes object on Windows.</p>
  2726. <p>Added support for directory junctions, and changed to return the
  2727. substitution path (which typically includes <code class="docutils literal notranslate"><span class="pre">\\?\</span></code> prefix) rather
  2728. than the optional “print name” field that was previously returned.</p>
  2729. </div>
  2730. </dd></dl>
  2731. <dl class="py function">
  2732. <dt class="sig sig-object py" id="os.remove">
  2733. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">remove</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.remove" title="Link to this definition">¶</a></dt>
  2734. <dd><p>Remove (delete) the file <em>path</em>. If <em>path</em> is a directory, an
  2735. <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised. Use <a class="reference internal" href="#os.rmdir" title="os.rmdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmdir()</span></code></a> to remove directories.
  2736. If the file does not exist, a <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> is raised.</p>
  2737. <p>This function can support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  2738. <p>On Windows, attempting to remove a file that is in use causes an exception to
  2739. be raised; on Unix, the directory entry is removed but the storage allocated
  2740. to the file is not made available until the original file is no longer in use.</p>
  2741. <p>This function is semantically identical to <a class="reference internal" href="#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">unlink()</span></code></a>.</p>
  2742. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.remove</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  2743. <div class="versionchanged">
  2744. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p>
  2745. </div>
  2746. <div class="versionchanged">
  2747. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2748. </div>
  2749. </dd></dl>
  2750. <dl class="py function">
  2751. <dt class="sig sig-object py" id="os.removedirs">
  2752. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">removedirs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.removedirs" title="Link to this definition">¶</a></dt>
  2753. <dd><p id="index-29">Remove directories recursively. Works like <a class="reference internal" href="#os.rmdir" title="os.rmdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmdir()</span></code></a> except that, if the
  2754. leaf directory is successfully removed, <a class="reference internal" href="#os.removedirs" title="os.removedirs"><code class="xref py py-func docutils literal notranslate"><span class="pre">removedirs()</span></code></a> tries to
  2755. successively remove every parent directory mentioned in <em>path</em> until an error
  2756. is raised (which is ignored, because it generally means that a parent directory
  2757. is not empty). For example, <code class="docutils literal notranslate"><span class="pre">os.removedirs('foo/bar/baz')</span></code> will first remove
  2758. the directory <code class="docutils literal notranslate"><span class="pre">'foo/bar/baz'</span></code>, and then remove <code class="docutils literal notranslate"><span class="pre">'foo/bar'</span></code> and <code class="docutils literal notranslate"><span class="pre">'foo'</span></code> if
  2759. they are empty. Raises <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if the leaf directory could not be
  2760. successfully removed.</p>
  2761. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.remove</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  2762. <div class="versionchanged">
  2763. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2764. </div>
  2765. </dd></dl>
  2766. <dl class="py function">
  2767. <dt class="sig sig-object py" id="os.rename">
  2768. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">rename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">src_dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst_dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.rename" title="Link to this definition">¶</a></dt>
  2769. <dd><p>Rename the file or directory <em>src</em> to <em>dst</em>. If <em>dst</em> exists, the operation
  2770. will fail with an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> subclass in a number of cases:</p>
  2771. <p>On Windows, if <em>dst</em> exists a <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> is always raised.
  2772. The operation may fail if <em>src</em> and <em>dst</em> are on different filesystems. Use
  2773. <a class="reference internal" href="shutil.html#shutil.move" title="shutil.move"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.move()</span></code></a> to support moves to a different filesystem.</p>
  2774. <p>On Unix, if <em>src</em> is a file and <em>dst</em> is a directory or vice-versa, an
  2775. <a class="reference internal" href="exceptions.html#IsADirectoryError" title="IsADirectoryError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IsADirectoryError</span></code></a> or a <a class="reference internal" href="exceptions.html#NotADirectoryError" title="NotADirectoryError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotADirectoryError</span></code></a> will be raised
  2776. respectively. If both are directories and <em>dst</em> is empty, <em>dst</em> will be
  2777. silently replaced. If <em>dst</em> is a non-empty directory, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>
  2778. is raised. If both are files, <em>dst</em> will be replaced silently if the user
  2779. has permission. The operation may fail on some Unix flavors if <em>src</em> and
  2780. <em>dst</em> are on different filesystems. If successful, the renaming will be an
  2781. atomic operation (this is a POSIX requirement).</p>
  2782. <p>This function can support specifying <em>src_dir_fd</em> and/or <em>dst_dir_fd</em> to
  2783. supply <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  2784. <p>If you want cross-platform overwriting of the destination, use <a class="reference internal" href="#os.replace" title="os.replace"><code class="xref py py-func docutils literal notranslate"><span class="pre">replace()</span></code></a>.</p>
  2785. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.rename</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>, <code class="docutils literal notranslate"><span class="pre">src_dir_fd</span></code>, <code class="docutils literal notranslate"><span class="pre">dst_dir_fd</span></code>.</p>
  2786. <div class="versionchanged">
  2787. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>src_dir_fd</em> and <em>dst_dir_fd</em> parameters.</p>
  2788. </div>
  2789. <div class="versionchanged">
  2790. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>src</em> and <em>dst</em>.</p>
  2791. </div>
  2792. </dd></dl>
  2793. <dl class="py function">
  2794. <dt class="sig sig-object py" id="os.renames">
  2795. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">renames</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">old</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.renames" title="Link to this definition">¶</a></dt>
  2796. <dd><p>Recursive directory or file renaming function. Works like <a class="reference internal" href="#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">rename()</span></code></a>, except
  2797. creation of any intermediate directories needed to make the new pathname good is
  2798. attempted first. After the rename, directories corresponding to rightmost path
  2799. segments of the old name will be pruned away using <a class="reference internal" href="#os.removedirs" title="os.removedirs"><code class="xref py py-func docutils literal notranslate"><span class="pre">removedirs()</span></code></a>.</p>
  2800. <div class="admonition note">
  2801. <p class="admonition-title">Note</p>
  2802. <p>This function can fail with the new directory structure made if you lack
  2803. permissions needed to remove the leaf directory or file.</p>
  2804. </div>
  2805. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.rename</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>, <code class="docutils literal notranslate"><span class="pre">src_dir_fd</span></code>, <code class="docutils literal notranslate"><span class="pre">dst_dir_fd</span></code>.</p>
  2806. <div class="versionchanged">
  2807. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>old</em> and <em>new</em>.</p>
  2808. </div>
  2809. </dd></dl>
  2810. <dl class="py function">
  2811. <dt class="sig sig-object py" id="os.replace">
  2812. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">replace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">src_dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst_dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.replace" title="Link to this definition">¶</a></dt>
  2813. <dd><p>Rename the file or directory <em>src</em> to <em>dst</em>. If <em>dst</em> is a non-empty directory,
  2814. <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> will be raised. If <em>dst</em> exists and is a file, it will
  2815. be replaced silently if the user has permission. The operation may fail
  2816. if <em>src</em> and <em>dst</em> are on different filesystems. If successful,
  2817. the renaming will be an atomic operation (this is a POSIX requirement).</p>
  2818. <p>This function can support specifying <em>src_dir_fd</em> and/or <em>dst_dir_fd</em> to
  2819. supply <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  2820. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.rename</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>, <code class="docutils literal notranslate"><span class="pre">src_dir_fd</span></code>, <code class="docutils literal notranslate"><span class="pre">dst_dir_fd</span></code>.</p>
  2821. <div class="versionadded">
  2822. <p><span class="versionmodified added">New in version 3.3.</span></p>
  2823. </div>
  2824. <div class="versionchanged">
  2825. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>src</em> and <em>dst</em>.</p>
  2826. </div>
  2827. </dd></dl>
  2828. <dl class="py function">
  2829. <dt class="sig sig-object py" id="os.rmdir">
  2830. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">rmdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.rmdir" title="Link to this definition">¶</a></dt>
  2831. <dd><p>Remove (delete) the directory <em>path</em>. If the directory does not exist or is
  2832. not empty, a <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> or an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised
  2833. respectively. In order to remove whole directory trees,
  2834. <a class="reference internal" href="shutil.html#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.rmtree()</span></code></a> can be used.</p>
  2835. <p>This function can support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  2836. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.rmdir</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  2837. <div class="versionchanged">
  2838. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p>
  2839. </div>
  2840. <div class="versionchanged">
  2841. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  2842. </div>
  2843. </dd></dl>
  2844. <dl class="py function">
  2845. <dt class="sig sig-object py" id="os.scandir">
  2846. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">scandir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'.'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.scandir" title="Link to this definition">¶</a></dt>
  2847. <dd><p>Return an iterator of <a class="reference internal" href="#os.DirEntry" title="os.DirEntry"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.DirEntry</span></code></a> objects corresponding to the
  2848. entries in the directory given by <em>path</em>. The entries are yielded in
  2849. arbitrary order, and the special entries <code class="docutils literal notranslate"><span class="pre">'.'</span></code> and <code class="docutils literal notranslate"><span class="pre">'..'</span></code> are not
  2850. included. If a file is removed from or added to the directory after
  2851. creating the iterator, whether an entry for that file be included is
  2852. unspecified.</p>
  2853. <p>Using <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> instead of <a class="reference internal" href="#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">listdir()</span></code></a> can significantly
  2854. increase the performance of code that also needs file type or file
  2855. attribute information, because <a class="reference internal" href="#os.DirEntry" title="os.DirEntry"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.DirEntry</span></code></a> objects expose this
  2856. information if the operating system provides it when scanning a directory.
  2857. All <a class="reference internal" href="#os.DirEntry" title="os.DirEntry"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.DirEntry</span></code></a> methods may perform a system call, but
  2858. <a class="reference internal" href="#os.DirEntry.is_dir" title="os.DirEntry.is_dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_dir()</span></code></a> and <a class="reference internal" href="#os.DirEntry.is_file" title="os.DirEntry.is_file"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_file()</span></code></a> usually only
  2859. require a system call for symbolic links; <a class="reference internal" href="#os.DirEntry.stat" title="os.DirEntry.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.DirEntry.stat()</span></code></a>
  2860. always requires a system call on Unix but only requires one for
  2861. symbolic links on Windows.</p>
  2862. <p><em>path</em> may be a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>. If <em>path</em> is of type <code class="docutils literal notranslate"><span class="pre">bytes</span></code>
  2863. (directly or indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathLike</span></code></a> interface),
  2864. the type of the <a class="reference internal" href="#os.DirEntry.name" title="os.DirEntry.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> and <a class="reference internal" href="#os.DirEntry.path" title="os.DirEntry.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>
  2865. attributes of each <a class="reference internal" href="#os.DirEntry" title="os.DirEntry"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.DirEntry</span></code></a> will be <code class="docutils literal notranslate"><span class="pre">bytes</span></code>; in all other
  2866. circumstances, they will be of type <code class="docutils literal notranslate"><span class="pre">str</span></code>.</p>
  2867. <p>This function can also support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>; the file descriptor must refer to a directory.</p>
  2868. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.scandir</span></code> with argument <code class="docutils literal notranslate"><span class="pre">path</span></code>.</p>
  2869. <p>The <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> iterator supports the <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> protocol
  2870. and has the following method:</p>
  2871. <dl class="py method">
  2872. <dt class="sig sig-object py" id="os.scandir.close">
  2873. <span class="sig-prename descclassname"><span class="pre">scandir.</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="#os.scandir.close" title="Link to this definition">¶</a></dt>
  2874. <dd><p>Close the iterator and free acquired resources.</p>
  2875. <p>This is called automatically when the iterator is exhausted or garbage
  2876. collected, or when an error happens during iterating. However it
  2877. is advisable to call it explicitly or 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>
  2878. statement.</p>
  2879. <div class="versionadded">
  2880. <p><span class="versionmodified added">New in version 3.6.</span></p>
  2881. </div>
  2882. </dd></dl>
  2883. <p>The following example shows a simple use of <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> to display all
  2884. the files (excluding directories) in the given <em>path</em> that don’t start with
  2885. <code class="docutils literal notranslate"><span class="pre">'.'</span></code>. The <code class="docutils literal notranslate"><span class="pre">entry.is_file()</span></code> call will generally not make an additional
  2886. system call:</p>
  2887. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">os</span><span class="o">.</span><span class="n">scandir</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="k">as</span> <span class="n">it</span><span class="p">:</span>
  2888. <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
  2889. <span class="k">if</span> <span class="ow">not</span> <span class="n">entry</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">entry</span><span class="o">.</span><span class="n">is_file</span><span class="p">():</span>
  2890. <span class="nb">print</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
  2891. </pre></div>
  2892. </div>
  2893. <div class="admonition note">
  2894. <p class="admonition-title">Note</p>
  2895. <p>On Unix-based systems, <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> uses the system’s
  2896. <a class="reference external" href="https://pubs.opengroup.org/onlinepubs/009695399/functions/opendir.html">opendir()</a>
  2897. and
  2898. <a class="reference external" href="https://pubs.opengroup.org/onlinepubs/009695399/functions/readdir_r.html">readdir()</a>
  2899. functions. On Windows, it uses the Win32
  2900. <a class="reference external" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx">FindFirstFileW</a>
  2901. and
  2902. <a class="reference external" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx">FindNextFileW</a>
  2903. functions.</p>
  2904. </div>
  2905. <div class="versionadded">
  2906. <p><span class="versionmodified added">New in version 3.5.</span></p>
  2907. </div>
  2908. <div class="versionchanged">
  2909. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added support for the <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> protocol and the
  2910. <a class="reference internal" href="#os.scandir.close" title="os.scandir.close"><code class="xref py py-func docutils literal notranslate"><span class="pre">close()</span></code></a> method. If a <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> iterator is neither
  2911. exhausted nor explicitly closed a <a class="reference internal" href="exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a> will be emitted
  2912. in its destructor.</p>
  2913. <p>The function 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>
  2914. </div>
  2915. <div class="versionchanged">
  2916. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added support for <a class="reference internal" href="#path-fd"><span class="std std-ref">file descriptors</span></a> on Unix.</p>
  2917. </div>
  2918. </dd></dl>
  2919. <dl class="py class">
  2920. <dt class="sig sig-object py" id="os.DirEntry">
  2921. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">DirEntry</span></span><a class="headerlink" href="#os.DirEntry" title="Link to this definition">¶</a></dt>
  2922. <dd><p>Object yielded by <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> to expose the file path and other file
  2923. attributes of a directory entry.</p>
  2924. <p><a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> will provide as much of this information as possible without
  2925. making additional system calls. When a <code class="docutils literal notranslate"><span class="pre">stat()</span></code> or <code class="docutils literal notranslate"><span class="pre">lstat()</span></code> system call
  2926. is made, the <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> object will cache the result.</p>
  2927. <p><code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> instances are not intended to be stored in long-lived data
  2928. structures; if you know the file metadata has changed or if a long time has
  2929. elapsed since calling <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a>, call <code class="docutils literal notranslate"><span class="pre">os.stat(entry.path)</span></code> to fetch
  2930. up-to-date information.</p>
  2931. <p>Because the <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> methods can make operating system calls, they may
  2932. also raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>. If you need very fine-grained
  2933. control over errors, you can catch <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> when calling one of the
  2934. <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> methods and handle as appropriate.</p>
  2935. <p>To be directly usable as a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>, <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code>
  2936. implements the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathLike</span></code></a> interface.</p>
  2937. <p>Attributes and methods on a <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> instance are as follows:</p>
  2938. <dl class="py attribute">
  2939. <dt class="sig sig-object py" id="os.DirEntry.name">
  2940. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#os.DirEntry.name" title="Link to this definition">¶</a></dt>
  2941. <dd><p>The entry’s base filename, relative to the <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> <em>path</em>
  2942. argument.</p>
  2943. <p>The <a class="reference internal" href="#os.name" title="os.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> attribute will be <code class="docutils literal notranslate"><span class="pre">bytes</span></code> if the <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a>
  2944. <em>path</em> argument is of type <code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">str</span></code> otherwise. Use
  2945. <a class="reference internal" href="#os.fsdecode" title="os.fsdecode"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsdecode()</span></code></a> to decode byte filenames.</p>
  2946. </dd></dl>
  2947. <dl class="py attribute">
  2948. <dt class="sig sig-object py" id="os.DirEntry.path">
  2949. <span class="sig-name descname"><span class="pre">path</span></span><a class="headerlink" href="#os.DirEntry.path" title="Link to this definition">¶</a></dt>
  2950. <dd><p>The entry’s full path name: equivalent to <code class="docutils literal notranslate"><span class="pre">os.path.join(scandir_path,</span>
  2951. <span class="pre">entry.name)</span></code> where <em>scandir_path</em> is the <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> <em>path</em>
  2952. argument. The path is only absolute if the <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> <em>path</em>
  2953. argument was absolute. If the <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> <em>path</em>
  2954. argument was a <a class="reference internal" href="#path-fd"><span class="std std-ref">file descriptor</span></a>, the <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>
  2955. attribute is the same as the <a class="reference internal" href="#os.name" title="os.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> attribute.</p>
  2956. <p>The <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a> attribute will be <code class="docutils literal notranslate"><span class="pre">bytes</span></code> if the <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a>
  2957. <em>path</em> argument is of type <code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">str</span></code> otherwise. Use
  2958. <a class="reference internal" href="#os.fsdecode" title="os.fsdecode"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsdecode()</span></code></a> to decode byte filenames.</p>
  2959. </dd></dl>
  2960. <dl class="py method">
  2961. <dt class="sig sig-object py" id="os.DirEntry.inode">
  2962. <span class="sig-name descname"><span class="pre">inode</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.DirEntry.inode" title="Link to this definition">¶</a></dt>
  2963. <dd><p>Return the inode number of the entry.</p>
  2964. <p>The result is cached on the <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> object. Use
  2965. <code class="docutils literal notranslate"><span class="pre">os.stat(entry.path,</span> <span class="pre">follow_symlinks=False).st_ino</span></code> to fetch up-to-date
  2966. information.</p>
  2967. <p>On the first, uncached call, a system call is required on Windows but
  2968. not on Unix.</p>
  2969. </dd></dl>
  2970. <dl class="py method">
  2971. <dt class="sig sig-object py" id="os.DirEntry.is_dir">
  2972. <span class="sig-name descname"><span class="pre">is_dir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.DirEntry.is_dir" title="Link to this definition">¶</a></dt>
  2973. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if this entry is a directory or a symbolic link pointing
  2974. to a directory; return <code class="docutils literal notranslate"><span class="pre">False</span></code> if the entry is or points to any other
  2975. kind of file, or if it doesn’t exist anymore.</p>
  2976. <p>If <em>follow_symlinks</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, return <code class="docutils literal notranslate"><span class="pre">True</span></code> only if this entry
  2977. is a directory (without following symlinks); return <code class="docutils literal notranslate"><span class="pre">False</span></code> if the
  2978. entry is any other kind of file or if it doesn’t exist anymore.</p>
  2979. <p>The result is cached on the <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> object, with a separate cache
  2980. for <em>follow_symlinks</em> <code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">False</span></code>. Call <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> along
  2981. with <a class="reference internal" href="stat.html#stat.S_ISDIR" title="stat.S_ISDIR"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat.S_ISDIR()</span></code></a> to fetch up-to-date information.</p>
  2982. <p>On the first, uncached call, no system call is required in most cases.
  2983. Specifically, for non-symlinks, neither Windows or Unix require a system
  2984. call, except on certain Unix file systems, such as network file systems,
  2985. that return <code class="docutils literal notranslate"><span class="pre">dirent.d_type</span> <span class="pre">==</span> <span class="pre">DT_UNKNOWN</span></code>. If the entry is a symlink,
  2986. a system call will be required to follow the symlink unless
  2987. <em>follow_symlinks</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  2988. <p>This method can raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, such as <a class="reference internal" href="exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a>,
  2989. but <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> is caught and not raised.</p>
  2990. </dd></dl>
  2991. <dl class="py method">
  2992. <dt class="sig sig-object py" id="os.DirEntry.is_file">
  2993. <span class="sig-name descname"><span class="pre">is_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.DirEntry.is_file" title="Link to this definition">¶</a></dt>
  2994. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if this entry is a file or a symbolic link pointing to a
  2995. file; return <code class="docutils literal notranslate"><span class="pre">False</span></code> if the entry is or points to a directory or other
  2996. non-file entry, or if it doesn’t exist anymore.</p>
  2997. <p>If <em>follow_symlinks</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, return <code class="docutils literal notranslate"><span class="pre">True</span></code> only if this entry
  2998. is a file (without following symlinks); return <code class="docutils literal notranslate"><span class="pre">False</span></code> if the entry is
  2999. a directory or other non-file entry, or if it doesn’t exist anymore.</p>
  3000. <p>The result is cached on the <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> object. Caching, system calls
  3001. made, and exceptions raised are as per <a class="reference internal" href="#os.DirEntry.is_dir" title="os.DirEntry.is_dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_dir()</span></code></a>.</p>
  3002. </dd></dl>
  3003. <dl class="py method">
  3004. <dt class="sig sig-object py" id="os.DirEntry.is_symlink">
  3005. <span class="sig-name descname"><span class="pre">is_symlink</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.DirEntry.is_symlink" title="Link to this definition">¶</a></dt>
  3006. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if this entry is a symbolic link (even if broken);
  3007. return <code class="docutils literal notranslate"><span class="pre">False</span></code> if the entry points to a directory or any kind of file,
  3008. or if it doesn’t exist anymore.</p>
  3009. <p>The result is cached on the <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> object. Call
  3010. <a class="reference internal" href="os.path.html#os.path.islink" title="os.path.islink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.islink()</span></code></a> to fetch up-to-date information.</p>
  3011. <p>On the first, uncached call, no system call is required in most cases.
  3012. Specifically, neither Windows or Unix require a system call, except on
  3013. certain Unix file systems, such as network file systems, that return
  3014. <code class="docutils literal notranslate"><span class="pre">dirent.d_type</span> <span class="pre">==</span> <span class="pre">DT_UNKNOWN</span></code>.</p>
  3015. <p>This method can raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, such as <a class="reference internal" href="exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a>,
  3016. but <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> is caught and not raised.</p>
  3017. </dd></dl>
  3018. <dl class="py method">
  3019. <dt class="sig sig-object py" id="os.DirEntry.is_junction">
  3020. <span class="sig-name descname"><span class="pre">is_junction</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.DirEntry.is_junction" title="Link to this definition">¶</a></dt>
  3021. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if this entry is a junction (even if broken);
  3022. return <code class="docutils literal notranslate"><span class="pre">False</span></code> if the entry points to a regular directory, any kind
  3023. of file, a symlink, or if it doesn’t exist anymore.</p>
  3024. <p>The result is cached on the <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> object. Call
  3025. <a class="reference internal" href="os.path.html#os.path.isjunction" title="os.path.isjunction"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.isjunction()</span></code></a> to fetch up-to-date information.</p>
  3026. <div class="versionadded">
  3027. <p><span class="versionmodified added">New in version 3.12.</span></p>
  3028. </div>
  3029. </dd></dl>
  3030. <dl class="py method">
  3031. <dt class="sig sig-object py" id="os.DirEntry.stat">
  3032. <span class="sig-name descname"><span class="pre">stat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.DirEntry.stat" title="Link to this definition">¶</a></dt>
  3033. <dd><p>Return a <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code class="xref py py-class docutils literal notranslate"><span class="pre">stat_result</span></code></a> object for this entry. This method
  3034. follows symbolic links by default; to stat a symbolic link add the
  3035. <code class="docutils literal notranslate"><span class="pre">follow_symlinks=False</span></code> argument.</p>
  3036. <p>On Unix, this method always requires a system call. On Windows, it
  3037. only requires a system call if <em>follow_symlinks</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> and the
  3038. entry is a reparse point (for example, a symbolic link or directory
  3039. junction).</p>
  3040. <p>On Windows, the <code class="docutils literal notranslate"><span class="pre">st_ino</span></code>, <code class="docutils literal notranslate"><span class="pre">st_dev</span></code> and <code class="docutils literal notranslate"><span class="pre">st_nlink</span></code> attributes of the
  3041. <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code class="xref py py-class docutils literal notranslate"><span class="pre">stat_result</span></code></a> are always set to zero. Call <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> to
  3042. get these attributes.</p>
  3043. <p>The result is cached on the <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> object, with a separate cache
  3044. for <em>follow_symlinks</em> <code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">False</span></code>. Call <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> to
  3045. fetch up-to-date information.</p>
  3046. </dd></dl>
  3047. <p>Note that there is a nice correspondence between several attributes
  3048. and methods of <code class="docutils literal notranslate"><span class="pre">os.DirEntry</span></code> and of <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">pathlib.Path</span></code></a>. In
  3049. particular, the <code class="docutils literal notranslate"><span class="pre">name</span></code> attribute has the same
  3050. meaning, as do the <code class="docutils literal notranslate"><span class="pre">is_dir()</span></code>, <code class="docutils literal notranslate"><span class="pre">is_file()</span></code>, <code class="docutils literal notranslate"><span class="pre">is_symlink()</span></code>,
  3051. <code class="docutils literal notranslate"><span class="pre">is_junction()</span></code>, and <code class="docutils literal notranslate"><span class="pre">stat()</span></code> methods.</p>
  3052. <div class="versionadded">
  3053. <p><span class="versionmodified added">New in version 3.5.</span></p>
  3054. </div>
  3055. <div class="versionchanged">
  3056. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added support for the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathLike</span></code></a> interface. Added support
  3057. for <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> paths on Windows.</p>
  3058. </div>
  3059. <div class="versionchanged">
  3060. <p><span class="versionmodified changed">Changed in version 3.12: </span>The <code class="docutils literal notranslate"><span class="pre">st_ctime</span></code> attribute of a stat result is deprecated on Windows.
  3061. The file creation time is properly available as <code class="docutils literal notranslate"><span class="pre">st_birthtime</span></code>, and
  3062. in the future <code class="docutils literal notranslate"><span class="pre">st_ctime</span></code> may be changed to return zero or the
  3063. metadata change time, if available.</p>
  3064. </div>
  3065. </dd></dl>
  3066. <dl class="py function">
  3067. <dt class="sig sig-object py" id="os.stat">
  3068. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">stat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.stat" title="Link to this definition">¶</a></dt>
  3069. <dd><p>Get the status of a file or a file descriptor. Perform the equivalent of a
  3070. <code class="xref c c-func docutils literal notranslate"><span class="pre">stat()</span></code> system call on the given path. <em>path</em> may be specified as
  3071. either a string or bytes – directly or indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathLike</span></code></a>
  3072. interface – or as an open file descriptor. Return a <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code class="xref py py-class docutils literal notranslate"><span class="pre">stat_result</span></code></a>
  3073. object.</p>
  3074. <p>This function normally follows symlinks; to stat a symlink add the argument
  3075. <code class="docutils literal notranslate"><span class="pre">follow_symlinks=False</span></code>, or use <a class="reference internal" href="#os.lstat" title="os.lstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">lstat()</span></code></a>.</p>
  3076. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and
  3077. <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p>
  3078. <p>On Windows, passing <code class="docutils literal notranslate"><span class="pre">follow_symlinks=False</span></code> will disable following all
  3079. name-surrogate reparse points, which includes symlinks and directory
  3080. junctions. Other types of reparse points that do not resemble links or that
  3081. the operating system is unable to follow will be opened directly. When
  3082. following a chain of multiple links, this may result in the original link
  3083. being returned instead of the non-link that prevented full traversal. To
  3084. obtain stat results for the final path in this case, use the
  3085. <a class="reference internal" href="os.path.html#os.path.realpath" title="os.path.realpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.realpath()</span></code></a> function to resolve the path name as far as
  3086. possible and call <a class="reference internal" href="#os.lstat" title="os.lstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">lstat()</span></code></a> on the result. This does not apply to
  3087. dangling symlinks or junction points, which will raise the usual exceptions.</p>
  3088. <p id="index-30">Example:</p>
  3089. <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">os</span>
  3090. <span class="gp">&gt;&gt;&gt; </span><span class="n">statinfo</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="s1">&#39;somefile.txt&#39;</span><span class="p">)</span>
  3091. <span class="gp">&gt;&gt;&gt; </span><span class="n">statinfo</span>
  3092. <span class="go">os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026,</span>
  3093. <span class="go">st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295,</span>
  3094. <span class="go">st_mtime=1297230027, st_ctime=1297230027)</span>
  3095. <span class="gp">&gt;&gt;&gt; </span><span class="n">statinfo</span><span class="o">.</span><span class="n">st_size</span>
  3096. <span class="go">264</span>
  3097. </pre></div>
  3098. </div>
  3099. <div class="admonition seealso">
  3100. <p class="admonition-title">See also</p>
  3101. <p><a class="reference internal" href="#os.fstat" title="os.fstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">fstat()</span></code></a> and <a class="reference internal" href="#os.lstat" title="os.lstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">lstat()</span></code></a> functions.</p>
  3102. </div>
  3103. <div class="versionchanged">
  3104. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> and <em>follow_symlinks</em> parameters,
  3105. specifying a file descriptor instead of a path.</p>
  3106. </div>
  3107. <div class="versionchanged">
  3108. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  3109. </div>
  3110. <div class="versionchanged">
  3111. <p><span class="versionmodified changed">Changed in version 3.8: </span>On Windows, all reparse points that can be resolved by the operating
  3112. system are now followed, and passing <code class="docutils literal notranslate"><span class="pre">follow_symlinks=False</span></code>
  3113. disables following all name surrogate reparse points. If the operating
  3114. system reaches a reparse point that it is not able to follow, <em>stat</em> now
  3115. returns the information for the original path as if
  3116. <code class="docutils literal notranslate"><span class="pre">follow_symlinks=False</span></code> had been specified instead of raising an error.</p>
  3117. </div>
  3118. </dd></dl>
  3119. <dl class="py class">
  3120. <dt class="sig sig-object py" id="os.stat_result">
  3121. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">stat_result</span></span><a class="headerlink" href="#os.stat_result" title="Link to this definition">¶</a></dt>
  3122. <dd><p>Object whose attributes correspond roughly to the members of the
  3123. <code class="xref c c-struct docutils literal notranslate"><span class="pre">stat</span></code> structure. It is used for the result of <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a>,
  3124. <a class="reference internal" href="#os.fstat" title="os.fstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fstat()</span></code></a> and <a class="reference internal" href="#os.lstat" title="os.lstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.lstat()</span></code></a>.</p>
  3125. <p>Attributes:</p>
  3126. <dl class="py attribute">
  3127. <dt class="sig sig-object py" id="os.stat_result.st_mode">
  3128. <span class="sig-name descname"><span class="pre">st_mode</span></span><a class="headerlink" href="#os.stat_result.st_mode" title="Link to this definition">¶</a></dt>
  3129. <dd><p>File mode: file type and file mode bits (permissions).</p>
  3130. </dd></dl>
  3131. <dl class="py attribute">
  3132. <dt class="sig sig-object py" id="os.stat_result.st_ino">
  3133. <span class="sig-name descname"><span class="pre">st_ino</span></span><a class="headerlink" href="#os.stat_result.st_ino" title="Link to this definition">¶</a></dt>
  3134. <dd><p>Platform dependent, but if non-zero, uniquely identifies the
  3135. file for a given value of <code class="docutils literal notranslate"><span class="pre">st_dev</span></code>. Typically:</p>
  3136. <ul class="simple">
  3137. <li><p>the inode number on Unix,</p></li>
  3138. <li><p>the <a class="reference external" href="https://msdn.microsoft.com/en-us/library/aa363788">file index</a> on
  3139. Windows</p></li>
  3140. </ul>
  3141. </dd></dl>
  3142. <dl class="py attribute">
  3143. <dt class="sig sig-object py" id="os.stat_result.st_dev">
  3144. <span class="sig-name descname"><span class="pre">st_dev</span></span><a class="headerlink" href="#os.stat_result.st_dev" title="Link to this definition">¶</a></dt>
  3145. <dd><p>Identifier of the device on which this file resides.</p>
  3146. </dd></dl>
  3147. <dl class="py attribute">
  3148. <dt class="sig sig-object py" id="os.stat_result.st_nlink">
  3149. <span class="sig-name descname"><span class="pre">st_nlink</span></span><a class="headerlink" href="#os.stat_result.st_nlink" title="Link to this definition">¶</a></dt>
  3150. <dd><p>Number of hard links.</p>
  3151. </dd></dl>
  3152. <dl class="py attribute">
  3153. <dt class="sig sig-object py" id="os.stat_result.st_uid">
  3154. <span class="sig-name descname"><span class="pre">st_uid</span></span><a class="headerlink" href="#os.stat_result.st_uid" title="Link to this definition">¶</a></dt>
  3155. <dd><p>User identifier of the file owner.</p>
  3156. </dd></dl>
  3157. <dl class="py attribute">
  3158. <dt class="sig sig-object py" id="os.stat_result.st_gid">
  3159. <span class="sig-name descname"><span class="pre">st_gid</span></span><a class="headerlink" href="#os.stat_result.st_gid" title="Link to this definition">¶</a></dt>
  3160. <dd><p>Group identifier of the file owner.</p>
  3161. </dd></dl>
  3162. <dl class="py attribute">
  3163. <dt class="sig sig-object py" id="os.stat_result.st_size">
  3164. <span class="sig-name descname"><span class="pre">st_size</span></span><a class="headerlink" href="#os.stat_result.st_size" title="Link to this definition">¶</a></dt>
  3165. <dd><p>Size of the file in bytes, if it is a regular file or a symbolic link.
  3166. The size of a symbolic link is the length of the pathname it contains,
  3167. without a terminating null byte.</p>
  3168. </dd></dl>
  3169. <p>Timestamps:</p>
  3170. <dl class="py attribute">
  3171. <dt class="sig sig-object py" id="os.stat_result.st_atime">
  3172. <span class="sig-name descname"><span class="pre">st_atime</span></span><a class="headerlink" href="#os.stat_result.st_atime" title="Link to this definition">¶</a></dt>
  3173. <dd><p>Time of most recent access expressed in seconds.</p>
  3174. </dd></dl>
  3175. <dl class="py attribute">
  3176. <dt class="sig sig-object py" id="os.stat_result.st_mtime">
  3177. <span class="sig-name descname"><span class="pre">st_mtime</span></span><a class="headerlink" href="#os.stat_result.st_mtime" title="Link to this definition">¶</a></dt>
  3178. <dd><p>Time of most recent content modification expressed in seconds.</p>
  3179. </dd></dl>
  3180. <dl class="py attribute">
  3181. <dt class="sig sig-object py" id="os.stat_result.st_ctime">
  3182. <span class="sig-name descname"><span class="pre">st_ctime</span></span><a class="headerlink" href="#os.stat_result.st_ctime" title="Link to this definition">¶</a></dt>
  3183. <dd><p>Time of most recent metadata change expressed in seconds.</p>
  3184. <div class="versionchanged">
  3185. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">st_ctime</span></code> is deprecated on Windows. Use <code class="docutils literal notranslate"><span class="pre">st_birthtime</span></code> for
  3186. the file creation time. In the future, <code class="docutils literal notranslate"><span class="pre">st_ctime</span></code> will contain
  3187. the time of the most recent metadata change, as for other platforms.</p>
  3188. </div>
  3189. </dd></dl>
  3190. <dl class="py attribute">
  3191. <dt class="sig sig-object py" id="os.stat_result.st_atime_ns">
  3192. <span class="sig-name descname"><span class="pre">st_atime_ns</span></span><a class="headerlink" href="#os.stat_result.st_atime_ns" title="Link to this definition">¶</a></dt>
  3193. <dd><p>Time of most recent access expressed in nanoseconds as an integer.</p>
  3194. <div class="versionadded">
  3195. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3196. </div>
  3197. </dd></dl>
  3198. <dl class="py attribute">
  3199. <dt class="sig sig-object py" id="os.stat_result.st_mtime_ns">
  3200. <span class="sig-name descname"><span class="pre">st_mtime_ns</span></span><a class="headerlink" href="#os.stat_result.st_mtime_ns" title="Link to this definition">¶</a></dt>
  3201. <dd><p>Time of most recent content modification expressed in nanoseconds as an
  3202. integer.</p>
  3203. <div class="versionadded">
  3204. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3205. </div>
  3206. </dd></dl>
  3207. <dl class="py attribute">
  3208. <dt class="sig sig-object py" id="os.stat_result.st_ctime_ns">
  3209. <span class="sig-name descname"><span class="pre">st_ctime_ns</span></span><a class="headerlink" href="#os.stat_result.st_ctime_ns" title="Link to this definition">¶</a></dt>
  3210. <dd><p>Time of most recent metadata change expressed in nanoseconds as an
  3211. integer.</p>
  3212. <div class="versionadded">
  3213. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3214. </div>
  3215. <div class="versionchanged">
  3216. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">st_ctime_ns</span></code> is deprecated on Windows. Use <code class="docutils literal notranslate"><span class="pre">st_birthtime_ns</span></code>
  3217. for the file creation time. In the future, <code class="docutils literal notranslate"><span class="pre">st_ctime</span></code> will contain
  3218. the time of the most recent metadata change, as for other platforms.</p>
  3219. </div>
  3220. </dd></dl>
  3221. <dl class="py attribute">
  3222. <dt class="sig sig-object py" id="os.stat_result.st_birthtime">
  3223. <span class="sig-name descname"><span class="pre">st_birthtime</span></span><a class="headerlink" href="#os.stat_result.st_birthtime" title="Link to this definition">¶</a></dt>
  3224. <dd><p>Time of file creation expressed in seconds. This attribute is not
  3225. always available, and may raise <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>.</p>
  3226. <div class="versionchanged">
  3227. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">st_birthtime</span></code> is now available on Windows.</p>
  3228. </div>
  3229. </dd></dl>
  3230. <dl class="py attribute">
  3231. <dt class="sig sig-object py" id="os.stat_result.st_birthtime_ns">
  3232. <span class="sig-name descname"><span class="pre">st_birthtime_ns</span></span><a class="headerlink" href="#os.stat_result.st_birthtime_ns" title="Link to this definition">¶</a></dt>
  3233. <dd><p>Time of file creation expressed in nanoseconds as an integer.
  3234. This attribute is not always available, and may raise
  3235. <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>.</p>
  3236. <div class="versionadded">
  3237. <p><span class="versionmodified added">New in version 3.12.</span></p>
  3238. </div>
  3239. </dd></dl>
  3240. <div class="admonition note">
  3241. <p class="admonition-title">Note</p>
  3242. <p>The exact meaning and resolution of the <a class="reference internal" href="#os.stat_result.st_atime" title="os.stat_result.st_atime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime</span></code></a>,
  3243. <a class="reference internal" href="#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime</span></code></a>, <a class="reference internal" href="#os.stat_result.st_ctime" title="os.stat_result.st_ctime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ctime</span></code></a> and <a class="reference internal" href="#os.stat_result.st_birthtime" title="os.stat_result.st_birthtime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_birthtime</span></code></a> attributes
  3244. depend on the operating system and the file system. For example, on
  3245. Windows systems using the FAT32 file systems, <a class="reference internal" href="#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime</span></code></a> has
  3246. 2-second resolution, and <a class="reference internal" href="#os.stat_result.st_atime" title="os.stat_result.st_atime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime</span></code></a> has only 1-day resolution.
  3247. See your operating system documentation for details.</p>
  3248. <p>Similarly, although <a class="reference internal" href="#os.stat_result.st_atime_ns" title="os.stat_result.st_atime_ns"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime_ns</span></code></a>, <a class="reference internal" href="#os.stat_result.st_mtime_ns" title="os.stat_result.st_mtime_ns"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime_ns</span></code></a>,
  3249. <a class="reference internal" href="#os.stat_result.st_ctime_ns" title="os.stat_result.st_ctime_ns"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ctime_ns</span></code></a> and <a class="reference internal" href="#os.stat_result.st_birthtime_ns" title="os.stat_result.st_birthtime_ns"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_birthtime_ns</span></code></a> are always expressed in
  3250. nanoseconds, many systems do not provide nanosecond precision. On
  3251. systems that do provide nanosecond precision, the floating-point object
  3252. used to store <a class="reference internal" href="#os.stat_result.st_atime" title="os.stat_result.st_atime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime</span></code></a>, <a class="reference internal" href="#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime</span></code></a>, <a class="reference internal" href="#os.stat_result.st_ctime" title="os.stat_result.st_ctime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ctime</span></code></a> and
  3253. <a class="reference internal" href="#os.stat_result.st_birthtime" title="os.stat_result.st_birthtime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_birthtime</span></code></a> cannot preserve all of it, and as such will be
  3254. slightly inexact. If you need the exact timestamps you should always use
  3255. <a class="reference internal" href="#os.stat_result.st_atime_ns" title="os.stat_result.st_atime_ns"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime_ns</span></code></a>, <a class="reference internal" href="#os.stat_result.st_mtime_ns" title="os.stat_result.st_mtime_ns"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime_ns</span></code></a>, <a class="reference internal" href="#os.stat_result.st_ctime_ns" title="os.stat_result.st_ctime_ns"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ctime_ns</span></code></a> and
  3256. <a class="reference internal" href="#os.stat_result.st_birthtime_ns" title="os.stat_result.st_birthtime_ns"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_birthtime_ns</span></code></a>.</p>
  3257. </div>
  3258. <p>On some Unix systems (such as Linux), the following attributes may also be
  3259. available:</p>
  3260. <dl class="py attribute">
  3261. <dt class="sig sig-object py" id="os.stat_result.st_blocks">
  3262. <span class="sig-name descname"><span class="pre">st_blocks</span></span><a class="headerlink" href="#os.stat_result.st_blocks" title="Link to this definition">¶</a></dt>
  3263. <dd><p>Number of 512-byte blocks allocated for file.
  3264. This may be smaller than <a class="reference internal" href="#os.stat_result.st_size" title="os.stat_result.st_size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_size</span></code></a>/512 when the file has holes.</p>
  3265. </dd></dl>
  3266. <dl class="py attribute">
  3267. <dt class="sig sig-object py" id="os.stat_result.st_blksize">
  3268. <span class="sig-name descname"><span class="pre">st_blksize</span></span><a class="headerlink" href="#os.stat_result.st_blksize" title="Link to this definition">¶</a></dt>
  3269. <dd><p>“Preferred” blocksize for efficient file system I/O. Writing to a file in
  3270. smaller chunks may cause an inefficient read-modify-rewrite.</p>
  3271. </dd></dl>
  3272. <dl class="py attribute">
  3273. <dt class="sig sig-object py" id="os.stat_result.st_rdev">
  3274. <span class="sig-name descname"><span class="pre">st_rdev</span></span><a class="headerlink" href="#os.stat_result.st_rdev" title="Link to this definition">¶</a></dt>
  3275. <dd><p>Type of device if an inode device.</p>
  3276. </dd></dl>
  3277. <dl class="py attribute">
  3278. <dt class="sig sig-object py" id="os.stat_result.st_flags">
  3279. <span class="sig-name descname"><span class="pre">st_flags</span></span><a class="headerlink" href="#os.stat_result.st_flags" title="Link to this definition">¶</a></dt>
  3280. <dd><p>User defined flags for file.</p>
  3281. </dd></dl>
  3282. <p>On other Unix systems (such as FreeBSD), the following attributes may be
  3283. available (but may be only filled out if root tries to use them):</p>
  3284. <dl class="py attribute">
  3285. <dt class="sig sig-object py" id="os.stat_result.st_gen">
  3286. <span class="sig-name descname"><span class="pre">st_gen</span></span><a class="headerlink" href="#os.stat_result.st_gen" title="Link to this definition">¶</a></dt>
  3287. <dd><p>File generation number.</p>
  3288. </dd></dl>
  3289. <p>On Solaris and derivatives, the following attributes may also be
  3290. available:</p>
  3291. <dl class="py attribute">
  3292. <dt class="sig sig-object py" id="os.stat_result.st_fstype">
  3293. <span class="sig-name descname"><span class="pre">st_fstype</span></span><a class="headerlink" href="#os.stat_result.st_fstype" title="Link to this definition">¶</a></dt>
  3294. <dd><p>String that uniquely identifies the type of the filesystem that
  3295. contains the file.</p>
  3296. </dd></dl>
  3297. <p>On macOS systems, the following attributes may also be available:</p>
  3298. <dl class="py attribute">
  3299. <dt class="sig sig-object py" id="os.stat_result.st_rsize">
  3300. <span class="sig-name descname"><span class="pre">st_rsize</span></span><a class="headerlink" href="#os.stat_result.st_rsize" title="Link to this definition">¶</a></dt>
  3301. <dd><p>Real size of the file.</p>
  3302. </dd></dl>
  3303. <dl class="py attribute">
  3304. <dt class="sig sig-object py" id="os.stat_result.st_creator">
  3305. <span class="sig-name descname"><span class="pre">st_creator</span></span><a class="headerlink" href="#os.stat_result.st_creator" title="Link to this definition">¶</a></dt>
  3306. <dd><p>Creator of the file.</p>
  3307. </dd></dl>
  3308. <dl class="py attribute">
  3309. <dt class="sig sig-object py" id="os.stat_result.st_type">
  3310. <span class="sig-name descname"><span class="pre">st_type</span></span><a class="headerlink" href="#os.stat_result.st_type" title="Link to this definition">¶</a></dt>
  3311. <dd><p>File type.</p>
  3312. </dd></dl>
  3313. <p>On Windows systems, the following attributes are also available:</p>
  3314. <dl class="py attribute">
  3315. <dt class="sig sig-object py" id="os.stat_result.st_file_attributes">
  3316. <span class="sig-name descname"><span class="pre">st_file_attributes</span></span><a class="headerlink" href="#os.stat_result.st_file_attributes" title="Link to this definition">¶</a></dt>
  3317. <dd><p>Windows file attributes: <code class="docutils literal notranslate"><span class="pre">dwFileAttributes</span></code> member of the
  3318. <code class="docutils literal notranslate"><span class="pre">BY_HANDLE_FILE_INFORMATION</span></code> structure returned by
  3319. <code class="xref c c-func docutils literal notranslate"><span class="pre">GetFileInformationByHandle()</span></code>.
  3320. See the <code class="xref py py-const docutils literal notranslate"><span class="pre">FILE_ATTRIBUTE_*</span> <span class="pre">&lt;stat.FILE_ATTRIBUTE_ARCHIVE&gt;</span></code>
  3321. constants in the <a class="reference internal" href="stat.html#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code class="xref py py-mod docutils literal notranslate"><span class="pre">stat</span></code></a> module.</p>
  3322. <div class="versionadded">
  3323. <p><span class="versionmodified added">New in version 3.5.</span></p>
  3324. </div>
  3325. </dd></dl>
  3326. <dl class="py attribute">
  3327. <dt class="sig sig-object py" id="os.stat_result.st_reparse_tag">
  3328. <span class="sig-name descname"><span class="pre">st_reparse_tag</span></span><a class="headerlink" href="#os.stat_result.st_reparse_tag" title="Link to this definition">¶</a></dt>
  3329. <dd><p>When <a class="reference internal" href="#os.stat_result.st_file_attributes" title="os.stat_result.st_file_attributes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_file_attributes</span></code></a> has the <a class="reference internal" href="stat.html#stat.FILE_ATTRIBUTE_REPARSE_POINT" title="stat.FILE_ATTRIBUTE_REPARSE_POINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">FILE_ATTRIBUTE_REPARSE_POINT</span></code></a>
  3330. set, this field contains the tag identifying the type of reparse point.
  3331. See the <a class="reference internal" href="stat.html#stat.IO_REPARSE_TAG_SYMLINK" title="stat.IO_REPARSE_TAG_SYMLINK"><code class="xref py py-const docutils literal notranslate"><span class="pre">IO_REPARSE_TAG_*</span></code></a>
  3332. constants in the <a class="reference internal" href="stat.html#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code class="xref py py-mod docutils literal notranslate"><span class="pre">stat</span></code></a> module.</p>
  3333. </dd></dl>
  3334. <p>The standard module <a class="reference internal" href="stat.html#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code class="xref py py-mod docutils literal notranslate"><span class="pre">stat</span></code></a> defines functions and constants that are
  3335. useful for extracting information from a <code class="xref c c-struct docutils literal notranslate"><span class="pre">stat</span></code> structure. (On
  3336. Windows, some items are filled with dummy values.)</p>
  3337. <p>For backward compatibility, a <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code class="xref py py-class docutils literal notranslate"><span class="pre">stat_result</span></code></a> instance is also
  3338. accessible as a tuple of at least 10 integers giving the most important (and
  3339. portable) members of the <code class="xref c c-struct docutils literal notranslate"><span class="pre">stat</span></code> structure, in the order
  3340. <a class="reference internal" href="#os.stat_result.st_mode" title="os.stat_result.st_mode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mode</span></code></a>, <a class="reference internal" href="#os.stat_result.st_ino" title="os.stat_result.st_ino"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ino</span></code></a>, <a class="reference internal" href="#os.stat_result.st_dev" title="os.stat_result.st_dev"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_dev</span></code></a>, <a class="reference internal" href="#os.stat_result.st_nlink" title="os.stat_result.st_nlink"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_nlink</span></code></a>,
  3341. <a class="reference internal" href="#os.stat_result.st_uid" title="os.stat_result.st_uid"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_uid</span></code></a>, <a class="reference internal" href="#os.stat_result.st_gid" title="os.stat_result.st_gid"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_gid</span></code></a>, <a class="reference internal" href="#os.stat_result.st_size" title="os.stat_result.st_size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_size</span></code></a>, <a class="reference internal" href="#os.stat_result.st_atime" title="os.stat_result.st_atime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime</span></code></a>,
  3342. <a class="reference internal" href="#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime</span></code></a>, <a class="reference internal" href="#os.stat_result.st_ctime" title="os.stat_result.st_ctime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ctime</span></code></a>. More items may be added at the end by
  3343. some implementations. For compatibility with older Python versions,
  3344. accessing <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code class="xref py py-class docutils literal notranslate"><span class="pre">stat_result</span></code></a> as a tuple always returns integers.</p>
  3345. <div class="versionchanged">
  3346. <p><span class="versionmodified changed">Changed in version 3.5: </span>Windows now returns the file index as <a class="reference internal" href="#os.stat_result.st_ino" title="os.stat_result.st_ino"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ino</span></code></a> when
  3347. available.</p>
  3348. </div>
  3349. <div class="versionchanged">
  3350. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <a class="reference internal" href="#os.stat_result.st_fstype" title="os.stat_result.st_fstype"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_fstype</span></code></a> member to Solaris/derivatives.</p>
  3351. </div>
  3352. <div class="versionchanged">
  3353. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <a class="reference internal" href="#os.stat_result.st_reparse_tag" title="os.stat_result.st_reparse_tag"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_reparse_tag</span></code></a> member on Windows.</p>
  3354. </div>
  3355. <div class="versionchanged">
  3356. <p><span class="versionmodified changed">Changed in version 3.8: </span>On Windows, the <a class="reference internal" href="#os.stat_result.st_mode" title="os.stat_result.st_mode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mode</span></code></a> member now identifies special
  3357. files as <code class="xref py py-const docutils literal notranslate"><span class="pre">S_IFCHR</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">S_IFIFO</span></code> or <code class="xref py py-const docutils literal notranslate"><span class="pre">S_IFBLK</span></code>
  3358. as appropriate.</p>
  3359. </div>
  3360. <div class="versionchanged">
  3361. <p><span class="versionmodified changed">Changed in version 3.12: </span>On Windows, <a class="reference internal" href="#os.stat_result.st_ctime" title="os.stat_result.st_ctime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ctime</span></code></a> is deprecated. Eventually, it will
  3362. contain the last metadata change time, for consistency with other
  3363. platforms, but for now still contains creation time.
  3364. Use <a class="reference internal" href="#os.stat_result.st_birthtime" title="os.stat_result.st_birthtime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_birthtime</span></code></a> for the creation time.</p>
  3365. <p>On Windows, <a class="reference internal" href="#os.stat_result.st_ino" title="os.stat_result.st_ino"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ino</span></code></a> may now be up to 128 bits, depending
  3366. on the file system. Previously it would not be above 64 bits, and
  3367. larger file identifiers would be arbitrarily packed.</p>
  3368. <p>On Windows, <a class="reference internal" href="#os.stat_result.st_rdev" title="os.stat_result.st_rdev"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_rdev</span></code></a> no longer returns a value. Previously
  3369. it would contain the same as <a class="reference internal" href="#os.stat_result.st_dev" title="os.stat_result.st_dev"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_dev</span></code></a>, which was incorrect.</p>
  3370. <p>Added the <a class="reference internal" href="#os.stat_result.st_birthtime" title="os.stat_result.st_birthtime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_birthtime</span></code></a> member on Windows.</p>
  3371. </div>
  3372. </dd></dl>
  3373. <dl class="py function">
  3374. <dt class="sig sig-object py" id="os.statvfs">
  3375. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">statvfs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.statvfs" title="Link to this definition">¶</a></dt>
  3376. <dd><p>Perform a <code class="xref c c-func docutils literal notranslate"><span class="pre">statvfs()</span></code> system call on the given path. The return value is
  3377. an object whose attributes describe the filesystem on the given path, and
  3378. correspond to the members of the <code class="xref c c-struct docutils literal notranslate"><span class="pre">statvfs</span></code> structure, namely:
  3379. <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_bsize</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_frsize</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_blocks</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_bfree</span></code>,
  3380. <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_bavail</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_files</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_ffree</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_favail</span></code>,
  3381. <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_flag</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_namemax</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_fsid</span></code>.</p>
  3382. <p>Two module-level constants are defined for the <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_flag</span></code> attribute’s
  3383. bit-flags: if <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_RDONLY</span></code> is set, the filesystem is mounted
  3384. read-only, and if <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NOSUID</span></code> is set, the semantics of
  3385. setuid/setgid bits are disabled or not supported.</p>
  3386. <p>Additional module-level constants are defined for GNU/glibc based systems.
  3387. These are <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NODEV</span></code> (disallow access to device special files),
  3388. <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NOEXEC</span></code> (disallow program execution), <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_SYNCHRONOUS</span></code>
  3389. (writes are synced at once), <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_MANDLOCK</span></code> (allow mandatory locks on an FS),
  3390. <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_WRITE</span></code> (write on file/directory/symlink), <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_APPEND</span></code>
  3391. (append-only file), <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_IMMUTABLE</span></code> (immutable file), <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NOATIME</span></code>
  3392. (do not update access times), <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NODIRATIME</span></code> (do not update directory access
  3393. times), <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_RELATIME</span></code> (update atime relative to mtime/ctime).</p>
  3394. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>.</p>
  3395. <div class="availability docutils container">
  3396. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  3397. </div>
  3398. <div class="versionchanged">
  3399. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_RDONLY</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NOSUID</span></code> constants were added.</p>
  3400. </div>
  3401. <div class="versionchanged">
  3402. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor.</p>
  3403. </div>
  3404. <div class="versionchanged">
  3405. <p><span class="versionmodified changed">Changed in version 3.4: </span>The <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NODEV</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NOEXEC</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_SYNCHRONOUS</span></code>,
  3406. <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_MANDLOCK</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_WRITE</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_APPEND</span></code>,
  3407. <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_IMMUTABLE</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NOATIME</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_NODIRATIME</span></code>,
  3408. and <code class="xref py py-const docutils literal notranslate"><span class="pre">ST_RELATIME</span></code> constants were added.</p>
  3409. </div>
  3410. <div class="versionchanged">
  3411. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  3412. </div>
  3413. <div class="versionchanged">
  3414. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <code class="xref py py-attr docutils literal notranslate"><span class="pre">f_fsid</span></code> attribute.</p>
  3415. </div>
  3416. </dd></dl>
  3417. <dl class="py data">
  3418. <dt class="sig sig-object py" id="os.supports_dir_fd">
  3419. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">supports_dir_fd</span></span><a class="headerlink" href="#os.supports_dir_fd" title="Link to this definition">¶</a></dt>
  3420. <dd><p>A <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> object indicating which functions in the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a>
  3421. module accept an open file descriptor for their <em>dir_fd</em> parameter.
  3422. Different platforms provide different features, and the underlying
  3423. functionality Python uses to implement the <em>dir_fd</em> parameter is not
  3424. available on all platforms Python supports. For consistency’s sake,
  3425. functions that may support <em>dir_fd</em> always allow specifying the
  3426. parameter, but will throw an exception if the functionality is used
  3427. when it’s not locally available. (Specifying <code class="docutils literal notranslate"><span class="pre">None</span></code> for <em>dir_fd</em>
  3428. is always supported on all platforms.)</p>
  3429. <p>To check whether a particular function accepts an open file descriptor
  3430. for its <em>dir_fd</em> parameter, use the <code class="docutils literal notranslate"><span class="pre">in</span></code> operator on <code class="docutils literal notranslate"><span class="pre">supports_dir_fd</span></code>.
  3431. As an example, this expression evaluates to <code class="docutils literal notranslate"><span class="pre">True</span></code> if <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a>
  3432. accepts open file descriptors for <em>dir_fd</em> on the local platform:</p>
  3433. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">stat</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">supports_dir_fd</span>
  3434. </pre></div>
  3435. </div>
  3436. <p>Currently <em>dir_fd</em> parameters only work on Unix platforms;
  3437. none of them work on Windows.</p>
  3438. <div class="versionadded">
  3439. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3440. </div>
  3441. </dd></dl>
  3442. <dl class="py data">
  3443. <dt class="sig sig-object py" id="os.supports_effective_ids">
  3444. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">supports_effective_ids</span></span><a class="headerlink" href="#os.supports_effective_ids" title="Link to this definition">¶</a></dt>
  3445. <dd><p>A <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> object indicating whether <a class="reference internal" href="#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.access()</span></code></a> permits
  3446. specifying <code class="docutils literal notranslate"><span class="pre">True</span></code> for its <em>effective_ids</em> parameter on the local platform.
  3447. (Specifying <code class="docutils literal notranslate"><span class="pre">False</span></code> for <em>effective_ids</em> is always supported on all
  3448. platforms.) If the local platform supports it, the collection will contain
  3449. <a class="reference internal" href="#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.access()</span></code></a>; otherwise it will be empty.</p>
  3450. <p>This expression evaluates to <code class="docutils literal notranslate"><span class="pre">True</span></code> if <a class="reference internal" href="#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.access()</span></code></a> supports
  3451. <code class="docutils literal notranslate"><span class="pre">effective_ids=True</span></code> on the local platform:</p>
  3452. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">access</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">supports_effective_ids</span>
  3453. </pre></div>
  3454. </div>
  3455. <p>Currently <em>effective_ids</em> is only supported on Unix platforms;
  3456. it does not work on Windows.</p>
  3457. <div class="versionadded">
  3458. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3459. </div>
  3460. </dd></dl>
  3461. <dl class="py data">
  3462. <dt class="sig sig-object py" id="os.supports_fd">
  3463. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">supports_fd</span></span><a class="headerlink" href="#os.supports_fd" title="Link to this definition">¶</a></dt>
  3464. <dd><p>A <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> object indicating which functions in the
  3465. <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module permit specifying their <em>path</em> parameter as an open file
  3466. descriptor on the local platform. Different platforms provide different
  3467. features, and the underlying functionality Python uses to accept open file
  3468. descriptors as <em>path</em> arguments is not available on all platforms Python
  3469. supports.</p>
  3470. <p>To determine whether a particular function permits specifying an open file
  3471. descriptor for its <em>path</em> parameter, use the <code class="docutils literal notranslate"><span class="pre">in</span></code> operator on
  3472. <code class="docutils literal notranslate"><span class="pre">supports_fd</span></code>. As an example, this expression evaluates to <code class="docutils literal notranslate"><span class="pre">True</span></code> if
  3473. <a class="reference internal" href="#os.chdir" title="os.chdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.chdir()</span></code></a> accepts open file descriptors for <em>path</em> on your local
  3474. platform:</p>
  3475. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">chdir</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">supports_fd</span>
  3476. </pre></div>
  3477. </div>
  3478. <div class="versionadded">
  3479. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3480. </div>
  3481. </dd></dl>
  3482. <dl class="py data">
  3483. <dt class="sig sig-object py" id="os.supports_follow_symlinks">
  3484. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">supports_follow_symlinks</span></span><a class="headerlink" href="#os.supports_follow_symlinks" title="Link to this definition">¶</a></dt>
  3485. <dd><p>A <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> object indicating which functions in the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module
  3486. accept <code class="docutils literal notranslate"><span class="pre">False</span></code> for their <em>follow_symlinks</em> parameter on the local platform.
  3487. Different platforms provide different features, and the underlying
  3488. functionality Python uses to implement <em>follow_symlinks</em> is not available
  3489. on all platforms Python supports. For consistency’s sake, functions that
  3490. may support <em>follow_symlinks</em> always allow specifying the parameter, but
  3491. will throw an exception if the functionality is used when it’s not locally
  3492. available. (Specifying <code class="docutils literal notranslate"><span class="pre">True</span></code> for <em>follow_symlinks</em> is always supported
  3493. on all platforms.)</p>
  3494. <p>To check whether a particular function accepts <code class="docutils literal notranslate"><span class="pre">False</span></code> for its
  3495. <em>follow_symlinks</em> parameter, use the <code class="docutils literal notranslate"><span class="pre">in</span></code> operator on
  3496. <code class="docutils literal notranslate"><span class="pre">supports_follow_symlinks</span></code>. As an example, this expression evaluates
  3497. to <code class="docutils literal notranslate"><span class="pre">True</span></code> if you may specify <code class="docutils literal notranslate"><span class="pre">follow_symlinks=False</span></code> when calling
  3498. <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> on the local platform:</p>
  3499. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">stat</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">supports_follow_symlinks</span>
  3500. </pre></div>
  3501. </div>
  3502. <div class="versionadded">
  3503. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3504. </div>
  3505. </dd></dl>
  3506. <dl class="py function">
  3507. <dt class="sig sig-object py" id="os.symlink">
  3508. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">symlink</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target_is_directory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.symlink" title="Link to this definition">¶</a></dt>
  3509. <dd><p>Create a symbolic link pointing to <em>src</em> named <em>dst</em>.</p>
  3510. <p>On Windows, a symlink represents either a file or a directory, and does not
  3511. morph to the target dynamically. If the target is present, the type of the
  3512. symlink will be created to match. Otherwise, the symlink will be created
  3513. as a directory if <em>target_is_directory</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> or a file symlink (the
  3514. default) otherwise. On non-Windows platforms, <em>target_is_directory</em> is ignored.</p>
  3515. <p>This function can support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>
  3516. <div class="admonition note">
  3517. <p class="admonition-title">Note</p>
  3518. <p>On newer versions of Windows 10, unprivileged accounts can create symlinks
  3519. if Developer Mode is enabled. When Developer Mode is not available/enabled,
  3520. the <em>SeCreateSymbolicLinkPrivilege</em> privilege is required, or the process
  3521. must be run as an administrator.</p>
  3522. <p><a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised when the function is called by an unprivileged
  3523. user.</p>
  3524. </div>
  3525. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.symlink</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  3526. <div class="availability docutils container">
  3527. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  3528. <p>The function is limited on Emscripten and WASI, see
  3529. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  3530. </div>
  3531. <div class="versionchanged">
  3532. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added support for Windows 6.0 (Vista) symbolic links.</p>
  3533. </div>
  3534. <div class="versionchanged">
  3535. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter, and now allow <em>target_is_directory</em>
  3536. on non-Windows platforms.</p>
  3537. </div>
  3538. <div class="versionchanged">
  3539. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>src</em> and <em>dst</em>.</p>
  3540. </div>
  3541. <div class="versionchanged">
  3542. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added support for unelevated symlinks on Windows with Developer Mode.</p>
  3543. </div>
  3544. </dd></dl>
  3545. <dl class="py function">
  3546. <dt class="sig sig-object py" id="os.sync">
  3547. <span class="sig-prename descclassname"><span class="pre">os.</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="#os.sync" title="Link to this definition">¶</a></dt>
  3548. <dd><p>Force write of everything to disk.</p>
  3549. <div class="availability docutils container">
  3550. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  3551. </div>
  3552. <div class="versionadded">
  3553. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3554. </div>
  3555. </dd></dl>
  3556. <dl class="py function">
  3557. <dt class="sig sig-object py" id="os.truncate">
  3558. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">truncate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">length</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.truncate" title="Link to this definition">¶</a></dt>
  3559. <dd><p>Truncate the file corresponding to <em>path</em>, so that it is at most
  3560. <em>length</em> bytes in size.</p>
  3561. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>.</p>
  3562. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.truncate</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">length</span></code>.</p>
  3563. <div class="availability docutils container">
  3564. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  3565. </div>
  3566. <div class="versionadded">
  3567. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3568. </div>
  3569. <div class="versionchanged">
  3570. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for Windows</p>
  3571. </div>
  3572. <div class="versionchanged">
  3573. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  3574. </div>
  3575. </dd></dl>
  3576. <dl class="py function">
  3577. <dt class="sig sig-object py" id="os.unlink">
  3578. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">unlink</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.unlink" title="Link to this definition">¶</a></dt>
  3579. <dd><p>Remove (delete) the file <em>path</em>. This function is semantically
  3580. identical to <a class="reference internal" href="#os.remove" title="os.remove"><code class="xref py py-func docutils literal notranslate"><span class="pre">remove()</span></code></a>; the <code class="docutils literal notranslate"><span class="pre">unlink</span></code> name is its
  3581. traditional Unix name. Please see the documentation for
  3582. <a class="reference internal" href="#os.remove" title="os.remove"><code class="xref py py-func docutils literal notranslate"><span class="pre">remove()</span></code></a> for further information.</p>
  3583. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.remove</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  3584. <div class="versionchanged">
  3585. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p>
  3586. </div>
  3587. <div class="versionchanged">
  3588. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  3589. </div>
  3590. </dd></dl>
  3591. <dl class="py function">
  3592. <dt class="sig sig-object py" id="os.utime">
  3593. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">utime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">times=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">ns</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">dir_fd=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.utime" title="Link to this definition">¶</a></dt>
  3594. <dd><p>Set the access and modified times of the file specified by <em>path</em>.</p>
  3595. <p><a class="reference internal" href="#os.utime" title="os.utime"><code class="xref py py-func docutils literal notranslate"><span class="pre">utime()</span></code></a> takes two optional parameters, <em>times</em> and <em>ns</em>.
  3596. These specify the times set on <em>path</em> and are used as follows:</p>
  3597. <ul class="simple">
  3598. <li><p>If <em>ns</em> is specified,
  3599. it must be a 2-tuple of the form <code class="docutils literal notranslate"><span class="pre">(atime_ns,</span> <span class="pre">mtime_ns)</span></code>
  3600. where each member is an int expressing nanoseconds.</p></li>
  3601. <li><p>If <em>times</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  3602. it must be a 2-tuple of the form <code class="docutils literal notranslate"><span class="pre">(atime,</span> <span class="pre">mtime)</span></code>
  3603. where each member is an int or float expressing seconds.</p></li>
  3604. <li><p>If <em>times</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> and <em>ns</em> is unspecified,
  3605. this is equivalent to specifying <code class="docutils literal notranslate"><span class="pre">ns=(atime_ns,</span> <span class="pre">mtime_ns)</span></code>
  3606. where both times are the current time.</p></li>
  3607. </ul>
  3608. <p>It is an error to specify tuples for both <em>times</em> and <em>ns</em>.</p>
  3609. <p>Note that the exact times you set here may not be returned by a subsequent
  3610. <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a> call, depending on the resolution with which your operating
  3611. system records access and modification times; see <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a>. The best
  3612. way to preserve exact times is to use the <em>st_atime_ns</em> and <em>st_mtime_ns</em>
  3613. fields from the <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> result object with the <em>ns</em> parameter to
  3614. <a class="reference internal" href="#os.utime" title="os.utime"><code class="xref py py-func docutils literal notranslate"><span class="pre">utime()</span></code></a>.</p>
  3615. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>,
  3616. <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not
  3617. following symlinks</span></a>.</p>
  3618. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.utime</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">times</span></code>, <code class="docutils literal notranslate"><span class="pre">ns</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  3619. <div class="versionchanged">
  3620. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor,
  3621. and the <em>dir_fd</em>, <em>follow_symlinks</em>, and <em>ns</em> parameters.</p>
  3622. </div>
  3623. <div class="versionchanged">
  3624. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  3625. </div>
  3626. </dd></dl>
  3627. <dl class="py function">
  3628. <dt class="sig sig-object py" id="os.walk">
  3629. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">walk</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">top</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">topdown</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">onerror</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">followlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.walk" title="Link to this definition">¶</a></dt>
  3630. <dd><p id="index-31">Generate the file names in a directory tree by walking the tree
  3631. either top-down or bottom-up. For each directory in the tree rooted at directory
  3632. <em>top</em> (including <em>top</em> itself), it yields a 3-tuple <code class="docutils literal notranslate"><span class="pre">(dirpath,</span> <span class="pre">dirnames,</span>
  3633. <span class="pre">filenames)</span></code>.</p>
  3634. <p><em>dirpath</em> is a string, the path to the directory. <em>dirnames</em> is a list of the
  3635. names of the subdirectories in <em>dirpath</em> (including symlinks to directories,
  3636. and excluding <code class="docutils literal notranslate"><span class="pre">'.'</span></code> and <code class="docutils literal notranslate"><span class="pre">'..'</span></code>).
  3637. <em>filenames</em> is a list of the names of the non-directory files in <em>dirpath</em>.
  3638. Note that the names in the lists contain no path components. To get a full path
  3639. (which begins with <em>top</em>) to a file or directory in <em>dirpath</em>, do
  3640. <code class="docutils literal notranslate"><span class="pre">os.path.join(dirpath,</span> <span class="pre">name)</span></code>. Whether or not the lists are sorted
  3641. depends on the file system. If a file is removed from or added to the
  3642. <em>dirpath</em> directory during generating the lists, whether a name for that
  3643. file be included is unspecified.</p>
  3644. <p>If optional argument <em>topdown</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> or not specified, the triple for a
  3645. directory is generated before the triples for any of its subdirectories
  3646. (directories are generated top-down). If <em>topdown</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, the triple
  3647. for a directory is generated after the triples for all of its subdirectories
  3648. (directories are generated bottom-up). No matter the value of <em>topdown</em>, the
  3649. list of subdirectories is retrieved before the tuples for the directory and
  3650. its subdirectories are generated.</p>
  3651. <p>When <em>topdown</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the caller can modify the <em>dirnames</em> list in-place
  3652. (perhaps using <a class="reference internal" href="../reference/simple_stmts.html#del"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code></a> or slice assignment), and <a class="reference internal" href="#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk()</span></code></a> will only
  3653. recurse into the subdirectories whose names remain in <em>dirnames</em>; this can be
  3654. used to prune the search, impose a specific order of visiting, or even to inform
  3655. <a class="reference internal" href="#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk()</span></code></a> about directories the caller creates or renames before it resumes
  3656. <a class="reference internal" href="#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk()</span></code></a> again. Modifying <em>dirnames</em> when <em>topdown</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> has
  3657. no effect on the behavior of the walk, because in bottom-up mode the directories
  3658. in <em>dirnames</em> are generated before <em>dirpath</em> itself is generated.</p>
  3659. <p>By default, errors from the <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">scandir()</span></code></a> call are ignored. If optional
  3660. argument <em>onerror</em> is specified, it should be a function; it will be called with
  3661. one argument, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instance. It can report the error to continue
  3662. with the walk, or raise the exception to abort the walk. Note that the filename
  3663. is available as the <code class="docutils literal notranslate"><span class="pre">filename</span></code> attribute of the exception object.</p>
  3664. <p>By default, <a class="reference internal" href="#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk()</span></code></a> will not walk down into symbolic links that resolve to
  3665. directories. Set <em>followlinks</em> to <code class="docutils literal notranslate"><span class="pre">True</span></code> to visit directories pointed to by
  3666. symlinks, on systems that support them.</p>
  3667. <div class="admonition note">
  3668. <p class="admonition-title">Note</p>
  3669. <p>Be aware that setting <em>followlinks</em> to <code class="docutils literal notranslate"><span class="pre">True</span></code> can lead to infinite
  3670. recursion if a link points to a parent directory of itself. <a class="reference internal" href="#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk()</span></code></a>
  3671. does not keep track of the directories it visited already.</p>
  3672. </div>
  3673. <div class="admonition note">
  3674. <p class="admonition-title">Note</p>
  3675. <p>If you pass a relative pathname, don’t change the current working directory
  3676. between resumptions of <a class="reference internal" href="#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk()</span></code></a>. <a class="reference internal" href="#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk()</span></code></a> never changes the current
  3677. directory, and assumes that its caller doesn’t either.</p>
  3678. </div>
  3679. <p>This example displays the number of bytes taken by non-directory files in each
  3680. directory under the starting directory, except that it doesn’t look under any
  3681. CVS subdirectory:</p>
  3682. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
  3683. <span class="kn">from</span> <span class="nn">os.path</span> <span class="kn">import</span> <span class="n">join</span><span class="p">,</span> <span class="n">getsize</span>
  3684. <span class="k">for</span> <span class="n">root</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">files</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="s1">&#39;python/Lib/email&#39;</span><span class="p">):</span>
  3685. <span class="nb">print</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s2">&quot;consumes&quot;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot; &quot;</span><span class="p">)</span>
  3686. <span class="nb">print</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">getsize</span><span class="p">(</span><span class="n">join</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">files</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot; &quot;</span><span class="p">)</span>
  3687. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;bytes in&quot;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">files</span><span class="p">),</span> <span class="s2">&quot;non-directory files&quot;</span><span class="p">)</span>
  3688. <span class="k">if</span> <span class="s1">&#39;CVS&#39;</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span>
  3689. <span class="n">dirs</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&#39;CVS&#39;</span><span class="p">)</span> <span class="c1"># don&#39;t visit CVS directories</span>
  3690. </pre></div>
  3691. </div>
  3692. <p>In the next example (simple implementation of <a class="reference internal" href="shutil.html#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.rmtree()</span></code></a>),
  3693. walking the tree bottom-up is essential, <a class="reference internal" href="#os.rmdir" title="os.rmdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmdir()</span></code></a> doesn’t allow
  3694. deleting a directory before the directory is empty:</p>
  3695. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Delete everything reachable from the directory named in &quot;top&quot;,</span>
  3696. <span class="c1"># assuming there are no symbolic links.</span>
  3697. <span class="c1"># CAUTION: This is dangerous! For example, if top == &#39;/&#39;, it</span>
  3698. <span class="c1"># could delete all your disk files.</span>
  3699. <span class="kn">import</span> <span class="nn">os</span>
  3700. <span class="k">for</span> <span class="n">root</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">files</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="n">topdown</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
  3701. <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
  3702. <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
  3703. <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span>
  3704. <span class="n">os</span><span class="o">.</span><span class="n">rmdir</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
  3705. </pre></div>
  3706. </div>
  3707. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.walk</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">top</span></code>, <code class="docutils literal notranslate"><span class="pre">topdown</span></code>, <code class="docutils literal notranslate"><span class="pre">onerror</span></code>, <code class="docutils literal notranslate"><span class="pre">followlinks</span></code>.</p>
  3708. <div class="versionchanged">
  3709. <p><span class="versionmodified changed">Changed in version 3.5: </span>This function now calls <a class="reference internal" href="#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.scandir()</span></code></a> instead of <a class="reference internal" href="#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a>,
  3710. making it faster by reducing the number of calls to <a class="reference internal" href="#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a>.</p>
  3711. </div>
  3712. <div class="versionchanged">
  3713. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  3714. </div>
  3715. </dd></dl>
  3716. <dl class="py function">
  3717. <dt class="sig sig-object py" id="os.fwalk">
  3718. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fwalk</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">top</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'.'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">topdown</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">onerror</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.fwalk" title="Link to this definition">¶</a></dt>
  3719. <dd><p id="index-32">This behaves exactly like <a class="reference internal" href="#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk()</span></code></a>, except that it yields a 4-tuple
  3720. <code class="docutils literal notranslate"><span class="pre">(dirpath,</span> <span class="pre">dirnames,</span> <span class="pre">filenames,</span> <span class="pre">dirfd)</span></code>, and it supports <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  3721. <p><em>dirpath</em>, <em>dirnames</em> and <em>filenames</em> are identical to <a class="reference internal" href="#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk()</span></code></a> output,
  3722. and <em>dirfd</em> is a file descriptor referring to the directory <em>dirpath</em>.</p>
  3723. <p>This function always supports <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>. Note however
  3724. that, unlike other functions, the <a class="reference internal" href="#os.fwalk" title="os.fwalk"><code class="xref py py-func docutils literal notranslate"><span class="pre">fwalk()</span></code></a> default value for
  3725. <em>follow_symlinks</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  3726. <div class="admonition note">
  3727. <p class="admonition-title">Note</p>
  3728. <p>Since <a class="reference internal" href="#os.fwalk" title="os.fwalk"><code class="xref py py-func docutils literal notranslate"><span class="pre">fwalk()</span></code></a> yields file descriptors, those are only valid until
  3729. the next iteration step, so you should duplicate them (e.g. with
  3730. <a class="reference internal" href="#os.dup" title="os.dup"><code class="xref py py-func docutils literal notranslate"><span class="pre">dup()</span></code></a>) if you want to keep them longer.</p>
  3731. </div>
  3732. <p>This example displays the number of bytes taken by non-directory files in each
  3733. directory under the starting directory, except that it doesn’t look under any
  3734. CVS subdirectory:</p>
  3735. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
  3736. <span class="k">for</span> <span class="n">root</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">rootfd</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">fwalk</span><span class="p">(</span><span class="s1">&#39;python/Lib/email&#39;</span><span class="p">):</span>
  3737. <span class="nb">print</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s2">&quot;consumes&quot;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
  3738. <span class="nb">print</span><span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">rootfd</span><span class="p">)</span><span class="o">.</span><span class="n">st_size</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">files</span><span class="p">]),</span>
  3739. <span class="n">end</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
  3740. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;bytes in&quot;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">files</span><span class="p">),</span> <span class="s2">&quot;non-directory files&quot;</span><span class="p">)</span>
  3741. <span class="k">if</span> <span class="s1">&#39;CVS&#39;</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span>
  3742. <span class="n">dirs</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&#39;CVS&#39;</span><span class="p">)</span> <span class="c1"># don&#39;t visit CVS directories</span>
  3743. </pre></div>
  3744. </div>
  3745. <p>In the next example, walking the tree bottom-up is essential:
  3746. <a class="reference internal" href="#os.rmdir" title="os.rmdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmdir()</span></code></a> doesn’t allow deleting a directory before the directory is
  3747. empty:</p>
  3748. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Delete everything reachable from the directory named in &quot;top&quot;,</span>
  3749. <span class="c1"># assuming there are no symbolic links.</span>
  3750. <span class="c1"># CAUTION: This is dangerous! For example, if top == &#39;/&#39;, it</span>
  3751. <span class="c1"># could delete all your disk files.</span>
  3752. <span class="kn">import</span> <span class="nn">os</span>
  3753. <span class="k">for</span> <span class="n">root</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">rootfd</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">fwalk</span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="n">topdown</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
  3754. <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
  3755. <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">rootfd</span><span class="p">)</span>
  3756. <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span>
  3757. <span class="n">os</span><span class="o">.</span><span class="n">rmdir</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">rootfd</span><span class="p">)</span>
  3758. </pre></div>
  3759. </div>
  3760. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.fwalk</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">top</span></code>, <code class="docutils literal notranslate"><span class="pre">topdown</span></code>, <code class="docutils literal notranslate"><span class="pre">onerror</span></code>, <code class="docutils literal notranslate"><span class="pre">follow_symlinks</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>
  3761. <div class="availability docutils container">
  3762. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  3763. </div>
  3764. <div class="versionadded">
  3765. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3766. </div>
  3767. <div class="versionchanged">
  3768. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  3769. </div>
  3770. <div class="versionchanged">
  3771. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added support for <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> paths.</p>
  3772. </div>
  3773. </dd></dl>
  3774. <dl class="py function">
  3775. <dt class="sig sig-object py" id="os.memfd_create">
  3776. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">memfd_create</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">flags=os.MFD_CLOEXEC</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.memfd_create" title="Link to this definition">¶</a></dt>
  3777. <dd><p>Create an anonymous file and return a file descriptor that refers to it.
  3778. <em>flags</em> must be one of the <code class="docutils literal notranslate"><span class="pre">os.MFD_*</span></code> constants available on the system
  3779. (or a bitwise ORed combination of them). By default, the new file
  3780. descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p>
  3781. <p>The name supplied in <em>name</em> is used as a filename and will be displayed as
  3782. the target of the corresponding symbolic link in the directory
  3783. <code class="docutils literal notranslate"><span class="pre">/proc/self/fd/</span></code>. The displayed name is always prefixed with <code class="docutils literal notranslate"><span class="pre">memfd:</span></code>
  3784. and serves only for debugging purposes. Names do not affect the behavior of
  3785. the file descriptor, and as such multiple files can have the same name
  3786. without any side effects.</p>
  3787. <div class="availability docutils container">
  3788. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 3.17 with glibc &gt;= 2.27.</p>
  3789. </div>
  3790. <div class="versionadded">
  3791. <p><span class="versionmodified added">New in version 3.8.</span></p>
  3792. </div>
  3793. </dd></dl>
  3794. <dl class="py data">
  3795. <dt class="sig sig-object py" id="os.MFD_CLOEXEC">
  3796. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_CLOEXEC</span></span><a class="headerlink" href="#os.MFD_CLOEXEC" title="Link to this definition">¶</a></dt>
  3797. <dt class="sig sig-object py" id="os.MFD_ALLOW_SEALING">
  3798. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_ALLOW_SEALING</span></span><a class="headerlink" href="#os.MFD_ALLOW_SEALING" title="Link to this definition">¶</a></dt>
  3799. <dt class="sig sig-object py" id="os.MFD_HUGETLB">
  3800. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGETLB</span></span><a class="headerlink" href="#os.MFD_HUGETLB" title="Link to this definition">¶</a></dt>
  3801. <dt class="sig sig-object py" id="os.MFD_HUGE_SHIFT">
  3802. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_SHIFT</span></span><a class="headerlink" href="#os.MFD_HUGE_SHIFT" title="Link to this definition">¶</a></dt>
  3803. <dt class="sig sig-object py" id="os.MFD_HUGE_MASK">
  3804. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_MASK</span></span><a class="headerlink" href="#os.MFD_HUGE_MASK" title="Link to this definition">¶</a></dt>
  3805. <dt class="sig sig-object py" id="os.MFD_HUGE_64KB">
  3806. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_64KB</span></span><a class="headerlink" href="#os.MFD_HUGE_64KB" title="Link to this definition">¶</a></dt>
  3807. <dt class="sig sig-object py" id="os.MFD_HUGE_512KB">
  3808. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_512KB</span></span><a class="headerlink" href="#os.MFD_HUGE_512KB" title="Link to this definition">¶</a></dt>
  3809. <dt class="sig sig-object py" id="os.MFD_HUGE_1MB">
  3810. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_1MB</span></span><a class="headerlink" href="#os.MFD_HUGE_1MB" title="Link to this definition">¶</a></dt>
  3811. <dt class="sig sig-object py" id="os.MFD_HUGE_2MB">
  3812. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_2MB</span></span><a class="headerlink" href="#os.MFD_HUGE_2MB" title="Link to this definition">¶</a></dt>
  3813. <dt class="sig sig-object py" id="os.MFD_HUGE_8MB">
  3814. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_8MB</span></span><a class="headerlink" href="#os.MFD_HUGE_8MB" title="Link to this definition">¶</a></dt>
  3815. <dt class="sig sig-object py" id="os.MFD_HUGE_16MB">
  3816. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_16MB</span></span><a class="headerlink" href="#os.MFD_HUGE_16MB" title="Link to this definition">¶</a></dt>
  3817. <dt class="sig sig-object py" id="os.MFD_HUGE_32MB">
  3818. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_32MB</span></span><a class="headerlink" href="#os.MFD_HUGE_32MB" title="Link to this definition">¶</a></dt>
  3819. <dt class="sig sig-object py" id="os.MFD_HUGE_256MB">
  3820. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_256MB</span></span><a class="headerlink" href="#os.MFD_HUGE_256MB" title="Link to this definition">¶</a></dt>
  3821. <dt class="sig sig-object py" id="os.MFD_HUGE_512MB">
  3822. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_512MB</span></span><a class="headerlink" href="#os.MFD_HUGE_512MB" title="Link to this definition">¶</a></dt>
  3823. <dt class="sig sig-object py" id="os.MFD_HUGE_1GB">
  3824. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_1GB</span></span><a class="headerlink" href="#os.MFD_HUGE_1GB" title="Link to this definition">¶</a></dt>
  3825. <dt class="sig sig-object py" id="os.MFD_HUGE_2GB">
  3826. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_2GB</span></span><a class="headerlink" href="#os.MFD_HUGE_2GB" title="Link to this definition">¶</a></dt>
  3827. <dt class="sig sig-object py" id="os.MFD_HUGE_16GB">
  3828. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">MFD_HUGE_16GB</span></span><a class="headerlink" href="#os.MFD_HUGE_16GB" title="Link to this definition">¶</a></dt>
  3829. <dd><p>These flags can be passed to <a class="reference internal" href="#os.memfd_create" title="os.memfd_create"><code class="xref py py-func docutils literal notranslate"><span class="pre">memfd_create()</span></code></a>.</p>
  3830. <div class="availability docutils container">
  3831. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 3.17 with glibc &gt;= 2.27</p>
  3832. <p>The <code class="docutils literal notranslate"><span class="pre">MFD_HUGE*</span></code> flags are only available since Linux 4.14.</p>
  3833. </div>
  3834. <div class="versionadded">
  3835. <p><span class="versionmodified added">New in version 3.8.</span></p>
  3836. </div>
  3837. </dd></dl>
  3838. <dl class="py function">
  3839. <dt class="sig sig-object py" id="os.eventfd">
  3840. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">eventfd</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">initval</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">flags=os.EFD_CLOEXEC</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.eventfd" title="Link to this definition">¶</a></dt>
  3841. <dd><p>Create and return an event file descriptor. The file descriptors supports
  3842. raw <a class="reference internal" href="#os.read" title="os.read"><code class="xref py py-func docutils literal notranslate"><span class="pre">read()</span></code></a> and <a class="reference internal" href="#os.write" title="os.write"><code class="xref py py-func docutils literal notranslate"><span class="pre">write()</span></code></a> with a buffer size of 8,
  3843. <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a>, <a class="reference internal" href="select.html#select.poll" title="select.poll"><code class="xref py py-func docutils literal notranslate"><span class="pre">poll()</span></code></a> and similar. See man page
  3844. <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/eventfd(2)">eventfd(2)</a></em> for more information. By default, the
  3845. new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p>
  3846. <p><em>initval</em> is the initial value of the event counter. The initial value
  3847. must be an 32 bit unsigned integer. Please note that the initial value is
  3848. limited to a 32 bit unsigned int although the event counter is an unsigned
  3849. 64 bit integer with a maximum value of 2<sup>64</sup>-2.</p>
  3850. <p><em>flags</em> can be constructed from <a class="reference internal" href="#os.EFD_CLOEXEC" title="os.EFD_CLOEXEC"><code class="xref py py-const docutils literal notranslate"><span class="pre">EFD_CLOEXEC</span></code></a>,
  3851. <a class="reference internal" href="#os.EFD_NONBLOCK" title="os.EFD_NONBLOCK"><code class="xref py py-const docutils literal notranslate"><span class="pre">EFD_NONBLOCK</span></code></a>, and <a class="reference internal" href="#os.EFD_SEMAPHORE" title="os.EFD_SEMAPHORE"><code class="xref py py-const docutils literal notranslate"><span class="pre">EFD_SEMAPHORE</span></code></a>.</p>
  3852. <p>If <a class="reference internal" href="#os.EFD_SEMAPHORE" title="os.EFD_SEMAPHORE"><code class="xref py py-const docutils literal notranslate"><span class="pre">EFD_SEMAPHORE</span></code></a> is specified and the event counter is non-zero,
  3853. <a class="reference internal" href="#os.eventfd_read" title="os.eventfd_read"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd_read()</span></code></a> returns 1 and decrements the counter by one.</p>
  3854. <p>If <a class="reference internal" href="#os.EFD_SEMAPHORE" title="os.EFD_SEMAPHORE"><code class="xref py py-const docutils literal notranslate"><span class="pre">EFD_SEMAPHORE</span></code></a> is not specified and the event counter is
  3855. non-zero, <a class="reference internal" href="#os.eventfd_read" title="os.eventfd_read"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd_read()</span></code></a> returns the current event counter value and
  3856. resets the counter to zero.</p>
  3857. <p>If the event counter is zero and <a class="reference internal" href="#os.EFD_NONBLOCK" title="os.EFD_NONBLOCK"><code class="xref py py-const docutils literal notranslate"><span class="pre">EFD_NONBLOCK</span></code></a> is not
  3858. specified, <a class="reference internal" href="#os.eventfd_read" title="os.eventfd_read"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd_read()</span></code></a> blocks.</p>
  3859. <p><a class="reference internal" href="#os.eventfd_write" title="os.eventfd_write"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd_write()</span></code></a> increments the event counter. Write blocks if the
  3860. write operation would increment the counter to a value larger than
  3861. 2<sup>64</sup>-2.</p>
  3862. <p>Example:</p>
  3863. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
  3864. <span class="c1"># semaphore with start value &#39;1&#39;</span>
  3865. <span class="n">fd</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">eventfd</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">EFD_SEMAPHORE</span> <span class="o">|</span> <span class="n">os</span><span class="o">.</span><span class="n">EFC_CLOEXEC</span><span class="p">)</span>
  3866. <span class="k">try</span><span class="p">:</span>
  3867. <span class="c1"># acquire semaphore</span>
  3868. <span class="n">v</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">eventfd_read</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
  3869. <span class="k">try</span><span class="p">:</span>
  3870. <span class="n">do_work</span><span class="p">()</span>
  3871. <span class="k">finally</span><span class="p">:</span>
  3872. <span class="c1"># release semaphore</span>
  3873. <span class="n">os</span><span class="o">.</span><span class="n">eventfd_write</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
  3874. <span class="k">finally</span><span class="p">:</span>
  3875. <span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
  3876. </pre></div>
  3877. </div>
  3878. <div class="availability docutils container">
  3879. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.27 with glibc &gt;= 2.8</p>
  3880. </div>
  3881. <div class="versionadded">
  3882. <p><span class="versionmodified added">New in version 3.10.</span></p>
  3883. </div>
  3884. </dd></dl>
  3885. <dl class="py function">
  3886. <dt class="sig sig-object py" id="os.eventfd_read">
  3887. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">eventfd_read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.eventfd_read" title="Link to this definition">¶</a></dt>
  3888. <dd><p>Read value from an <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd()</span></code></a> file descriptor and return a 64 bit
  3889. unsigned int. The function does not verify that <em>fd</em> is an <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd()</span></code></a>.</p>
  3890. <div class="availability docutils container">
  3891. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.27</p>
  3892. </div>
  3893. <div class="versionadded">
  3894. <p><span class="versionmodified added">New in version 3.10.</span></p>
  3895. </div>
  3896. </dd></dl>
  3897. <dl class="py function">
  3898. <dt class="sig sig-object py" id="os.eventfd_write">
  3899. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">eventfd_write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.eventfd_write" title="Link to this definition">¶</a></dt>
  3900. <dd><p>Add value to an <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd()</span></code></a> file descriptor. <em>value</em> must be a 64 bit
  3901. unsigned int. The function does not verify that <em>fd</em> is an <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd()</span></code></a>.</p>
  3902. <div class="availability docutils container">
  3903. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.27</p>
  3904. </div>
  3905. <div class="versionadded">
  3906. <p><span class="versionmodified added">New in version 3.10.</span></p>
  3907. </div>
  3908. </dd></dl>
  3909. <dl class="py data">
  3910. <dt class="sig sig-object py" id="os.EFD_CLOEXEC">
  3911. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EFD_CLOEXEC</span></span><a class="headerlink" href="#os.EFD_CLOEXEC" title="Link to this definition">¶</a></dt>
  3912. <dd><p>Set close-on-exec flag for new <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd()</span></code></a> file descriptor.</p>
  3913. <div class="availability docutils container">
  3914. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.27</p>
  3915. </div>
  3916. <div class="versionadded">
  3917. <p><span class="versionmodified added">New in version 3.10.</span></p>
  3918. </div>
  3919. </dd></dl>
  3920. <dl class="py data">
  3921. <dt class="sig sig-object py" id="os.EFD_NONBLOCK">
  3922. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EFD_NONBLOCK</span></span><a class="headerlink" href="#os.EFD_NONBLOCK" title="Link to this definition">¶</a></dt>
  3923. <dd><p>Set <a class="reference internal" href="#os.O_NONBLOCK" title="os.O_NONBLOCK"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_NONBLOCK</span></code></a> status flag for new <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd()</span></code></a> file
  3924. descriptor.</p>
  3925. <div class="availability docutils container">
  3926. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.27</p>
  3927. </div>
  3928. <div class="versionadded">
  3929. <p><span class="versionmodified added">New in version 3.10.</span></p>
  3930. </div>
  3931. </dd></dl>
  3932. <dl class="py data">
  3933. <dt class="sig sig-object py" id="os.EFD_SEMAPHORE">
  3934. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EFD_SEMAPHORE</span></span><a class="headerlink" href="#os.EFD_SEMAPHORE" title="Link to this definition">¶</a></dt>
  3935. <dd><p>Provide semaphore-like semantics for reads from a <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code class="xref py py-func docutils literal notranslate"><span class="pre">eventfd()</span></code></a> file
  3936. descriptor. On read the internal counter is decremented by one.</p>
  3937. <div class="availability docutils container">
  3938. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 2.6.30</p>
  3939. </div>
  3940. <div class="versionadded">
  3941. <p><span class="versionmodified added">New in version 3.10.</span></p>
  3942. </div>
  3943. </dd></dl>
  3944. <section id="linux-extended-attributes">
  3945. <h3>Linux extended attributes<a class="headerlink" href="#linux-extended-attributes" title="Link to this heading">¶</a></h3>
  3946. <div class="versionadded">
  3947. <p><span class="versionmodified added">New in version 3.3.</span></p>
  3948. </div>
  3949. <p>These functions are all available on Linux only.</p>
  3950. <dl class="py function">
  3951. <dt class="sig sig-object py" id="os.getxattr">
  3952. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getxattr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attribute</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.getxattr" title="Link to this definition">¶</a></dt>
  3953. <dd><p>Return the value of the extended filesystem attribute <em>attribute</em> for
  3954. <em>path</em>. <em>attribute</em> can be bytes or str (directly or indirectly through the
  3955. <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathLike</span></code></a> interface). If it is str, it is encoded with the filesystem
  3956. encoding.</p>
  3957. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and
  3958. <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p>
  3959. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.getxattr</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">attribute</span></code>.</p>
  3960. <div class="versionchanged">
  3961. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>path</em> and <em>attribute</em>.</p>
  3962. </div>
  3963. </dd></dl>
  3964. <dl class="py function">
  3965. <dt class="sig sig-object py" id="os.listxattr">
  3966. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">listxattr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.listxattr" title="Link to this definition">¶</a></dt>
  3967. <dd><p>Return a list of the extended filesystem attributes on <em>path</em>. The
  3968. attributes in the list are represented as strings decoded with the filesystem
  3969. encoding. If <em>path</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#os.listxattr" title="os.listxattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">listxattr()</span></code></a> will examine the current
  3970. directory.</p>
  3971. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and
  3972. <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p>
  3973. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.listxattr</span></code> with argument <code class="docutils literal notranslate"><span class="pre">path</span></code>.</p>
  3974. <div class="versionchanged">
  3975. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  3976. </div>
  3977. </dd></dl>
  3978. <dl class="py function">
  3979. <dt class="sig sig-object py" id="os.removexattr">
  3980. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">removexattr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attribute</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.removexattr" title="Link to this definition">¶</a></dt>
  3981. <dd><p>Removes the extended filesystem attribute <em>attribute</em> from <em>path</em>.
  3982. <em>attribute</em> should be bytes or str (directly or indirectly through the
  3983. <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathLike</span></code></a> interface). If it is a string, it is encoded
  3984. with the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>.</p>
  3985. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and
  3986. <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p>
  3987. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.removexattr</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">attribute</span></code>.</p>
  3988. <div class="versionchanged">
  3989. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>path</em> and <em>attribute</em>.</p>
  3990. </div>
  3991. </dd></dl>
  3992. <dl class="py function">
  3993. <dt class="sig sig-object py" id="os.setxattr">
  3994. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">setxattr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attribute</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.setxattr" title="Link to this definition">¶</a></dt>
  3995. <dd><p>Set the extended filesystem attribute <em>attribute</em> on <em>path</em> to <em>value</em>.
  3996. <em>attribute</em> must be a bytes or str with no embedded NULs (directly or
  3997. indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathLike</span></code></a> interface). If it is a str,
  3998. it is encoded with the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>. <em>flags</em> may be
  3999. <a class="reference internal" href="#os.XATTR_REPLACE" title="os.XATTR_REPLACE"><code class="xref py py-data docutils literal notranslate"><span class="pre">XATTR_REPLACE</span></code></a> or <a class="reference internal" href="#os.XATTR_CREATE" title="os.XATTR_CREATE"><code class="xref py py-data docutils literal notranslate"><span class="pre">XATTR_CREATE</span></code></a>. If <a class="reference internal" href="#os.XATTR_REPLACE" title="os.XATTR_REPLACE"><code class="xref py py-data docutils literal notranslate"><span class="pre">XATTR_REPLACE</span></code></a> is
  4000. given and the attribute does not exist, <code class="docutils literal notranslate"><span class="pre">ENODATA</span></code> will be raised.
  4001. If <a class="reference internal" href="#os.XATTR_CREATE" title="os.XATTR_CREATE"><code class="xref py py-data docutils literal notranslate"><span class="pre">XATTR_CREATE</span></code></a> is given and the attribute already exists, the
  4002. attribute will not be created and <code class="docutils literal notranslate"><span class="pre">EEXISTS</span></code> will be raised.</p>
  4003. <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and
  4004. <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p>
  4005. <div class="admonition note">
  4006. <p class="admonition-title">Note</p>
  4007. <p>A bug in Linux kernel versions less than 2.6.39 caused the flags argument
  4008. to be ignored on some filesystems.</p>
  4009. </div>
  4010. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.setxattr</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">attribute</span></code>, <code class="docutils literal notranslate"><span class="pre">value</span></code>, <code class="docutils literal notranslate"><span class="pre">flags</span></code>.</p>
  4011. <div class="versionchanged">
  4012. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>path</em> and <em>attribute</em>.</p>
  4013. </div>
  4014. </dd></dl>
  4015. <dl class="py data">
  4016. <dt class="sig sig-object py" id="os.XATTR_SIZE_MAX">
  4017. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">XATTR_SIZE_MAX</span></span><a class="headerlink" href="#os.XATTR_SIZE_MAX" title="Link to this definition">¶</a></dt>
  4018. <dd><p>The maximum size the value of an extended attribute can be. Currently, this
  4019. is 64 KiB on Linux.</p>
  4020. </dd></dl>
  4021. <dl class="py data">
  4022. <dt class="sig sig-object py" id="os.XATTR_CREATE">
  4023. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">XATTR_CREATE</span></span><a class="headerlink" href="#os.XATTR_CREATE" title="Link to this definition">¶</a></dt>
  4024. <dd><p>This is a possible value for the flags argument in <a class="reference internal" href="#os.setxattr" title="os.setxattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">setxattr()</span></code></a>. It
  4025. indicates the operation must create an attribute.</p>
  4026. </dd></dl>
  4027. <dl class="py data">
  4028. <dt class="sig sig-object py" id="os.XATTR_REPLACE">
  4029. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">XATTR_REPLACE</span></span><a class="headerlink" href="#os.XATTR_REPLACE" title="Link to this definition">¶</a></dt>
  4030. <dd><p>This is a possible value for the flags argument in <a class="reference internal" href="#os.setxattr" title="os.setxattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">setxattr()</span></code></a>. It
  4031. indicates the operation must replace an existing attribute.</p>
  4032. </dd></dl>
  4033. </section>
  4034. </section>
  4035. <section id="process-management">
  4036. <span id="os-process"></span><h2>Process Management<a class="headerlink" href="#process-management" title="Link to this heading">¶</a></h2>
  4037. <p>These functions may be used to create and manage processes.</p>
  4038. <p>The various <a class="reference internal" href="#os.execl" title="os.execl"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec*</span></code></a> functions take a list of arguments for the new
  4039. program loaded into the process. In each case, the first of these arguments is
  4040. passed to the new program as its own name rather than as an argument a user may
  4041. have typed on a command line. For the C programmer, this is the <code class="docutils literal notranslate"><span class="pre">argv[0]</span></code>
  4042. passed to a program’s <code class="xref c c-func docutils literal notranslate"><span class="pre">main()</span></code>. For example, <code class="docutils literal notranslate"><span class="pre">os.execv('/bin/echo',</span>
  4043. <span class="pre">['foo',</span> <span class="pre">'bar'])</span></code> will only print <code class="docutils literal notranslate"><span class="pre">bar</span></code> on standard output; <code class="docutils literal notranslate"><span class="pre">foo</span></code> will seem
  4044. to be ignored.</p>
  4045. <dl class="py function">
  4046. <dt class="sig sig-object py" id="os.abort">
  4047. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">abort</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.abort" title="Link to this definition">¶</a></dt>
  4048. <dd><p>Generate a <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGABRT</span></code> signal to the current process. On Unix, the default
  4049. behavior is to produce a core dump; on Windows, the process immediately returns
  4050. an exit code of <code class="docutils literal notranslate"><span class="pre">3</span></code>. Be aware that calling this function will not call the
  4051. Python signal handler registered for <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGABRT</span></code> with
  4052. <a class="reference internal" href="signal.html#signal.signal" title="signal.signal"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.signal()</span></code></a>.</p>
  4053. </dd></dl>
  4054. <dl class="py function">
  4055. <dt class="sig sig-object py" id="os.add_dll_directory">
  4056. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">add_dll_directory</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.add_dll_directory" title="Link to this definition">¶</a></dt>
  4057. <dd><p>Add a path to the DLL search path.</p>
  4058. <p>This search path is used when resolving dependencies for imported
  4059. extension modules (the module itself is resolved through
  4060. <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>), and also by <a class="reference internal" href="ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code></a>.</p>
  4061. <p>Remove the directory by calling <strong>close()</strong> on the returned object
  4062. or using it 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.</p>
  4063. <p>See the <a class="reference external" href="https://msdn.microsoft.com/44228cf2-6306-466c-8f16-f513cd3ba8b5">Microsoft documentation</a>
  4064. for more information about how DLLs are loaded.</p>
  4065. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.add_dll_directory</span></code> with argument <code class="docutils literal notranslate"><span class="pre">path</span></code>.</p>
  4066. <div class="availability docutils container">
  4067. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  4068. </div>
  4069. <div class="versionadded">
  4070. <p><span class="versionmodified added">New in version 3.8: </span>Previous versions of CPython would resolve DLLs using the default
  4071. behavior for the current process. This led to inconsistencies,
  4072. such as only sometimes searching <span class="target" id="index-33"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> or the current
  4073. working directory, and OS functions such as <code class="docutils literal notranslate"><span class="pre">AddDllDirectory</span></code>
  4074. having no effect.</p>
  4075. <p>In 3.8, the two primary ways DLLs are loaded now explicitly
  4076. override the process-wide behavior to ensure consistency. See the
  4077. <a class="reference internal" href="../whatsnew/3.8.html#bpo-36085-whatsnew"><span class="std std-ref">porting notes</span></a> for information on
  4078. updating libraries.</p>
  4079. </div>
  4080. </dd></dl>
  4081. <dl class="py function">
  4082. <dt class="sig sig-object py" id="os.execl">
  4083. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">execl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.execl" title="Link to this definition">¶</a></dt>
  4084. <dt class="sig sig-object py" id="os.execle">
  4085. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">execle</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.execle" title="Link to this definition">¶</a></dt>
  4086. <dt class="sig sig-object py" id="os.execlp">
  4087. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">execlp</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">arg0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.execlp" title="Link to this definition">¶</a></dt>
  4088. <dt class="sig sig-object py" id="os.execlpe">
  4089. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">execlpe</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">arg0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.execlpe" title="Link to this definition">¶</a></dt>
  4090. <dt class="sig sig-object py" id="os.execv">
  4091. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">execv</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.execv" title="Link to this definition">¶</a></dt>
  4092. <dt class="sig sig-object py" id="os.execve">
  4093. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">execve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.execve" title="Link to this definition">¶</a></dt>
  4094. <dt class="sig sig-object py" id="os.execvp">
  4095. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">execvp</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">args</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.execvp" title="Link to this definition">¶</a></dt>
  4096. <dt class="sig sig-object py" id="os.execvpe">
  4097. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">execvpe</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">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.execvpe" title="Link to this definition">¶</a></dt>
  4098. <dd><p>These functions all execute a new program, replacing the current process; they
  4099. do not return. On Unix, the new executable is loaded into the current process,
  4100. and will have the same process id as the caller. Errors will be reported as
  4101. <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> exceptions.</p>
  4102. <p>The current process is replaced immediately. Open file objects and
  4103. descriptors are not flushed, so if there may be data buffered
  4104. on these open files, you should flush them using
  4105. <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.stdout.flush()</span></code> or <a class="reference internal" href="#os.fsync" title="os.fsync"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fsync()</span></code></a> before calling an
  4106. <a class="reference internal" href="#os.execl" title="os.execl"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec*</span></code></a> function.</p>
  4107. <p>The “l” and “v” variants of the <a class="reference internal" href="#os.execl" title="os.execl"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec*</span></code></a> functions differ in how
  4108. command-line arguments are passed. The “l” variants are perhaps the easiest
  4109. to work with if the number of parameters is fixed when the code is written; the
  4110. individual parameters simply become additional parameters to the <code class="xref py py-func docutils literal notranslate"><span class="pre">execl*()</span></code>
  4111. functions. The “v” variants are good when the number of parameters is
  4112. variable, with the arguments being passed in a list or tuple as the <em>args</em>
  4113. parameter. In either case, the arguments to the child process should start with
  4114. the name of the command being run, but this is not enforced.</p>
  4115. <p>The variants which include a “p” near the end (<a class="reference internal" href="#os.execlp" title="os.execlp"><code class="xref py py-func docutils literal notranslate"><span class="pre">execlp()</span></code></a>,
  4116. <a class="reference internal" href="#os.execlpe" title="os.execlpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">execlpe()</span></code></a>, <a class="reference internal" href="#os.execvp" title="os.execvp"><code class="xref py py-func docutils literal notranslate"><span class="pre">execvp()</span></code></a>, and <a class="reference internal" href="#os.execvpe" title="os.execvpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">execvpe()</span></code></a>) will use the
  4117. <span class="target" id="index-34"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> environment variable to locate the program <em>file</em>. When the
  4118. environment is being replaced (using one of the <a class="reference internal" href="#os.execl" title="os.execl"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec*e</span></code></a> variants,
  4119. discussed in the next paragraph), the new environment is used as the source of
  4120. the <span class="target" id="index-35"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> variable. The other variants, <a class="reference internal" href="#os.execl" title="os.execl"><code class="xref py py-func docutils literal notranslate"><span class="pre">execl()</span></code></a>, <a class="reference internal" href="#os.execle" title="os.execle"><code class="xref py py-func docutils literal notranslate"><span class="pre">execle()</span></code></a>,
  4121. <a class="reference internal" href="#os.execv" title="os.execv"><code class="xref py py-func docutils literal notranslate"><span class="pre">execv()</span></code></a>, and <a class="reference internal" href="#os.execve" title="os.execve"><code class="xref py py-func docutils literal notranslate"><span class="pre">execve()</span></code></a>, will not use the <span class="target" id="index-36"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> variable to
  4122. locate the executable; <em>path</em> must contain an appropriate absolute or relative
  4123. path. Relative paths must include at least one slash, even on Windows, as
  4124. plain names will not be resolved.</p>
  4125. <p>For <a class="reference internal" href="#os.execle" title="os.execle"><code class="xref py py-func docutils literal notranslate"><span class="pre">execle()</span></code></a>, <a class="reference internal" href="#os.execlpe" title="os.execlpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">execlpe()</span></code></a>, <a class="reference internal" href="#os.execve" title="os.execve"><code class="xref py py-func docutils literal notranslate"><span class="pre">execve()</span></code></a>, and <a class="reference internal" href="#os.execvpe" title="os.execvpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">execvpe()</span></code></a> (note
  4126. that these all end in “e”), the <em>env</em> parameter must be a mapping which is
  4127. used to define the environment variables for the new process (these are used
  4128. instead of the current process’ environment); the functions <a class="reference internal" href="#os.execl" title="os.execl"><code class="xref py py-func docutils literal notranslate"><span class="pre">execl()</span></code></a>,
  4129. <a class="reference internal" href="#os.execlp" title="os.execlp"><code class="xref py py-func docutils literal notranslate"><span class="pre">execlp()</span></code></a>, <a class="reference internal" href="#os.execv" title="os.execv"><code class="xref py py-func docutils literal notranslate"><span class="pre">execv()</span></code></a>, and <a class="reference internal" href="#os.execvp" title="os.execvp"><code class="xref py py-func docutils literal notranslate"><span class="pre">execvp()</span></code></a> all cause the new process to
  4130. inherit the environment of the current process.</p>
  4131. <p>For <a class="reference internal" href="#os.execve" title="os.execve"><code class="xref py py-func docutils literal notranslate"><span class="pre">execve()</span></code></a> on some platforms, <em>path</em> may also be specified as an open
  4132. file descriptor. This functionality may not be supported on your platform;
  4133. you can check whether or not it is available using <a class="reference internal" href="#os.supports_fd" title="os.supports_fd"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.supports_fd</span></code></a>.
  4134. If it is unavailable, using it will raise a <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  4135. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.exec</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">args</span></code>, <code class="docutils literal notranslate"><span class="pre">env</span></code>.</p>
  4136. <div class="availability docutils container">
  4137. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p>
  4138. </div>
  4139. <div class="versionchanged">
  4140. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor
  4141. for <a class="reference internal" href="#os.execve" title="os.execve"><code class="xref py py-func docutils literal notranslate"><span class="pre">execve()</span></code></a>.</p>
  4142. </div>
  4143. <div class="versionchanged">
  4144. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  4145. </div>
  4146. </dd></dl>
  4147. <dl class="py function">
  4148. <dt class="sig sig-object py" id="os._exit">
  4149. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">_exit</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os._exit" title="Link to this definition">¶</a></dt>
  4150. <dd><p>Exit the process with status <em>n</em>, without calling cleanup handlers, flushing
  4151. stdio buffers, etc.</p>
  4152. <div class="admonition note">
  4153. <p class="admonition-title">Note</p>
  4154. <p>The standard way to exit is <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit(n)</span></code></a>. <code class="xref py py-func docutils literal notranslate"><span class="pre">_exit()</span></code> should
  4155. normally only be used in the child process after a <a class="reference internal" href="#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">fork()</span></code></a>.</p>
  4156. </div>
  4157. </dd></dl>
  4158. <p>The following exit codes are defined and can be used with <a class="reference internal" href="#os._exit" title="os._exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">_exit()</span></code></a>,
  4159. although they are not required. These are typically used for system programs
  4160. written in Python, such as a mail server’s external command delivery program.</p>
  4161. <div class="admonition note">
  4162. <p class="admonition-title">Note</p>
  4163. <p>Some of these may not be available on all Unix platforms, since there is some
  4164. variation. These constants are defined where they are defined by the underlying
  4165. platform.</p>
  4166. </div>
  4167. <dl class="py data">
  4168. <dt class="sig sig-object py" id="os.EX_OK">
  4169. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_OK</span></span><a class="headerlink" href="#os.EX_OK" title="Link to this definition">¶</a></dt>
  4170. <dd><p>Exit code that means no error occurred. May be taken from the defined value of
  4171. <code class="docutils literal notranslate"><span class="pre">EXIT_SUCCESS</span></code> on some platforms. Generally has a value of zero.</p>
  4172. <div class="availability docutils container">
  4173. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  4174. </div>
  4175. </dd></dl>
  4176. <dl class="py data">
  4177. <dt class="sig sig-object py" id="os.EX_USAGE">
  4178. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_USAGE</span></span><a class="headerlink" href="#os.EX_USAGE" title="Link to this definition">¶</a></dt>
  4179. <dd><p>Exit code that means the command was used incorrectly, such as when the wrong
  4180. number of arguments are given.</p>
  4181. <div class="availability docutils container">
  4182. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4183. </div>
  4184. </dd></dl>
  4185. <dl class="py data">
  4186. <dt class="sig sig-object py" id="os.EX_DATAERR">
  4187. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_DATAERR</span></span><a class="headerlink" href="#os.EX_DATAERR" title="Link to this definition">¶</a></dt>
  4188. <dd><p>Exit code that means the input data was incorrect.</p>
  4189. <div class="availability docutils container">
  4190. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4191. </div>
  4192. </dd></dl>
  4193. <dl class="py data">
  4194. <dt class="sig sig-object py" id="os.EX_NOINPUT">
  4195. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_NOINPUT</span></span><a class="headerlink" href="#os.EX_NOINPUT" title="Link to this definition">¶</a></dt>
  4196. <dd><p>Exit code that means an input file did not exist or was not readable.</p>
  4197. <div class="availability docutils container">
  4198. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4199. </div>
  4200. </dd></dl>
  4201. <dl class="py data">
  4202. <dt class="sig sig-object py" id="os.EX_NOUSER">
  4203. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_NOUSER</span></span><a class="headerlink" href="#os.EX_NOUSER" title="Link to this definition">¶</a></dt>
  4204. <dd><p>Exit code that means a specified user did not exist.</p>
  4205. <div class="availability docutils container">
  4206. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4207. </div>
  4208. </dd></dl>
  4209. <dl class="py data">
  4210. <dt class="sig sig-object py" id="os.EX_NOHOST">
  4211. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_NOHOST</span></span><a class="headerlink" href="#os.EX_NOHOST" title="Link to this definition">¶</a></dt>
  4212. <dd><p>Exit code that means a specified host did not exist.</p>
  4213. <div class="availability docutils container">
  4214. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4215. </div>
  4216. </dd></dl>
  4217. <dl class="py data">
  4218. <dt class="sig sig-object py" id="os.EX_UNAVAILABLE">
  4219. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_UNAVAILABLE</span></span><a class="headerlink" href="#os.EX_UNAVAILABLE" title="Link to this definition">¶</a></dt>
  4220. <dd><p>Exit code that means that a required service is unavailable.</p>
  4221. <div class="availability docutils container">
  4222. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4223. </div>
  4224. </dd></dl>
  4225. <dl class="py data">
  4226. <dt class="sig sig-object py" id="os.EX_SOFTWARE">
  4227. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_SOFTWARE</span></span><a class="headerlink" href="#os.EX_SOFTWARE" title="Link to this definition">¶</a></dt>
  4228. <dd><p>Exit code that means an internal software error was detected.</p>
  4229. <div class="availability docutils container">
  4230. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4231. </div>
  4232. </dd></dl>
  4233. <dl class="py data">
  4234. <dt class="sig sig-object py" id="os.EX_OSERR">
  4235. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_OSERR</span></span><a class="headerlink" href="#os.EX_OSERR" title="Link to this definition">¶</a></dt>
  4236. <dd><p>Exit code that means an operating system error was detected, such as the
  4237. inability to fork or create a pipe.</p>
  4238. <div class="availability docutils container">
  4239. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4240. </div>
  4241. </dd></dl>
  4242. <dl class="py data">
  4243. <dt class="sig sig-object py" id="os.EX_OSFILE">
  4244. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_OSFILE</span></span><a class="headerlink" href="#os.EX_OSFILE" title="Link to this definition">¶</a></dt>
  4245. <dd><p>Exit code that means some system file did not exist, could not be opened, or had
  4246. some other kind of error.</p>
  4247. <div class="availability docutils container">
  4248. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4249. </div>
  4250. </dd></dl>
  4251. <dl class="py data">
  4252. <dt class="sig sig-object py" id="os.EX_CANTCREAT">
  4253. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_CANTCREAT</span></span><a class="headerlink" href="#os.EX_CANTCREAT" title="Link to this definition">¶</a></dt>
  4254. <dd><p>Exit code that means a user specified output file could not be created.</p>
  4255. <div class="availability docutils container">
  4256. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4257. </div>
  4258. </dd></dl>
  4259. <dl class="py data">
  4260. <dt class="sig sig-object py" id="os.EX_IOERR">
  4261. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_IOERR</span></span><a class="headerlink" href="#os.EX_IOERR" title="Link to this definition">¶</a></dt>
  4262. <dd><p>Exit code that means that an error occurred while doing I/O on some file.</p>
  4263. <div class="availability docutils container">
  4264. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4265. </div>
  4266. </dd></dl>
  4267. <dl class="py data">
  4268. <dt class="sig sig-object py" id="os.EX_TEMPFAIL">
  4269. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_TEMPFAIL</span></span><a class="headerlink" href="#os.EX_TEMPFAIL" title="Link to this definition">¶</a></dt>
  4270. <dd><p>Exit code that means a temporary failure occurred. This indicates something
  4271. that may not really be an error, such as a network connection that couldn’t be
  4272. made during a retryable operation.</p>
  4273. <div class="availability docutils container">
  4274. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4275. </div>
  4276. </dd></dl>
  4277. <dl class="py data">
  4278. <dt class="sig sig-object py" id="os.EX_PROTOCOL">
  4279. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_PROTOCOL</span></span><a class="headerlink" href="#os.EX_PROTOCOL" title="Link to this definition">¶</a></dt>
  4280. <dd><p>Exit code that means that a protocol exchange was illegal, invalid, or not
  4281. understood.</p>
  4282. <div class="availability docutils container">
  4283. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4284. </div>
  4285. </dd></dl>
  4286. <dl class="py data">
  4287. <dt class="sig sig-object py" id="os.EX_NOPERM">
  4288. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_NOPERM</span></span><a class="headerlink" href="#os.EX_NOPERM" title="Link to this definition">¶</a></dt>
  4289. <dd><p>Exit code that means that there were insufficient permissions to perform the
  4290. operation (but not intended for file system problems).</p>
  4291. <div class="availability docutils container">
  4292. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4293. </div>
  4294. </dd></dl>
  4295. <dl class="py data">
  4296. <dt class="sig sig-object py" id="os.EX_CONFIG">
  4297. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_CONFIG</span></span><a class="headerlink" href="#os.EX_CONFIG" title="Link to this definition">¶</a></dt>
  4298. <dd><p>Exit code that means that some kind of configuration error occurred.</p>
  4299. <div class="availability docutils container">
  4300. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4301. </div>
  4302. </dd></dl>
  4303. <dl class="py data">
  4304. <dt class="sig sig-object py" id="os.EX_NOTFOUND">
  4305. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">EX_NOTFOUND</span></span><a class="headerlink" href="#os.EX_NOTFOUND" title="Link to this definition">¶</a></dt>
  4306. <dd><p>Exit code that means something like “an entry was not found”.</p>
  4307. <div class="availability docutils container">
  4308. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4309. </div>
  4310. </dd></dl>
  4311. <dl class="py function">
  4312. <dt class="sig sig-object py" id="os.fork">
  4313. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">fork</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.fork" title="Link to this definition">¶</a></dt>
  4314. <dd><p>Fork a child process. Return <code class="docutils literal notranslate"><span class="pre">0</span></code> in the child and the child’s process id in the
  4315. parent. If an error occurs <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised.</p>
  4316. <p>Note that some platforms including FreeBSD &lt;= 6.3 and Cygwin have
  4317. known issues when using <code class="docutils literal notranslate"><span class="pre">fork()</span></code> from a thread.</p>
  4318. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.fork</span></code> with no arguments.</p>
  4319. <div class="admonition warning">
  4320. <p class="admonition-title">Warning</p>
  4321. <p>If you use TLS sockets in an application calling <code class="docutils literal notranslate"><span class="pre">fork()</span></code>, see
  4322. the warning in the <a class="reference internal" href="ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> documentation.</p>
  4323. </div>
  4324. <div class="admonition warning">
  4325. <p class="admonition-title">Warning</p>
  4326. <p>On macOS the use of this function is unsafe when mixed with using
  4327. higher-level system APIs, and that includes using <a class="reference internal" href="urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code></a>.</p>
  4328. </div>
  4329. <div class="versionchanged">
  4330. <p><span class="versionmodified changed">Changed in version 3.8: </span>Calling <code class="docutils literal notranslate"><span class="pre">fork()</span></code> in a subinterpreter is no longer supported
  4331. (<a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised).</p>
  4332. </div>
  4333. <div class="versionchanged">
  4334. <p><span class="versionmodified changed">Changed in version 3.12: </span>If Python is able to detect that your process has multiple
  4335. threads, <a class="reference internal" href="#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> now raises a <a class="reference internal" href="exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>.</p>
  4336. <p>We chose to surface this as a warning, when detectable, to better
  4337. inform developers of a design problem that the POSIX platform
  4338. specifically notes as not supported. Even in code that
  4339. <em>appears</em> to work, it has never been safe to mix threading with
  4340. <a class="reference internal" href="#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> on POSIX platforms. The CPython runtime itself has
  4341. always made API calls that are not safe for use in the child
  4342. process when threads existed in the parent (such as <code class="docutils literal notranslate"><span class="pre">malloc</span></code> and
  4343. <code class="docutils literal notranslate"><span class="pre">free</span></code>).</p>
  4344. <p>Users of macOS or users of libc or malloc implementations other
  4345. than those typically found in glibc to date are among those
  4346. already more likely to experience deadlocks running such code.</p>
  4347. <p>See <a class="reference external" href="https://discuss.python.org/t/33555">this discussion on fork being incompatible with threads</a>
  4348. for technical details of why we’re surfacing this longstanding
  4349. platform compatibility problem to developers.</p>
  4350. </div>
  4351. <div class="availability docutils container">
  4352. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX, not Emscripten, not WASI.</p>
  4353. </div>
  4354. </dd></dl>
  4355. <dl class="py function">
  4356. <dt class="sig sig-object py" id="os.forkpty">
  4357. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">forkpty</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.forkpty" title="Link to this definition">¶</a></dt>
  4358. <dd><p>Fork a child process, using a new pseudo-terminal as the child’s controlling
  4359. terminal. Return a pair of <code class="docutils literal notranslate"><span class="pre">(pid,</span> <span class="pre">fd)</span></code>, where <em>pid</em> is <code class="docutils literal notranslate"><span class="pre">0</span></code> in the child, the
  4360. new child’s process id in the parent, and <em>fd</em> is the file descriptor of the
  4361. master end of the pseudo-terminal. For a more portable approach, use the
  4362. <a class="reference internal" href="pty.html#module-pty" title="pty: Pseudo-Terminal Handling for Unix. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pty</span></code></a> module. If an error occurs <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised.</p>
  4363. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.forkpty</span></code> with no arguments.</p>
  4364. <div class="admonition warning">
  4365. <p class="admonition-title">Warning</p>
  4366. <p>On macOS the use of this function is unsafe when mixed with using
  4367. higher-level system APIs, and that includes using <a class="reference internal" href="urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code></a>.</p>
  4368. </div>
  4369. <div class="versionchanged">
  4370. <p><span class="versionmodified changed">Changed in version 3.8: </span>Calling <code class="docutils literal notranslate"><span class="pre">forkpty()</span></code> in a subinterpreter is no longer supported
  4371. (<a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised).</p>
  4372. </div>
  4373. <div class="versionchanged">
  4374. <p><span class="versionmodified changed">Changed in version 3.12: </span>If Python is able to detect that your process has multiple
  4375. threads, this now raises a <a class="reference internal" href="exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>. See the
  4376. longer explanation on <a class="reference internal" href="#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a>.</p>
  4377. </div>
  4378. <div class="availability docutils container">
  4379. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4380. </div>
  4381. </dd></dl>
  4382. <dl class="py function">
  4383. <dt class="sig sig-object py" id="os.kill">
  4384. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">kill</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sig</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="#os.kill" title="Link to this definition">¶</a></dt>
  4385. <dd><p id="index-37">Send signal <em>sig</em> to the process <em>pid</em>. Constants for the specific signals
  4386. available on the host platform are defined in the <a class="reference internal" href="signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code></a> module.</p>
  4387. <p>Windows: The <a class="reference internal" href="signal.html#signal.CTRL_C_EVENT" title="signal.CTRL_C_EVENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.CTRL_C_EVENT</span></code></a> and
  4388. <a class="reference internal" href="signal.html#signal.CTRL_BREAK_EVENT" title="signal.CTRL_BREAK_EVENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.CTRL_BREAK_EVENT</span></code></a> signals are special signals which can
  4389. only be sent to console processes which share a common console window,
  4390. e.g., some subprocesses. Any other value for <em>sig</em> will cause the process
  4391. to be unconditionally killed by the TerminateProcess API, and the exit code
  4392. will be set to <em>sig</em>. The Windows version of <a class="reference internal" href="#os.kill" title="os.kill"><code class="xref py py-func docutils literal notranslate"><span class="pre">kill()</span></code></a> additionally takes
  4393. process handles to be killed.</p>
  4394. <p>See also <a class="reference internal" href="signal.html#signal.pthread_kill" title="signal.pthread_kill"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.pthread_kill()</span></code></a>.</p>
  4395. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.kill</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">pid</span></code>, <code class="docutils literal notranslate"><span class="pre">sig</span></code>.</p>
  4396. <div class="availability docutils container">
  4397. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p>
  4398. </div>
  4399. <div class="versionchanged">
  4400. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added Windows support.</p>
  4401. </div>
  4402. </dd></dl>
  4403. <dl class="py function">
  4404. <dt class="sig sig-object py" id="os.killpg">
  4405. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">killpg</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pgid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sig</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="#os.killpg" title="Link to this definition">¶</a></dt>
  4406. <dd><p id="index-38">Send the signal <em>sig</em> to the process group <em>pgid</em>.</p>
  4407. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.killpg</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">pgid</span></code>, <code class="docutils literal notranslate"><span class="pre">sig</span></code>.</p>
  4408. <div class="availability docutils container">
  4409. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4410. </div>
  4411. </dd></dl>
  4412. <dl class="py function">
  4413. <dt class="sig sig-object py" id="os.nice">
  4414. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">nice</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">increment</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="#os.nice" title="Link to this definition">¶</a></dt>
  4415. <dd><p>Add <em>increment</em> to the process’s “niceness”. Return the new niceness.</p>
  4416. <div class="availability docutils container">
  4417. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4418. </div>
  4419. </dd></dl>
  4420. <dl class="py function">
  4421. <dt class="sig sig-object py" id="os.pidfd_open">
  4422. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pidfd_open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.pidfd_open" title="Link to this definition">¶</a></dt>
  4423. <dd><p>Return a file descriptor referring to the process <em>pid</em> with <em>flags</em> set.
  4424. This descriptor can be used to perform process management without races
  4425. and signals.</p>
  4426. <p>See the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/pidfd_open(2)">pidfd_open(2)</a></em> man page for more details.</p>
  4427. <div class="availability docutils container">
  4428. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 5.3</p>
  4429. </div>
  4430. <div class="versionadded">
  4431. <p><span class="versionmodified added">New in version 3.9.</span></p>
  4432. </div>
  4433. <dl class="py data">
  4434. <dt class="sig sig-object py" id="os.PIDFD_NONBLOCK">
  4435. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">PIDFD_NONBLOCK</span></span><a class="headerlink" href="#os.PIDFD_NONBLOCK" title="Link to this definition">¶</a></dt>
  4436. <dd><p>This flag indicates that the file descriptor will be non-blocking.
  4437. If the process referred to by the file descriptor has not yet terminated,
  4438. then an attempt to wait on the file descriptor using <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/waitid(2)">waitid(2)</a></em>
  4439. will immediately return the error <a class="reference internal" href="errno.html#errno.EAGAIN" title="errno.EAGAIN"><code class="xref py py-const docutils literal notranslate"><span class="pre">EAGAIN</span></code></a> rather than blocking.</p>
  4440. </dd></dl>
  4441. <div class="availability docutils container">
  4442. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 5.10</p>
  4443. </div>
  4444. <div class="versionadded">
  4445. <p><span class="versionmodified added">New in version 3.12.</span></p>
  4446. </div>
  4447. </dd></dl>
  4448. <dl class="py function">
  4449. <dt class="sig sig-object py" id="os.plock">
  4450. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">plock</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">op</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="#os.plock" title="Link to this definition">¶</a></dt>
  4451. <dd><p>Lock program segments into memory. The value of <em>op</em> (defined in
  4452. <code class="docutils literal notranslate"><span class="pre">&lt;sys/lock.h&gt;</span></code>) determines which segments are locked.</p>
  4453. <div class="availability docutils container">
  4454. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4455. </div>
  4456. </dd></dl>
  4457. <dl class="py function">
  4458. <dt class="sig sig-object py" id="os.popen">
  4459. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">popen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffering</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.popen" title="Link to this definition">¶</a></dt>
  4460. <dd><p>Open a pipe to or from command <em>cmd</em>.
  4461. The return value is an open file object
  4462. connected to the pipe, which can be read or written depending on whether <em>mode</em>
  4463. is <code class="docutils literal notranslate"><span class="pre">'r'</span></code> (default) or <code class="docutils literal notranslate"><span class="pre">'w'</span></code>.
  4464. The <em>buffering</em> argument have the same meaning as
  4465. the corresponding argument to the built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function. The
  4466. returned file object reads or writes text strings rather than bytes.</p>
  4467. <p>The <code class="docutils literal notranslate"><span class="pre">close</span></code> method returns <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if the subprocess exited
  4468. successfully, or the subprocess’s return code if there was an
  4469. error. On POSIX systems, if the return code is positive it
  4470. represents the return value of the process left-shifted by one
  4471. byte. If the return code is negative, the process was terminated
  4472. by the signal given by the negated value of the return code. (For
  4473. example, the return value might be <code class="docutils literal notranslate"><span class="pre">-</span> <span class="pre">signal.SIGKILL</span></code> if the
  4474. subprocess was killed.) On Windows systems, the return value
  4475. contains the signed integer return code from the child process.</p>
  4476. <p>On Unix, <a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitstatus_to_exitcode()</span></code></a> can be used to convert the <code class="docutils literal notranslate"><span class="pre">close</span></code>
  4477. method result (exit status) into an exit code if it is not <code class="docutils literal notranslate"><span class="pre">None</span></code>. On
  4478. Windows, the <code class="docutils literal notranslate"><span class="pre">close</span></code> method result is directly the exit code
  4479. (or <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p>
  4480. <p>This is implemented using <a class="reference internal" href="subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a>; see that class’s
  4481. documentation for more powerful ways to manage and communicate with
  4482. subprocesses.</p>
  4483. <div class="availability docutils container">
  4484. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  4485. </div>
  4486. <div class="admonition note">
  4487. <p class="admonition-title">Note</p>
  4488. <p>The <a class="reference internal" href="#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> affects encodings used
  4489. for <em>cmd</em> and pipe contents.</p>
  4490. <p><a class="reference internal" href="#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">popen()</span></code></a> is a simple wrapper around <a class="reference internal" href="subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a>.
  4491. Use <a class="reference internal" href="subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a> or <a class="reference internal" href="subprocess.html#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">subprocess.run()</span></code></a> to
  4492. control options like encodings.</p>
  4493. </div>
  4494. </dd></dl>
  4495. <dl class="py function">
  4496. <dt class="sig sig-object py" id="os.posix_spawn">
  4497. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">posix_spawn</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">argv</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file_actions</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setpgroup</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">resetids</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setsid</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setsigmask</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setsigdef</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">scheduler</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.posix_spawn" title="Link to this definition">¶</a></dt>
  4498. <dd><p>Wraps the <code class="xref c c-func docutils literal notranslate"><span class="pre">posix_spawn()</span></code> C library API for use from Python.</p>
  4499. <p>Most users should use <a class="reference internal" href="subprocess.html#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">subprocess.run()</span></code></a> instead of <a class="reference internal" href="#os.posix_spawn" title="os.posix_spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">posix_spawn()</span></code></a>.</p>
  4500. <p>The positional-only arguments <em>path</em>, <em>args</em>, and <em>env</em> are similar to
  4501. <a class="reference internal" href="#os.execve" title="os.execve"><code class="xref py py-func docutils literal notranslate"><span class="pre">execve()</span></code></a>.</p>
  4502. <p>The <em>path</em> parameter is the path to the executable file. The <em>path</em> should
  4503. contain a directory. Use <a class="reference internal" href="#os.posix_spawnp" title="os.posix_spawnp"><code class="xref py py-func docutils literal notranslate"><span class="pre">posix_spawnp()</span></code></a> to pass an executable file
  4504. without directory.</p>
  4505. <p>The <em>file_actions</em> argument may be a sequence of tuples describing actions
  4506. to take on specific file descriptors in the child process between the C
  4507. library implementation’s <code class="xref c c-func docutils literal notranslate"><span class="pre">fork()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">exec()</span></code> steps.
  4508. The first item in each tuple must be one of the three type indicator
  4509. listed below describing the remaining tuple elements:</p>
  4510. <dl class="py data">
  4511. <dt class="sig sig-object py" id="os.POSIX_SPAWN_OPEN">
  4512. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">POSIX_SPAWN_OPEN</span></span><a class="headerlink" href="#os.POSIX_SPAWN_OPEN" title="Link to this definition">¶</a></dt>
  4513. <dd><p>(<code class="docutils literal notranslate"><span class="pre">os.POSIX_SPAWN_OPEN</span></code>, <em>fd</em>, <em>path</em>, <em>flags</em>, <em>mode</em>)</p>
  4514. <p>Performs <code class="docutils literal notranslate"><span class="pre">os.dup2(os.open(path,</span> <span class="pre">flags,</span> <span class="pre">mode),</span> <span class="pre">fd)</span></code>.</p>
  4515. </dd></dl>
  4516. <dl class="py data">
  4517. <dt class="sig sig-object py" id="os.POSIX_SPAWN_CLOSE">
  4518. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">POSIX_SPAWN_CLOSE</span></span><a class="headerlink" href="#os.POSIX_SPAWN_CLOSE" title="Link to this definition">¶</a></dt>
  4519. <dd><p>(<code class="docutils literal notranslate"><span class="pre">os.POSIX_SPAWN_CLOSE</span></code>, <em>fd</em>)</p>
  4520. <p>Performs <code class="docutils literal notranslate"><span class="pre">os.close(fd)</span></code>.</p>
  4521. </dd></dl>
  4522. <dl class="py data">
  4523. <dt class="sig sig-object py" id="os.POSIX_SPAWN_DUP2">
  4524. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">POSIX_SPAWN_DUP2</span></span><a class="headerlink" href="#os.POSIX_SPAWN_DUP2" title="Link to this definition">¶</a></dt>
  4525. <dd><p>(<code class="docutils literal notranslate"><span class="pre">os.POSIX_SPAWN_DUP2</span></code>, <em>fd</em>, <em>new_fd</em>)</p>
  4526. <p>Performs <code class="docutils literal notranslate"><span class="pre">os.dup2(fd,</span> <span class="pre">new_fd)</span></code>.</p>
  4527. </dd></dl>
  4528. <p>These tuples correspond to the C library
  4529. <code class="xref c c-func docutils literal notranslate"><span class="pre">posix_spawn_file_actions_addopen()</span></code>,
  4530. <code class="xref c c-func docutils literal notranslate"><span class="pre">posix_spawn_file_actions_addclose()</span></code>, and
  4531. <code class="xref c c-func docutils literal notranslate"><span class="pre">posix_spawn_file_actions_adddup2()</span></code> API calls used to prepare
  4532. for the <code class="xref c c-func docutils literal notranslate"><span class="pre">posix_spawn()</span></code> call itself.</p>
  4533. <p>The <em>setpgroup</em> argument will set the process group of the child to the value
  4534. specified. If the value specified is 0, the child’s process group ID will be
  4535. made the same as its process ID. If the value of <em>setpgroup</em> is not set, the
  4536. child will inherit the parent’s process group ID. This argument corresponds
  4537. to the C library <code class="xref c c-macro docutils literal notranslate"><span class="pre">POSIX_SPAWN_SETPGROUP</span></code> flag.</p>
  4538. <p>If the <em>resetids</em> argument is <code class="docutils literal notranslate"><span class="pre">True</span></code> it will reset the effective UID and
  4539. GID of the child to the real UID and GID of the parent process. If the
  4540. argument is <code class="docutils literal notranslate"><span class="pre">False</span></code>, then the child retains the effective UID and GID of
  4541. the parent. In either case, if the set-user-ID and set-group-ID permission
  4542. bits are enabled on the executable file, their effect will override the
  4543. setting of the effective UID and GID. This argument corresponds to the C
  4544. library <code class="xref c c-macro docutils literal notranslate"><span class="pre">POSIX_SPAWN_RESETIDS</span></code> flag.</p>
  4545. <p>If the <em>setsid</em> argument is <code class="docutils literal notranslate"><span class="pre">True</span></code>, it will create a new session ID
  4546. for <code class="docutils literal notranslate"><span class="pre">posix_spawn</span></code>. <em>setsid</em> requires <code class="xref c c-macro docutils literal notranslate"><span class="pre">POSIX_SPAWN_SETSID</span></code>
  4547. or <code class="xref c c-macro docutils literal notranslate"><span class="pre">POSIX_SPAWN_SETSID_NP</span></code> flag. Otherwise, <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>
  4548. is raised.</p>
  4549. <p>The <em>setsigmask</em> argument will set the signal mask to the signal set
  4550. specified. If the parameter is not used, then the child inherits the
  4551. parent’s signal mask. This argument corresponds to the C library
  4552. <code class="xref c c-macro docutils literal notranslate"><span class="pre">POSIX_SPAWN_SETSIGMASK</span></code> flag.</p>
  4553. <p>The <em>sigdef</em> argument will reset the disposition of all signals in the set
  4554. specified. This argument corresponds to the C library
  4555. <code class="xref c c-macro docutils literal notranslate"><span class="pre">POSIX_SPAWN_SETSIGDEF</span></code> flag.</p>
  4556. <p>The <em>scheduler</em> argument must be a tuple containing the (optional) scheduler
  4557. policy and an instance of <a class="reference internal" href="#os.sched_param" title="os.sched_param"><code class="xref py py-class docutils literal notranslate"><span class="pre">sched_param</span></code></a> with the scheduler parameters.
  4558. A value of <code class="docutils literal notranslate"><span class="pre">None</span></code> in the place of the scheduler policy indicates that is
  4559. not being provided. This argument is a combination of the C library
  4560. <code class="xref c c-macro docutils literal notranslate"><span class="pre">POSIX_SPAWN_SETSCHEDPARAM</span></code> and <code class="xref c c-macro docutils literal notranslate"><span class="pre">POSIX_SPAWN_SETSCHEDULER</span></code>
  4561. flags.</p>
  4562. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.posix_spawn</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">argv</span></code>, <code class="docutils literal notranslate"><span class="pre">env</span></code>.</p>
  4563. <div class="versionadded">
  4564. <p><span class="versionmodified added">New in version 3.8.</span></p>
  4565. </div>
  4566. <div class="availability docutils container">
  4567. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4568. </div>
  4569. </dd></dl>
  4570. <dl class="py function">
  4571. <dt class="sig sig-object py" id="os.posix_spawnp">
  4572. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">posix_spawnp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">argv</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file_actions</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setpgroup</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">resetids</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setsid</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setsigmask</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setsigdef</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">scheduler</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.posix_spawnp" title="Link to this definition">¶</a></dt>
  4573. <dd><p>Wraps the <code class="xref c c-func docutils literal notranslate"><span class="pre">posix_spawnp()</span></code> C library API for use from Python.</p>
  4574. <p>Similar to <a class="reference internal" href="#os.posix_spawn" title="os.posix_spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">posix_spawn()</span></code></a> except that the system searches
  4575. for the <em>executable</em> file in the list of directories specified by the
  4576. <span class="target" id="index-39"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> environment variable (in the same way as for <code class="docutils literal notranslate"><span class="pre">execvp(3)</span></code>).</p>
  4577. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.posix_spawn</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">argv</span></code>, <code class="docutils literal notranslate"><span class="pre">env</span></code>.</p>
  4578. <div class="versionadded">
  4579. <p><span class="versionmodified added">New in version 3.8.</span></p>
  4580. </div>
  4581. <div class="availability docutils container">
  4582. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX, not Emscripten, not WASI.</p>
  4583. <p>See <a class="reference internal" href="#os.posix_spawn" title="os.posix_spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">posix_spawn()</span></code></a> documentation.</p>
  4584. </div>
  4585. </dd></dl>
  4586. <dl class="py function">
  4587. <dt class="sig sig-object py" id="os.register_at_fork">
  4588. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">register_at_fork</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">before</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">after_in_parent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">after_in_child</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.register_at_fork" title="Link to this definition">¶</a></dt>
  4589. <dd><p>Register callables to be executed when a new child process is forked
  4590. using <a class="reference internal" href="#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> or similar process cloning APIs.
  4591. The parameters are optional and keyword-only.
  4592. Each specifies a different call point.</p>
  4593. <ul class="simple">
  4594. <li><p><em>before</em> is a function called before forking a child process.</p></li>
  4595. <li><p><em>after_in_parent</em> is a function called from the parent process
  4596. after forking a child process.</p></li>
  4597. <li><p><em>after_in_child</em> is a function called from the child process.</p></li>
  4598. </ul>
  4599. <p>These calls are only made if control is expected to return to the
  4600. Python interpreter. A typical <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> launch will not
  4601. trigger them as the child is not going to re-enter the interpreter.</p>
  4602. <p>Functions registered for execution before forking are called in
  4603. reverse registration order. Functions registered for execution
  4604. after forking (either in the parent or in the child) are called
  4605. in registration order.</p>
  4606. <p>Note that <code class="xref c c-func docutils literal notranslate"><span class="pre">fork()</span></code> calls made by third-party C code may not
  4607. call those functions, unless it explicitly calls <a class="reference internal" href="../c-api/sys.html#c.PyOS_BeforeFork" title="PyOS_BeforeFork"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_BeforeFork()</span></code></a>,
  4608. <a class="reference internal" href="../c-api/sys.html#c.PyOS_AfterFork_Parent" title="PyOS_AfterFork_Parent"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_AfterFork_Parent()</span></code></a> and <a class="reference internal" href="../c-api/sys.html#c.PyOS_AfterFork_Child" title="PyOS_AfterFork_Child"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_AfterFork_Child()</span></code></a>.</p>
  4609. <p>There is no way to unregister a function.</p>
  4610. <div class="availability docutils container">
  4611. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4612. </div>
  4613. <div class="versionadded">
  4614. <p><span class="versionmodified added">New in version 3.7.</span></p>
  4615. </div>
  4616. </dd></dl>
  4617. <dl class="py function">
  4618. <dt class="sig sig-object py" id="os.spawnl">
  4619. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">spawnl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnl" title="Link to this definition">¶</a></dt>
  4620. <dt class="sig sig-object py" id="os.spawnle">
  4621. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">spawnle</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnle" title="Link to this definition">¶</a></dt>
  4622. <dt class="sig sig-object py" id="os.spawnlp">
  4623. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">spawnlp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnlp" title="Link to this definition">¶</a></dt>
  4624. <dt class="sig sig-object py" id="os.spawnlpe">
  4625. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">spawnlpe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnlpe" title="Link to this definition">¶</a></dt>
  4626. <dt class="sig sig-object py" id="os.spawnv">
  4627. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">spawnv</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnv" title="Link to this definition">¶</a></dt>
  4628. <dt class="sig sig-object py" id="os.spawnve">
  4629. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">spawnve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnve" title="Link to this definition">¶</a></dt>
  4630. <dt class="sig sig-object py" id="os.spawnvp">
  4631. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">spawnvp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnvp" title="Link to this definition">¶</a></dt>
  4632. <dt class="sig sig-object py" id="os.spawnvpe">
  4633. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">spawnvpe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnvpe" title="Link to this definition">¶</a></dt>
  4634. <dd><p>Execute the program <em>path</em> in a new process.</p>
  4635. <p>(Note that the <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module provides more powerful facilities for
  4636. spawning new processes and retrieving their results; using that module is
  4637. preferable to using these functions. Check especially the
  4638. <a class="reference internal" href="subprocess.html#subprocess-replacements"><span class="std std-ref">Replacing Older Functions with the subprocess Module</span></a> section.)</p>
  4639. <p>If <em>mode</em> is <a class="reference internal" href="#os.P_NOWAIT" title="os.P_NOWAIT"><code class="xref py py-const docutils literal notranslate"><span class="pre">P_NOWAIT</span></code></a>, this function returns the process id of the new
  4640. process; if <em>mode</em> is <a class="reference internal" href="#os.P_WAIT" title="os.P_WAIT"><code class="xref py py-const docutils literal notranslate"><span class="pre">P_WAIT</span></code></a>, returns the process’s exit code if it
  4641. exits normally, or <code class="docutils literal notranslate"><span class="pre">-signal</span></code>, where <em>signal</em> is the signal that killed the
  4642. process. On Windows, the process id will actually be the process handle, so can
  4643. be used with the <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a> function.</p>
  4644. <p>Note on VxWorks, this function doesn’t return <code class="docutils literal notranslate"><span class="pre">-signal</span></code> when the new process is
  4645. killed. Instead it raises OSError exception.</p>
  4646. <p>The “l” and “v” variants of the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*</span></code></a> functions differ in how
  4647. command-line arguments are passed. The “l” variants are perhaps the easiest
  4648. to work with if the number of parameters is fixed when the code is written; the
  4649. individual parameters simply become additional parameters to the
  4650. <code class="xref py py-func docutils literal notranslate"><span class="pre">spawnl*()</span></code> functions. The “v” variants are good when the number of
  4651. parameters is variable, with the arguments being passed in a list or tuple as
  4652. the <em>args</em> parameter. In either case, the arguments to the child process must
  4653. start with the name of the command being run.</p>
  4654. <p>The variants which include a second “p” near the end (<a class="reference internal" href="#os.spawnlp" title="os.spawnlp"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnlp()</span></code></a>,
  4655. <a class="reference internal" href="#os.spawnlpe" title="os.spawnlpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnlpe()</span></code></a>, <a class="reference internal" href="#os.spawnvp" title="os.spawnvp"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnvp()</span></code></a>, and <a class="reference internal" href="#os.spawnvpe" title="os.spawnvpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnvpe()</span></code></a>) will use the
  4656. <span class="target" id="index-40"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> environment variable to locate the program <em>file</em>. When the
  4657. environment is being replaced (using one of the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*e</span></code></a> variants,
  4658. discussed in the next paragraph), the new environment is used as the source of
  4659. the <span class="target" id="index-41"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> variable. The other variants, <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnl()</span></code></a>,
  4660. <a class="reference internal" href="#os.spawnle" title="os.spawnle"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnle()</span></code></a>, <a class="reference internal" href="#os.spawnv" title="os.spawnv"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnv()</span></code></a>, and <a class="reference internal" href="#os.spawnve" title="os.spawnve"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnve()</span></code></a>, will not use the
  4661. <span class="target" id="index-42"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> variable to locate the executable; <em>path</em> must contain an
  4662. appropriate absolute or relative path.</p>
  4663. <p>For <a class="reference internal" href="#os.spawnle" title="os.spawnle"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnle()</span></code></a>, <a class="reference internal" href="#os.spawnlpe" title="os.spawnlpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnlpe()</span></code></a>, <a class="reference internal" href="#os.spawnve" title="os.spawnve"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnve()</span></code></a>, and <a class="reference internal" href="#os.spawnvpe" title="os.spawnvpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnvpe()</span></code></a>
  4664. (note that these all end in “e”), the <em>env</em> parameter must be a mapping
  4665. which is used to define the environment variables for the new process (they are
  4666. used instead of the current process’ environment); the functions
  4667. <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnl()</span></code></a>, <a class="reference internal" href="#os.spawnlp" title="os.spawnlp"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnlp()</span></code></a>, <a class="reference internal" href="#os.spawnv" title="os.spawnv"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnv()</span></code></a>, and <a class="reference internal" href="#os.spawnvp" title="os.spawnvp"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnvp()</span></code></a> all cause
  4668. the new process to inherit the environment of the current process. Note that
  4669. keys and values in the <em>env</em> dictionary must be strings; invalid keys or
  4670. values will cause the function to fail, with a return value of <code class="docutils literal notranslate"><span class="pre">127</span></code>.</p>
  4671. <p>As an example, the following calls to <a class="reference internal" href="#os.spawnlp" title="os.spawnlp"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnlp()</span></code></a> and <a class="reference internal" href="#os.spawnvpe" title="os.spawnvpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnvpe()</span></code></a> are
  4672. equivalent:</p>
  4673. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
  4674. <span class="n">os</span><span class="o">.</span><span class="n">spawnlp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_WAIT</span><span class="p">,</span> <span class="s1">&#39;cp&#39;</span><span class="p">,</span> <span class="s1">&#39;cp&#39;</span><span class="p">,</span> <span class="s1">&#39;index.html&#39;</span><span class="p">,</span> <span class="s1">&#39;/dev/null&#39;</span><span class="p">)</span>
  4675. <span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;cp&#39;</span><span class="p">,</span> <span class="s1">&#39;index.html&#39;</span><span class="p">,</span> <span class="s1">&#39;/dev/null&#39;</span><span class="p">]</span>
  4676. <span class="n">os</span><span class="o">.</span><span class="n">spawnvpe</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_WAIT</span><span class="p">,</span> <span class="s1">&#39;cp&#39;</span><span class="p">,</span> <span class="n">L</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">)</span>
  4677. </pre></div>
  4678. </div>
  4679. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.spawn</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">mode</span></code>, <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">args</span></code>, <code class="docutils literal notranslate"><span class="pre">env</span></code>.</p>
  4680. <div class="availability docutils container">
  4681. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p>
  4682. <p><a class="reference internal" href="#os.spawnlp" title="os.spawnlp"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnlp()</span></code></a>, <a class="reference internal" href="#os.spawnlpe" title="os.spawnlpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnlpe()</span></code></a>, <a class="reference internal" href="#os.spawnvp" title="os.spawnvp"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnvp()</span></code></a>
  4683. and <a class="reference internal" href="#os.spawnvpe" title="os.spawnvpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnvpe()</span></code></a> are not available on Windows. <a class="reference internal" href="#os.spawnle" title="os.spawnle"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnle()</span></code></a> and
  4684. <a class="reference internal" href="#os.spawnve" title="os.spawnve"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawnve()</span></code></a> are not thread-safe on Windows; we advise you to use the
  4685. <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module instead.</p>
  4686. </div>
  4687. <div class="versionchanged">
  4688. <p><span class="versionmodified changed">Changed in version 3.6: </span>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>
  4689. </div>
  4690. </dd></dl>
  4691. <dl class="py data">
  4692. <dt class="sig sig-object py" id="os.P_NOWAIT">
  4693. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">P_NOWAIT</span></span><a class="headerlink" href="#os.P_NOWAIT" title="Link to this definition">¶</a></dt>
  4694. <dt class="sig sig-object py" id="os.P_NOWAITO">
  4695. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">P_NOWAITO</span></span><a class="headerlink" href="#os.P_NOWAITO" title="Link to this definition">¶</a></dt>
  4696. <dd><p>Possible values for the <em>mode</em> parameter to the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*</span></code></a> family of
  4697. functions. If either of these values is given, the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*</span></code></a> functions
  4698. will return as soon as the new process has been created, with the process id as
  4699. the return value.</p>
  4700. <div class="availability docutils container">
  4701. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  4702. </div>
  4703. </dd></dl>
  4704. <dl class="py data">
  4705. <dt class="sig sig-object py" id="os.P_WAIT">
  4706. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">P_WAIT</span></span><a class="headerlink" href="#os.P_WAIT" title="Link to this definition">¶</a></dt>
  4707. <dd><p>Possible value for the <em>mode</em> parameter to the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*</span></code></a> family of
  4708. functions. If this is given as <em>mode</em>, the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*</span></code></a> functions will not
  4709. return until the new process has run to completion and will return the exit code
  4710. of the process the run is successful, or <code class="docutils literal notranslate"><span class="pre">-signal</span></code> if a signal kills the
  4711. process.</p>
  4712. <div class="availability docutils container">
  4713. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  4714. </div>
  4715. </dd></dl>
  4716. <dl class="py data">
  4717. <dt class="sig sig-object py" id="os.P_DETACH">
  4718. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">P_DETACH</span></span><a class="headerlink" href="#os.P_DETACH" title="Link to this definition">¶</a></dt>
  4719. <dt class="sig sig-object py" id="os.P_OVERLAY">
  4720. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">P_OVERLAY</span></span><a class="headerlink" href="#os.P_OVERLAY" title="Link to this definition">¶</a></dt>
  4721. <dd><p>Possible values for the <em>mode</em> parameter to the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*</span></code></a> family of
  4722. functions. These are less portable than those listed above. <a class="reference internal" href="#os.P_DETACH" title="os.P_DETACH"><code class="xref py py-const docutils literal notranslate"><span class="pre">P_DETACH</span></code></a>
  4723. is similar to <a class="reference internal" href="#os.P_NOWAIT" title="os.P_NOWAIT"><code class="xref py py-const docutils literal notranslate"><span class="pre">P_NOWAIT</span></code></a>, but the new process is detached from the
  4724. console of the calling process. If <a class="reference internal" href="#os.P_OVERLAY" title="os.P_OVERLAY"><code class="xref py py-const docutils literal notranslate"><span class="pre">P_OVERLAY</span></code></a> is used, the current
  4725. process will be replaced; the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*</span></code></a> function will not return.</p>
  4726. <div class="availability docutils container">
  4727. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  4728. </div>
  4729. </dd></dl>
  4730. <dl class="py function">
  4731. <dt class="sig sig-object py" id="os.startfile">
  4732. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">startfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">operation</span></span></em><span class="optional">]</span><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">arguments</span></span></em><span class="optional">]</span><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">cwd</span></span></em><span class="optional">]</span><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">show_cmd</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.startfile" title="Link to this definition">¶</a></dt>
  4733. <dd><p>Start a file with its associated application.</p>
  4734. <p>When <em>operation</em> is not specified, this acts like double-clicking
  4735. the file in Windows Explorer, or giving the file name as an argument to the
  4736. <strong class="program">start</strong> command from the interactive command shell: the file is opened
  4737. with whatever application (if any) its extension is associated.</p>
  4738. <p>When another <em>operation</em> is given, it must be a “command verb” that specifies
  4739. what should be done with the file. Common verbs documented by Microsoft are <code class="docutils literal notranslate"><span class="pre">'open'</span></code>,
  4740. <code class="docutils literal notranslate"><span class="pre">'print'</span></code> and <code class="docutils literal notranslate"><span class="pre">'edit'</span></code> (to be used on files) as well as <code class="docutils literal notranslate"><span class="pre">'explore'</span></code> and
  4741. <code class="docutils literal notranslate"><span class="pre">'find'</span></code> (to be used on directories).</p>
  4742. <p>When launching an application, specify <em>arguments</em> to be passed as a single
  4743. string. This argument may have no effect when using this function to launch a
  4744. document.</p>
  4745. <p>The default working directory is inherited, but may be overridden by the <em>cwd</em>
  4746. argument. This should be an absolute path. A relative <em>path</em> will be resolved
  4747. against this argument.</p>
  4748. <p>Use <em>show_cmd</em> to override the default window style. Whether this has any
  4749. effect will depend on the application being launched. Values are integers as
  4750. supported by the Win32 <code class="xref c c-func docutils literal notranslate"><span class="pre">ShellExecute()</span></code> function.</p>
  4751. <p><a class="reference internal" href="#os.startfile" title="os.startfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">startfile()</span></code></a> returns as soon as the associated application is launched.
  4752. There is no option to wait for the application to close, and no way to retrieve
  4753. the application’s exit status. The <em>path</em> parameter is relative to the current
  4754. directory or <em>cwd</em>. If you want to use an absolute path, make sure the first
  4755. character is not a slash (<code class="docutils literal notranslate"><span class="pre">'/'</span></code>) Use <a class="reference internal" href="pathlib.html#module-pathlib" title="pathlib: Object-oriented filesystem paths"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code></a> or the
  4756. <a class="reference internal" href="os.path.html#os.path.normpath" title="os.path.normpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.normpath()</span></code></a> function to ensure that paths are properly encoded for
  4757. Win32.</p>
  4758. <p>To reduce interpreter startup overhead, the Win32 <code class="xref c c-func docutils literal notranslate"><span class="pre">ShellExecute()</span></code>
  4759. function is not resolved until this function is first called. If the function
  4760. cannot be resolved, <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> will be raised.</p>
  4761. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.startfile</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">operation</span></code>.</p>
  4762. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.startfile/2</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">operation</span></code>, <code class="docutils literal notranslate"><span class="pre">arguments</span></code>, <code class="docutils literal notranslate"><span class="pre">cwd</span></code>, <code class="docutils literal notranslate"><span class="pre">show_cmd</span></code>.</p>
  4763. <div class="availability docutils container">
  4764. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  4765. </div>
  4766. <div class="versionchanged">
  4767. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>arguments</em>, <em>cwd</em> and <em>show_cmd</em> arguments, and the
  4768. <code class="docutils literal notranslate"><span class="pre">os.startfile/2</span></code> audit event.</p>
  4769. </div>
  4770. </dd></dl>
  4771. <dl class="py function">
  4772. <dt class="sig sig-object py" id="os.system">
  4773. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">system</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">command</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.system" title="Link to this definition">¶</a></dt>
  4774. <dd><p>Execute the command (a string) in a subshell. This is implemented by calling
  4775. the Standard C function <code class="xref c c-func docutils literal notranslate"><span class="pre">system()</span></code>, and has the same limitations.
  4776. Changes to <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>, etc. are not reflected in the environment of
  4777. the executed command. If <em>command</em> generates any output, it will be sent to
  4778. the interpreter standard output stream. The C standard does not
  4779. specify the meaning of the return value of the C function, so the return
  4780. value of the Python function is system-dependent.</p>
  4781. <p>On Unix, the return value is the exit status of the process encoded in the
  4782. format specified for <a class="reference internal" href="#os.wait" title="os.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a>.</p>
  4783. <p>On Windows, the return value is that returned by the system shell after
  4784. running <em>command</em>. The shell is given by the Windows environment variable
  4785. <span class="target" id="index-43"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">COMSPEC</span></code>: it is usually <strong class="program">cmd.exe</strong>, which returns the exit
  4786. status of the command run; on systems using a non-native shell, consult your
  4787. shell documentation.</p>
  4788. <p>The <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module provides more powerful facilities for spawning
  4789. new processes and retrieving their results; using that module is preferable
  4790. to using this function. See the <a class="reference internal" href="subprocess.html#subprocess-replacements"><span class="std std-ref">Replacing Older Functions with the subprocess Module</span></a> section in
  4791. the <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> documentation for some helpful recipes.</p>
  4792. <p>On Unix, <a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitstatus_to_exitcode()</span></code></a> can be used to convert the result
  4793. (exit status) into an exit code. On Windows, the result is directly the exit
  4794. code.</p>
  4795. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">os.system</span></code> with argument <code class="docutils literal notranslate"><span class="pre">command</span></code>.</p>
  4796. <div class="availability docutils container">
  4797. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p>
  4798. </div>
  4799. </dd></dl>
  4800. <dl class="py function">
  4801. <dt class="sig sig-object py" id="os.times">
  4802. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">times</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.times" title="Link to this definition">¶</a></dt>
  4803. <dd><p>Returns the current global process times.
  4804. The return value is an object with five attributes:</p>
  4805. <ul class="simple">
  4806. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">user</span></code> - user time</p></li>
  4807. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">system</span></code> - system time</p></li>
  4808. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">children_user</span></code> - user time of all child processes</p></li>
  4809. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">children_system</span></code> - system time of all child processes</p></li>
  4810. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">elapsed</span></code> - elapsed real time since a fixed point in the past</p></li>
  4811. </ul>
  4812. <p>For backwards compatibility, this object also behaves like a five-tuple
  4813. containing <code class="xref py py-attr docutils literal notranslate"><span class="pre">user</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">system</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">children_user</span></code>,
  4814. <code class="xref py py-attr docutils literal notranslate"><span class="pre">children_system</span></code>, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">elapsed</span></code> in that order.</p>
  4815. <p>See the Unix manual page
  4816. <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/times(2)">times(2)</a></em> and <a class="reference external" href="https://man.freebsd.org/cgi/man.cgi?time(3)">times(3)</a> manual page on Unix or <a class="reference external" href="https://docs.microsoft.com/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesstimes">the GetProcessTimes MSDN</a>
  4817. on Windows. On Windows, only <code class="xref py py-attr docutils literal notranslate"><span class="pre">user</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">system</span></code> are known; the other attributes are zero.</p>
  4818. <div class="availability docutils container">
  4819. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  4820. </div>
  4821. <div class="versionchanged">
  4822. <p><span class="versionmodified changed">Changed in version 3.3: </span>Return type changed from a tuple to a tuple-like object
  4823. with named attributes.</p>
  4824. </div>
  4825. </dd></dl>
  4826. <dl class="py function">
  4827. <dt class="sig sig-object py" id="os.wait">
  4828. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">wait</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.wait" title="Link to this definition">¶</a></dt>
  4829. <dd><p>Wait for completion of a child process, and return a tuple containing its pid
  4830. and exit status indication: a 16-bit number, whose low byte is the signal number
  4831. that killed the process, and whose high byte is the exit status (if the signal
  4832. number is zero); the high bit of the low byte is set if a core file was
  4833. produced.</p>
  4834. <p>If there are no children that could be waited for, <a class="reference internal" href="exceptions.html#ChildProcessError" title="ChildProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ChildProcessError</span></code></a>
  4835. is raised.</p>
  4836. <p><a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitstatus_to_exitcode()</span></code></a> can be used to convert the exit status into an
  4837. exit code.</p>
  4838. <div class="availability docutils container">
  4839. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4840. </div>
  4841. <div class="admonition seealso">
  4842. <p class="admonition-title">See also</p>
  4843. <p>The other <code class="xref py py-func docutils literal notranslate"><span class="pre">wait*()</span></code> functions documented below can be used to wait for the
  4844. completion of a specific child process and have more options.
  4845. <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a> is the only one also available on Windows.</p>
  4846. </div>
  4847. </dd></dl>
  4848. <dl class="py function">
  4849. <dt class="sig sig-object py" id="os.waitid">
  4850. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">waitid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">idtype</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">id</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">options</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="#os.waitid" title="Link to this definition">¶</a></dt>
  4851. <dd><p>Wait for the completion of a child process.</p>
  4852. <p><em>idtype</em> can be <a class="reference internal" href="#os.P_PID" title="os.P_PID"><code class="xref py py-data docutils literal notranslate"><span class="pre">P_PID</span></code></a>, <a class="reference internal" href="#os.P_PGID" title="os.P_PGID"><code class="xref py py-data docutils literal notranslate"><span class="pre">P_PGID</span></code></a>, <a class="reference internal" href="#os.P_ALL" title="os.P_ALL"><code class="xref py py-data docutils literal notranslate"><span class="pre">P_ALL</span></code></a>, or (on Linux) <a class="reference internal" href="#os.P_PIDFD" title="os.P_PIDFD"><code class="xref py py-data docutils literal notranslate"><span class="pre">P_PIDFD</span></code></a>.
  4853. The interpretation of <em>id</em> depends on it; see their individual descriptions.</p>
  4854. <p><em>options</em> is an OR combination of flags. At least one of <a class="reference internal" href="#os.WEXITED" title="os.WEXITED"><code class="xref py py-data docutils literal notranslate"><span class="pre">WEXITED</span></code></a>,
  4855. <a class="reference internal" href="#os.WSTOPPED" title="os.WSTOPPED"><code class="xref py py-data docutils literal notranslate"><span class="pre">WSTOPPED</span></code></a> or <a class="reference internal" href="#os.WCONTINUED" title="os.WCONTINUED"><code class="xref py py-data docutils literal notranslate"><span class="pre">WCONTINUED</span></code></a> is required;
  4856. <a class="reference internal" href="#os.WNOHANG" title="os.WNOHANG"><code class="xref py py-data docutils literal notranslate"><span class="pre">WNOHANG</span></code></a> and <a class="reference internal" href="#os.WNOWAIT" title="os.WNOWAIT"><code class="xref py py-data docutils literal notranslate"><span class="pre">WNOWAIT</span></code></a> are additional optional flags.</p>
  4857. <p>The return value is an object representing the data contained in the
  4858. <code class="xref c c-type docutils literal notranslate"><span class="pre">siginfo_t</span></code> structure with the following attributes:</p>
  4859. <ul class="simple">
  4860. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">si_pid</span></code> (process ID)</p></li>
  4861. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">si_uid</span></code> (real user ID of the child)</p></li>
  4862. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">si_signo</span></code> (always <a class="reference internal" href="signal.html#signal.SIGCHLD" title="signal.SIGCHLD"><code class="xref py py-const docutils literal notranslate"><span class="pre">SIGCHLD</span></code></a>)</p></li>
  4863. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">si_status</span></code> (the exit status or signal number, depending on <code class="xref py py-attr docutils literal notranslate"><span class="pre">si_code</span></code>)</p></li>
  4864. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">si_code</span></code> (see <a class="reference internal" href="#os.CLD_EXITED" title="os.CLD_EXITED"><code class="xref py py-data docutils literal notranslate"><span class="pre">CLD_EXITED</span></code></a> for possible values)</p></li>
  4865. </ul>
  4866. <p>If <a class="reference internal" href="#os.WNOHANG" title="os.WNOHANG"><code class="xref py py-data docutils literal notranslate"><span class="pre">WNOHANG</span></code></a> is specified and there are no matching children in the
  4867. requested state, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.
  4868. Otherwise, if there are no matching children
  4869. that could be waited for, <a class="reference internal" href="exceptions.html#ChildProcessError" title="ChildProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ChildProcessError</span></code></a> is raised.</p>
  4870. <div class="availability docutils container">
  4871. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4872. </div>
  4873. <div class="admonition note">
  4874. <p class="admonition-title">Note</p>
  4875. <p>This function is not available on macOS.</p>
  4876. </div>
  4877. <div class="versionadded">
  4878. <p><span class="versionmodified added">New in version 3.3.</span></p>
  4879. </div>
  4880. </dd></dl>
  4881. <dl class="py function">
  4882. <dt class="sig sig-object py" id="os.waitpid">
  4883. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">waitpid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">options</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="#os.waitpid" title="Link to this definition">¶</a></dt>
  4884. <dd><p>The details of this function differ on Unix and Windows.</p>
  4885. <p>On Unix: Wait for completion of a child process given by process id <em>pid</em>, and
  4886. return a tuple containing its process id and exit status indication (encoded as
  4887. for <a class="reference internal" href="#os.wait" title="os.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a>). The semantics of the call are affected by the value of the
  4888. integer <em>options</em>, which should be <code class="docutils literal notranslate"><span class="pre">0</span></code> for normal operation.</p>
  4889. <p>If <em>pid</em> is greater than <code class="docutils literal notranslate"><span class="pre">0</span></code>, <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a> requests status information for
  4890. that specific process. If <em>pid</em> is <code class="docutils literal notranslate"><span class="pre">0</span></code>, the request is for the status of any
  4891. child in the process group of the current process. If <em>pid</em> is <code class="docutils literal notranslate"><span class="pre">-1</span></code>, the
  4892. request pertains to any child of the current process. If <em>pid</em> is less than
  4893. <code class="docutils literal notranslate"><span class="pre">-1</span></code>, status is requested for any process in the process group <code class="docutils literal notranslate"><span class="pre">-pid</span></code> (the
  4894. absolute value of <em>pid</em>).</p>
  4895. <p><em>options</em> is an OR combination of flags. If it contains <a class="reference internal" href="#os.WNOHANG" title="os.WNOHANG"><code class="xref py py-data docutils literal notranslate"><span class="pre">WNOHANG</span></code></a> and
  4896. there are no matching children in the requested state, <code class="docutils literal notranslate"><span class="pre">(0,</span> <span class="pre">0)</span></code> is
  4897. returned. Otherwise, if there are no matching children that could be waited
  4898. for, <a class="reference internal" href="exceptions.html#ChildProcessError" title="ChildProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ChildProcessError</span></code></a> is raised. Other options that can be used are
  4899. <a class="reference internal" href="#os.WUNTRACED" title="os.WUNTRACED"><code class="xref py py-data docutils literal notranslate"><span class="pre">WUNTRACED</span></code></a> and <a class="reference internal" href="#os.WCONTINUED" title="os.WCONTINUED"><code class="xref py py-data docutils literal notranslate"><span class="pre">WCONTINUED</span></code></a>.</p>
  4900. <p>On Windows: Wait for completion of a process given by process handle <em>pid</em>, and
  4901. return a tuple containing <em>pid</em>, and its exit status shifted left by 8 bits
  4902. (shifting makes cross-platform use of the function easier). A <em>pid</em> less than or
  4903. equal to <code class="docutils literal notranslate"><span class="pre">0</span></code> has no special meaning on Windows, and raises an exception. The
  4904. value of integer <em>options</em> has no effect. <em>pid</em> can refer to any process whose
  4905. id is known, not necessarily a child process. The <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*</span></code></a>
  4906. functions called with <a class="reference internal" href="#os.P_NOWAIT" title="os.P_NOWAIT"><code class="xref py py-const docutils literal notranslate"><span class="pre">P_NOWAIT</span></code></a> return suitable process handles.</p>
  4907. <p><a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitstatus_to_exitcode()</span></code></a> can be used to convert the exit status into an
  4908. exit code.</p>
  4909. <div class="availability docutils container">
  4910. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p>
  4911. </div>
  4912. <div class="versionchanged">
  4913. <p><span class="versionmodified changed">Changed in version 3.5: </span>If the system call is interrupted and the signal handler does not raise an
  4914. exception, the function now retries the system call instead of raising an
  4915. <a class="reference internal" href="exceptions.html#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a> exception (see <span class="target" id="index-44"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p>
  4916. </div>
  4917. </dd></dl>
  4918. <dl class="py function">
  4919. <dt class="sig sig-object py" id="os.wait3">
  4920. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">wait3</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">options</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.wait3" title="Link to this definition">¶</a></dt>
  4921. <dd><p>Similar to <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a>, except no process id argument is given and a
  4922. 3-element tuple containing the child’s process id, exit status indication,
  4923. and resource usage information is returned. Refer to
  4924. <a class="reference internal" href="resource.html#resource.getrusage" title="resource.getrusage"><code class="xref py py-func docutils literal notranslate"><span class="pre">resource.getrusage()</span></code></a> for details on resource usage information. The
  4925. <em>options</em> argument is the same as that provided to <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a> and
  4926. <a class="reference internal" href="#os.wait4" title="os.wait4"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait4()</span></code></a>.</p>
  4927. <p><a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitstatus_to_exitcode()</span></code></a> can be used to convert the exit status into an
  4928. exitcode.</p>
  4929. <div class="availability docutils container">
  4930. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4931. </div>
  4932. </dd></dl>
  4933. <dl class="py function">
  4934. <dt class="sig sig-object py" id="os.wait4">
  4935. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">wait4</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">options</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.wait4" title="Link to this definition">¶</a></dt>
  4936. <dd><p>Similar to <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a>, except a 3-element tuple, containing the child’s
  4937. process id, exit status indication, and resource usage information is
  4938. returned. Refer to <a class="reference internal" href="resource.html#resource.getrusage" title="resource.getrusage"><code class="xref py py-func docutils literal notranslate"><span class="pre">resource.getrusage()</span></code></a> for details on resource usage
  4939. information. The arguments to <a class="reference internal" href="#os.wait4" title="os.wait4"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait4()</span></code></a> are the same as those provided
  4940. to <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a>.</p>
  4941. <p><a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitstatus_to_exitcode()</span></code></a> can be used to convert the exit status into an
  4942. exitcode.</p>
  4943. <div class="availability docutils container">
  4944. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4945. </div>
  4946. </dd></dl>
  4947. <dl class="py data">
  4948. <dt class="sig sig-object py" id="os.P_PID">
  4949. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">P_PID</span></span><a class="headerlink" href="#os.P_PID" title="Link to this definition">¶</a></dt>
  4950. <dt class="sig sig-object py" id="os.P_PGID">
  4951. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">P_PGID</span></span><a class="headerlink" href="#os.P_PGID" title="Link to this definition">¶</a></dt>
  4952. <dt class="sig sig-object py" id="os.P_ALL">
  4953. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">P_ALL</span></span><a class="headerlink" href="#os.P_ALL" title="Link to this definition">¶</a></dt>
  4954. <dt class="sig sig-object py" id="os.P_PIDFD">
  4955. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">P_PIDFD</span></span><a class="headerlink" href="#os.P_PIDFD" title="Link to this definition">¶</a></dt>
  4956. <dd><p>These are the possible values for <em>idtype</em> in <a class="reference internal" href="#os.waitid" title="os.waitid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitid()</span></code></a>. They affect
  4957. how <em>id</em> is interpreted:</p>
  4958. <ul class="simple">
  4959. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">P_PID</span></code> - wait for the child whose PID is <em>id</em>.</p></li>
  4960. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">P_PGID</span></code> - wait for any child whose progress group ID is <em>id</em>.</p></li>
  4961. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">P_ALL</span></code> - wait for any child; <em>id</em> is ignored.</p></li>
  4962. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">P_PIDFD</span></code> - wait for the child identified by the file descriptor
  4963. <em>id</em> (a process file descriptor created with <a class="reference internal" href="#os.pidfd_open" title="os.pidfd_open"><code class="xref py py-func docutils literal notranslate"><span class="pre">pidfd_open()</span></code></a>).</p></li>
  4964. </ul>
  4965. <div class="availability docutils container">
  4966. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4967. </div>
  4968. <div class="admonition note">
  4969. <p class="admonition-title">Note</p>
  4970. <p><code class="xref py py-data docutils literal notranslate"><span class="pre">P_PIDFD</span></code> is only available on Linux &gt;= 5.4.</p>
  4971. </div>
  4972. <div class="versionadded">
  4973. <p><span class="versionmodified added">New in version 3.3.</span></p>
  4974. </div>
  4975. <div class="versionadded">
  4976. <p><span class="versionmodified added">New in version 3.9: </span>The <code class="xref py py-data docutils literal notranslate"><span class="pre">P_PIDFD</span></code> constant.</p>
  4977. </div>
  4978. </dd></dl>
  4979. <dl class="py data">
  4980. <dt class="sig sig-object py" id="os.WCONTINUED">
  4981. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WCONTINUED</span></span><a class="headerlink" href="#os.WCONTINUED" title="Link to this definition">¶</a></dt>
  4982. <dd><p>This <em>options</em> flag for <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a>, <a class="reference internal" href="#os.wait3" title="os.wait3"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait3()</span></code></a>, <a class="reference internal" href="#os.wait4" title="os.wait4"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait4()</span></code></a>, and
  4983. <a class="reference internal" href="#os.waitid" title="os.waitid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitid()</span></code></a> causes child processes to be reported if they have been
  4984. continued from a job control stop since they were last reported.</p>
  4985. <div class="availability docutils container">
  4986. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4987. </div>
  4988. </dd></dl>
  4989. <dl class="py data">
  4990. <dt class="sig sig-object py" id="os.WEXITED">
  4991. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WEXITED</span></span><a class="headerlink" href="#os.WEXITED" title="Link to this definition">¶</a></dt>
  4992. <dd><p>This <em>options</em> flag for <a class="reference internal" href="#os.waitid" title="os.waitid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitid()</span></code></a> causes child processes that have terminated to
  4993. be reported.</p>
  4994. <p>The other <code class="docutils literal notranslate"><span class="pre">wait*</span></code> functions always report children that have terminated,
  4995. so this option is not available for them.</p>
  4996. <div class="availability docutils container">
  4997. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  4998. </div>
  4999. <div class="versionadded">
  5000. <p><span class="versionmodified added">New in version 3.3.</span></p>
  5001. </div>
  5002. </dd></dl>
  5003. <dl class="py data">
  5004. <dt class="sig sig-object py" id="os.WSTOPPED">
  5005. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WSTOPPED</span></span><a class="headerlink" href="#os.WSTOPPED" title="Link to this definition">¶</a></dt>
  5006. <dd><p>This <em>options</em> flag for <a class="reference internal" href="#os.waitid" title="os.waitid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitid()</span></code></a> causes child processes that have been stopped
  5007. by the delivery of a signal to be reported.</p>
  5008. <p>This option is not available for the other <code class="docutils literal notranslate"><span class="pre">wait*</span></code> functions.</p>
  5009. <div class="availability docutils container">
  5010. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5011. </div>
  5012. <div class="versionadded">
  5013. <p><span class="versionmodified added">New in version 3.3.</span></p>
  5014. </div>
  5015. </dd></dl>
  5016. <dl class="py data">
  5017. <dt class="sig sig-object py" id="os.WUNTRACED">
  5018. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WUNTRACED</span></span><a class="headerlink" href="#os.WUNTRACED" title="Link to this definition">¶</a></dt>
  5019. <dd><p>This <em>options</em> flag for <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a>, <a class="reference internal" href="#os.wait3" title="os.wait3"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait3()</span></code></a>, and <a class="reference internal" href="#os.wait4" title="os.wait4"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait4()</span></code></a> causes
  5020. child processes to also be reported if they have been stopped but their
  5021. current state has not been reported since they were stopped.</p>
  5022. <p>This option is not available for <a class="reference internal" href="#os.waitid" title="os.waitid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitid()</span></code></a>.</p>
  5023. <div class="availability docutils container">
  5024. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5025. </div>
  5026. </dd></dl>
  5027. <dl class="py data">
  5028. <dt class="sig sig-object py" id="os.WNOHANG">
  5029. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WNOHANG</span></span><a class="headerlink" href="#os.WNOHANG" title="Link to this definition">¶</a></dt>
  5030. <dd><p>This <em>options</em> flag causes <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a>, <a class="reference internal" href="#os.wait3" title="os.wait3"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait3()</span></code></a>, <a class="reference internal" href="#os.wait4" title="os.wait4"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait4()</span></code></a>, and
  5031. <a class="reference internal" href="#os.waitid" title="os.waitid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitid()</span></code></a> to return right away if no child process status is available
  5032. immediately.</p>
  5033. <div class="availability docutils container">
  5034. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5035. </div>
  5036. </dd></dl>
  5037. <dl class="py data">
  5038. <dt class="sig sig-object py" id="os.WNOWAIT">
  5039. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WNOWAIT</span></span><a class="headerlink" href="#os.WNOWAIT" title="Link to this definition">¶</a></dt>
  5040. <dd><p>This <em>options</em> flag causes <a class="reference internal" href="#os.waitid" title="os.waitid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitid()</span></code></a> to leave the child in a waitable state, so that
  5041. a later <code class="xref py py-func docutils literal notranslate"><span class="pre">wait*()</span></code> call can be used to retrieve the child status information again.</p>
  5042. <p>This option is not available for the other <code class="docutils literal notranslate"><span class="pre">wait*</span></code> functions.</p>
  5043. <div class="availability docutils container">
  5044. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5045. </div>
  5046. </dd></dl>
  5047. <dl class="py data">
  5048. <dt class="sig sig-object py" id="os.CLD_EXITED">
  5049. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLD_EXITED</span></span><a class="headerlink" href="#os.CLD_EXITED" title="Link to this definition">¶</a></dt>
  5050. <dt class="sig sig-object py" id="os.CLD_KILLED">
  5051. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLD_KILLED</span></span><a class="headerlink" href="#os.CLD_KILLED" title="Link to this definition">¶</a></dt>
  5052. <dt class="sig sig-object py" id="os.CLD_DUMPED">
  5053. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLD_DUMPED</span></span><a class="headerlink" href="#os.CLD_DUMPED" title="Link to this definition">¶</a></dt>
  5054. <dt class="sig sig-object py" id="os.CLD_TRAPPED">
  5055. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLD_TRAPPED</span></span><a class="headerlink" href="#os.CLD_TRAPPED" title="Link to this definition">¶</a></dt>
  5056. <dt class="sig sig-object py" id="os.CLD_STOPPED">
  5057. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLD_STOPPED</span></span><a class="headerlink" href="#os.CLD_STOPPED" title="Link to this definition">¶</a></dt>
  5058. <dt class="sig sig-object py" id="os.CLD_CONTINUED">
  5059. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">CLD_CONTINUED</span></span><a class="headerlink" href="#os.CLD_CONTINUED" title="Link to this definition">¶</a></dt>
  5060. <dd><p>These are the possible values for <code class="xref py py-attr docutils literal notranslate"><span class="pre">si_code</span></code> in the result returned by
  5061. <a class="reference internal" href="#os.waitid" title="os.waitid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitid()</span></code></a>.</p>
  5062. <div class="availability docutils container">
  5063. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5064. </div>
  5065. <div class="versionadded">
  5066. <p><span class="versionmodified added">New in version 3.3.</span></p>
  5067. </div>
  5068. <div class="versionchanged">
  5069. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added <a class="reference internal" href="#os.CLD_KILLED" title="os.CLD_KILLED"><code class="xref py py-data docutils literal notranslate"><span class="pre">CLD_KILLED</span></code></a> and <a class="reference internal" href="#os.CLD_STOPPED" title="os.CLD_STOPPED"><code class="xref py py-data docutils literal notranslate"><span class="pre">CLD_STOPPED</span></code></a> values.</p>
  5070. </div>
  5071. </dd></dl>
  5072. <dl class="py function">
  5073. <dt class="sig sig-object py" id="os.waitstatus_to_exitcode">
  5074. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">waitstatus_to_exitcode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">status</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.waitstatus_to_exitcode" title="Link to this definition">¶</a></dt>
  5075. <dd><p>Convert a wait status to an exit code.</p>
  5076. <p>On Unix:</p>
  5077. <ul class="simple">
  5078. <li><p>If the process exited normally (if <code class="docutils literal notranslate"><span class="pre">WIFEXITED(status)</span></code> is true),
  5079. return the process exit status (return <code class="docutils literal notranslate"><span class="pre">WEXITSTATUS(status)</span></code>):
  5080. result greater than or equal to 0.</p></li>
  5081. <li><p>If the process was terminated by a signal (if <code class="docutils literal notranslate"><span class="pre">WIFSIGNALED(status)</span></code> is
  5082. true), return <code class="docutils literal notranslate"><span class="pre">-signum</span></code> where <em>signum</em> is the number of the signal that
  5083. caused the process to terminate (return <code class="docutils literal notranslate"><span class="pre">-WTERMSIG(status)</span></code>):
  5084. result less than 0.</p></li>
  5085. <li><p>Otherwise, raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p></li>
  5086. </ul>
  5087. <p>On Windows, return <em>status</em> shifted right by 8 bits.</p>
  5088. <p>On Unix, if the process is being traced or if <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a> was called
  5089. with <a class="reference internal" href="#os.WUNTRACED" title="os.WUNTRACED"><code class="xref py py-data docutils literal notranslate"><span class="pre">WUNTRACED</span></code></a> option, the caller must first check if
  5090. <code class="docutils literal notranslate"><span class="pre">WIFSTOPPED(status)</span></code> is true. This function must not be called if
  5091. <code class="docutils literal notranslate"><span class="pre">WIFSTOPPED(status)</span></code> is true.</p>
  5092. <div class="admonition seealso">
  5093. <p class="admonition-title">See also</p>
  5094. <p><a class="reference internal" href="#os.WIFEXITED" title="os.WIFEXITED"><code class="xref py py-func docutils literal notranslate"><span class="pre">WIFEXITED()</span></code></a>, <a class="reference internal" href="#os.WEXITSTATUS" title="os.WEXITSTATUS"><code class="xref py py-func docutils literal notranslate"><span class="pre">WEXITSTATUS()</span></code></a>, <a class="reference internal" href="#os.WIFSIGNALED" title="os.WIFSIGNALED"><code class="xref py py-func docutils literal notranslate"><span class="pre">WIFSIGNALED()</span></code></a>,
  5095. <a class="reference internal" href="#os.WTERMSIG" title="os.WTERMSIG"><code class="xref py py-func docutils literal notranslate"><span class="pre">WTERMSIG()</span></code></a>, <a class="reference internal" href="#os.WIFSTOPPED" title="os.WIFSTOPPED"><code class="xref py py-func docutils literal notranslate"><span class="pre">WIFSTOPPED()</span></code></a>, <a class="reference internal" href="#os.WSTOPSIG" title="os.WSTOPSIG"><code class="xref py py-func docutils literal notranslate"><span class="pre">WSTOPSIG()</span></code></a> functions.</p>
  5096. </div>
  5097. <div class="availability docutils container">
  5098. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p>
  5099. </div>
  5100. <div class="versionadded">
  5101. <p><span class="versionmodified added">New in version 3.9.</span></p>
  5102. </div>
  5103. </dd></dl>
  5104. <p>The following functions take a process status code as returned by
  5105. <a class="reference internal" href="#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">system()</span></code></a>, <a class="reference internal" href="#os.wait" title="os.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a>, or <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a> as a parameter. They may be
  5106. used to determine the disposition of a process.</p>
  5107. <dl class="py function">
  5108. <dt class="sig sig-object py" id="os.WCOREDUMP">
  5109. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WCOREDUMP</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">status</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="#os.WCOREDUMP" title="Link to this definition">¶</a></dt>
  5110. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if a core dump was generated for the process, otherwise
  5111. return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  5112. <p>This function should be employed only if <a class="reference internal" href="#os.WIFSIGNALED" title="os.WIFSIGNALED"><code class="xref py py-func docutils literal notranslate"><span class="pre">WIFSIGNALED()</span></code></a> is true.</p>
  5113. <div class="availability docutils container">
  5114. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5115. </div>
  5116. </dd></dl>
  5117. <dl class="py function">
  5118. <dt class="sig sig-object py" id="os.WIFCONTINUED">
  5119. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WIFCONTINUED</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">status</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.WIFCONTINUED" title="Link to this definition">¶</a></dt>
  5120. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if a stopped child has been resumed by delivery of
  5121. <a class="reference internal" href="signal.html#signal.SIGCONT" title="signal.SIGCONT"><code class="xref py py-const docutils literal notranslate"><span class="pre">SIGCONT</span></code></a> (if the process has been continued from a job
  5122. control stop), otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  5123. <p>See <a class="reference internal" href="#os.WCONTINUED" title="os.WCONTINUED"><code class="xref py py-data docutils literal notranslate"><span class="pre">WCONTINUED</span></code></a> option.</p>
  5124. <div class="availability docutils container">
  5125. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5126. </div>
  5127. </dd></dl>
  5128. <dl class="py function">
  5129. <dt class="sig sig-object py" id="os.WIFSTOPPED">
  5130. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WIFSTOPPED</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">status</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.WIFSTOPPED" title="Link to this definition">¶</a></dt>
  5131. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the process was stopped by delivery of a signal,
  5132. otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  5133. <p><a class="reference internal" href="#os.WIFSTOPPED" title="os.WIFSTOPPED"><code class="xref py py-func docutils literal notranslate"><span class="pre">WIFSTOPPED()</span></code></a> only returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a> call was
  5134. done using <a class="reference internal" href="#os.WUNTRACED" title="os.WUNTRACED"><code class="xref py py-data docutils literal notranslate"><span class="pre">WUNTRACED</span></code></a> option or when the process is being traced (see
  5135. <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/ptrace(2)">ptrace(2)</a></em>).</p>
  5136. <div class="availability docutils container">
  5137. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5138. </div>
  5139. </dd></dl>
  5140. <dl class="py function">
  5141. <dt class="sig sig-object py" id="os.WIFSIGNALED">
  5142. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WIFSIGNALED</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">status</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.WIFSIGNALED" title="Link to this definition">¶</a></dt>
  5143. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the process was terminated by a signal, otherwise return
  5144. <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  5145. <div class="availability docutils container">
  5146. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5147. </div>
  5148. </dd></dl>
  5149. <dl class="py function">
  5150. <dt class="sig sig-object py" id="os.WIFEXITED">
  5151. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WIFEXITED</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">status</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.WIFEXITED" title="Link to this definition">¶</a></dt>
  5152. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the process exited terminated normally, that is,
  5153. by calling <code class="docutils literal notranslate"><span class="pre">exit()</span></code> or <code class="docutils literal notranslate"><span class="pre">_exit()</span></code>, or by returning from <code class="docutils literal notranslate"><span class="pre">main()</span></code>;
  5154. otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  5155. <div class="availability docutils container">
  5156. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5157. </div>
  5158. </dd></dl>
  5159. <dl class="py function">
  5160. <dt class="sig sig-object py" id="os.WEXITSTATUS">
  5161. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WEXITSTATUS</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">status</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.WEXITSTATUS" title="Link to this definition">¶</a></dt>
  5162. <dd><p>Return the process exit status.</p>
  5163. <p>This function should be employed only if <a class="reference internal" href="#os.WIFEXITED" title="os.WIFEXITED"><code class="xref py py-func docutils literal notranslate"><span class="pre">WIFEXITED()</span></code></a> is true.</p>
  5164. <div class="availability docutils container">
  5165. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5166. </div>
  5167. </dd></dl>
  5168. <dl class="py function">
  5169. <dt class="sig sig-object py" id="os.WSTOPSIG">
  5170. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WSTOPSIG</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">status</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.WSTOPSIG" title="Link to this definition">¶</a></dt>
  5171. <dd><p>Return the signal which caused the process to stop.</p>
  5172. <p>This function should be employed only if <a class="reference internal" href="#os.WIFSTOPPED" title="os.WIFSTOPPED"><code class="xref py py-func docutils literal notranslate"><span class="pre">WIFSTOPPED()</span></code></a> is true.</p>
  5173. <div class="availability docutils container">
  5174. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5175. </div>
  5176. </dd></dl>
  5177. <dl class="py function">
  5178. <dt class="sig sig-object py" id="os.WTERMSIG">
  5179. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">WTERMSIG</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">status</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.WTERMSIG" title="Link to this definition">¶</a></dt>
  5180. <dd><p>Return the number of the signal that caused the process to terminate.</p>
  5181. <p>This function should be employed only if <a class="reference internal" href="#os.WIFSIGNALED" title="os.WIFSIGNALED"><code class="xref py py-func docutils literal notranslate"><span class="pre">WIFSIGNALED()</span></code></a> is true.</p>
  5182. <div class="availability docutils container">
  5183. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  5184. </div>
  5185. </dd></dl>
  5186. </section>
  5187. <section id="interface-to-the-scheduler">
  5188. <h2>Interface to the scheduler<a class="headerlink" href="#interface-to-the-scheduler" title="Link to this heading">¶</a></h2>
  5189. <p>These functions control how a process is allocated CPU time by the operating
  5190. system. They are only available on some Unix platforms. For more detailed
  5191. information, consult your Unix manpages.</p>
  5192. <div class="versionadded">
  5193. <p><span class="versionmodified added">New in version 3.3.</span></p>
  5194. </div>
  5195. <p>The following scheduling policies are exposed if they are supported by the
  5196. operating system.</p>
  5197. <dl class="py data">
  5198. <dt class="sig sig-object py" id="os.SCHED_OTHER">
  5199. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SCHED_OTHER</span></span><a class="headerlink" href="#os.SCHED_OTHER" title="Link to this definition">¶</a></dt>
  5200. <dd><p>The default scheduling policy.</p>
  5201. </dd></dl>
  5202. <dl class="py data">
  5203. <dt class="sig sig-object py" id="os.SCHED_BATCH">
  5204. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SCHED_BATCH</span></span><a class="headerlink" href="#os.SCHED_BATCH" title="Link to this definition">¶</a></dt>
  5205. <dd><p>Scheduling policy for CPU-intensive processes that tries to preserve
  5206. interactivity on the rest of the computer.</p>
  5207. </dd></dl>
  5208. <dl class="py data">
  5209. <dt class="sig sig-object py" id="os.SCHED_IDLE">
  5210. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SCHED_IDLE</span></span><a class="headerlink" href="#os.SCHED_IDLE" title="Link to this definition">¶</a></dt>
  5211. <dd><p>Scheduling policy for extremely low priority background tasks.</p>
  5212. </dd></dl>
  5213. <dl class="py data">
  5214. <dt class="sig sig-object py" id="os.SCHED_SPORADIC">
  5215. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SCHED_SPORADIC</span></span><a class="headerlink" href="#os.SCHED_SPORADIC" title="Link to this definition">¶</a></dt>
  5216. <dd><p>Scheduling policy for sporadic server programs.</p>
  5217. </dd></dl>
  5218. <dl class="py data">
  5219. <dt class="sig sig-object py" id="os.SCHED_FIFO">
  5220. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SCHED_FIFO</span></span><a class="headerlink" href="#os.SCHED_FIFO" title="Link to this definition">¶</a></dt>
  5221. <dd><p>A First In First Out scheduling policy.</p>
  5222. </dd></dl>
  5223. <dl class="py data">
  5224. <dt class="sig sig-object py" id="os.SCHED_RR">
  5225. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SCHED_RR</span></span><a class="headerlink" href="#os.SCHED_RR" title="Link to this definition">¶</a></dt>
  5226. <dd><p>A round-robin scheduling policy.</p>
  5227. </dd></dl>
  5228. <dl class="py data">
  5229. <dt class="sig sig-object py" id="os.SCHED_RESET_ON_FORK">
  5230. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">SCHED_RESET_ON_FORK</span></span><a class="headerlink" href="#os.SCHED_RESET_ON_FORK" title="Link to this definition">¶</a></dt>
  5231. <dd><p>This flag can be OR’ed with any other scheduling policy. When a process with
  5232. this flag set forks, its child’s scheduling policy and priority are reset to
  5233. the default.</p>
  5234. </dd></dl>
  5235. <dl class="py class">
  5236. <dt class="sig sig-object py" id="os.sched_param">
  5237. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_param</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sched_priority</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.sched_param" title="Link to this definition">¶</a></dt>
  5238. <dd><p>This class represents tunable scheduling parameters used in
  5239. <a class="reference internal" href="#os.sched_setparam" title="os.sched_setparam"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_setparam()</span></code></a>, <a class="reference internal" href="#os.sched_setscheduler" title="os.sched_setscheduler"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_setscheduler()</span></code></a>, and
  5240. <a class="reference internal" href="#os.sched_getparam" title="os.sched_getparam"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_getparam()</span></code></a>. It is immutable.</p>
  5241. <p>At the moment, there is only one possible parameter:</p>
  5242. <dl class="py attribute">
  5243. <dt class="sig sig-object py" id="os.sched_param.sched_priority">
  5244. <span class="sig-name descname"><span class="pre">sched_priority</span></span><a class="headerlink" href="#os.sched_param.sched_priority" title="Link to this definition">¶</a></dt>
  5245. <dd><p>The scheduling priority for a scheduling policy.</p>
  5246. </dd></dl>
  5247. </dd></dl>
  5248. <dl class="py function">
  5249. <dt class="sig sig-object py" id="os.sched_get_priority_min">
  5250. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_get_priority_min</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">policy</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.sched_get_priority_min" title="Link to this definition">¶</a></dt>
  5251. <dd><p>Get the minimum priority value for <em>policy</em>. <em>policy</em> is one of the
  5252. scheduling policy constants above.</p>
  5253. </dd></dl>
  5254. <dl class="py function">
  5255. <dt class="sig sig-object py" id="os.sched_get_priority_max">
  5256. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_get_priority_max</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">policy</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.sched_get_priority_max" title="Link to this definition">¶</a></dt>
  5257. <dd><p>Get the maximum priority value for <em>policy</em>. <em>policy</em> is one of the
  5258. scheduling policy constants above.</p>
  5259. </dd></dl>
  5260. <dl class="py function">
  5261. <dt class="sig sig-object py" id="os.sched_setscheduler">
  5262. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_setscheduler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">policy</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</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="#os.sched_setscheduler" title="Link to this definition">¶</a></dt>
  5263. <dd><p>Set the scheduling policy for the process with PID <em>pid</em>. A <em>pid</em> of 0 means
  5264. the calling process. <em>policy</em> is one of the scheduling policy constants
  5265. above. <em>param</em> is a <a class="reference internal" href="#os.sched_param" title="os.sched_param"><code class="xref py py-class docutils literal notranslate"><span class="pre">sched_param</span></code></a> instance.</p>
  5266. </dd></dl>
  5267. <dl class="py function">
  5268. <dt class="sig sig-object py" id="os.sched_getscheduler">
  5269. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_getscheduler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</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="#os.sched_getscheduler" title="Link to this definition">¶</a></dt>
  5270. <dd><p>Return the scheduling policy for the process with PID <em>pid</em>. A <em>pid</em> of 0
  5271. means the calling process. The result is one of the scheduling policy
  5272. constants above.</p>
  5273. </dd></dl>
  5274. <dl class="py function">
  5275. <dt class="sig sig-object py" id="os.sched_setparam">
  5276. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_setparam</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</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="#os.sched_setparam" title="Link to this definition">¶</a></dt>
  5277. <dd><p>Set the scheduling parameters for the process with PID <em>pid</em>. A <em>pid</em> of 0 means
  5278. the calling process. <em>param</em> is a <a class="reference internal" href="#os.sched_param" title="os.sched_param"><code class="xref py py-class docutils literal notranslate"><span class="pre">sched_param</span></code></a> instance.</p>
  5279. </dd></dl>
  5280. <dl class="py function">
  5281. <dt class="sig sig-object py" id="os.sched_getparam">
  5282. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_getparam</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</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="#os.sched_getparam" title="Link to this definition">¶</a></dt>
  5283. <dd><p>Return the scheduling parameters as a <a class="reference internal" href="#os.sched_param" title="os.sched_param"><code class="xref py py-class docutils literal notranslate"><span class="pre">sched_param</span></code></a> instance for the
  5284. process with PID <em>pid</em>. A <em>pid</em> of 0 means the calling process.</p>
  5285. </dd></dl>
  5286. <dl class="py function">
  5287. <dt class="sig sig-object py" id="os.sched_rr_get_interval">
  5288. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_rr_get_interval</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</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="#os.sched_rr_get_interval" title="Link to this definition">¶</a></dt>
  5289. <dd><p>Return the round-robin quantum in seconds for the process with PID <em>pid</em>. A
  5290. <em>pid</em> of 0 means the calling process.</p>
  5291. </dd></dl>
  5292. <dl class="py function">
  5293. <dt class="sig sig-object py" id="os.sched_yield">
  5294. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_yield</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.sched_yield" title="Link to this definition">¶</a></dt>
  5295. <dd><p>Voluntarily relinquish the CPU.</p>
  5296. </dd></dl>
  5297. <dl class="py function">
  5298. <dt class="sig sig-object py" id="os.sched_setaffinity">
  5299. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_setaffinity</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mask</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="#os.sched_setaffinity" title="Link to this definition">¶</a></dt>
  5300. <dd><p>Restrict the process with PID <em>pid</em> (or the current process if zero) to a
  5301. set of CPUs. <em>mask</em> is an iterable of integers representing the set of
  5302. CPUs to which the process should be restricted.</p>
  5303. </dd></dl>
  5304. <dl class="py function">
  5305. <dt class="sig sig-object py" id="os.sched_getaffinity">
  5306. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sched_getaffinity</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pid</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="#os.sched_getaffinity" title="Link to this definition">¶</a></dt>
  5307. <dd><p>Return the set of CPUs the process with PID <em>pid</em> is restricted to.</p>
  5308. <p>If <em>pid</em> is zero, return the set of CPUs the calling thread of the current
  5309. process is restricted to.</p>
  5310. </dd></dl>
  5311. </section>
  5312. <section id="miscellaneous-system-information">
  5313. <span id="os-path"></span><h2>Miscellaneous System Information<a class="headerlink" href="#miscellaneous-system-information" title="Link to this heading">¶</a></h2>
  5314. <dl class="py function">
  5315. <dt class="sig sig-object py" id="os.confstr">
  5316. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">confstr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.confstr" title="Link to this definition">¶</a></dt>
  5317. <dd><p>Return string-valued system configuration values. <em>name</em> specifies the
  5318. configuration value to retrieve; it may be a string which is the name of a
  5319. defined system value; these names are specified in a number of standards (POSIX,
  5320. Unix 95, Unix 98, and others). Some platforms define additional names as well.
  5321. The names known to the host operating system are given as the keys of the
  5322. <code class="docutils literal notranslate"><span class="pre">confstr_names</span></code> dictionary. For configuration variables not included in that
  5323. mapping, passing an integer for <em>name</em> is also accepted.</p>
  5324. <p>If the configuration value specified by <em>name</em> isn’t defined, <code class="docutils literal notranslate"><span class="pre">None</span></code> is
  5325. returned.</p>
  5326. <p>If <em>name</em> is a string and is not known, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. If a
  5327. specific value for <em>name</em> is not supported by the host system, even if it is
  5328. included in <code class="docutils literal notranslate"><span class="pre">confstr_names</span></code>, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised with
  5329. <a class="reference internal" href="errno.html#errno.EINVAL" title="errno.EINVAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">errno.EINVAL</span></code></a> for the error number.</p>
  5330. <div class="availability docutils container">
  5331. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  5332. </div>
  5333. </dd></dl>
  5334. <dl class="py data">
  5335. <dt class="sig sig-object py" id="os.confstr_names">
  5336. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">confstr_names</span></span><a class="headerlink" href="#os.confstr_names" title="Link to this definition">¶</a></dt>
  5337. <dd><p>Dictionary mapping names accepted by <a class="reference internal" href="#os.confstr" title="os.confstr"><code class="xref py py-func docutils literal notranslate"><span class="pre">confstr()</span></code></a> to the integer values
  5338. defined for those names by the host operating system. This can be used to
  5339. determine the set of names known to the system.</p>
  5340. <div class="availability docutils container">
  5341. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  5342. </div>
  5343. </dd></dl>
  5344. <dl class="py function">
  5345. <dt class="sig sig-object py" id="os.cpu_count">
  5346. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">cpu_count</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.cpu_count" title="Link to this definition">¶</a></dt>
  5347. <dd><p>Return the number of logical CPUs in the system. Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> if
  5348. undetermined.</p>
  5349. <p>This number is not equivalent to the number of logical CPUs the current
  5350. process can use. <code class="docutils literal notranslate"><span class="pre">len(os.sched_getaffinity(0))</span></code> gets the number of logical
  5351. CPUs the calling thread of the current process is restricted to</p>
  5352. <div class="versionadded">
  5353. <p><span class="versionmodified added">New in version 3.4.</span></p>
  5354. </div>
  5355. </dd></dl>
  5356. <dl class="py function">
  5357. <dt class="sig sig-object py" id="os.getloadavg">
  5358. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getloadavg</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getloadavg" title="Link to this definition">¶</a></dt>
  5359. <dd><p>Return the number of processes in the system run queue averaged over the last
  5360. 1, 5, and 15 minutes or raises <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if the load average was
  5361. unobtainable.</p>
  5362. <div class="availability docutils container">
  5363. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  5364. </div>
  5365. </dd></dl>
  5366. <dl class="py function">
  5367. <dt class="sig sig-object py" id="os.sysconf">
  5368. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sysconf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.sysconf" title="Link to this definition">¶</a></dt>
  5369. <dd><p>Return integer-valued system configuration values. If the configuration value
  5370. specified by <em>name</em> isn’t defined, <code class="docutils literal notranslate"><span class="pre">-1</span></code> is returned. The comments regarding
  5371. the <em>name</em> parameter for <a class="reference internal" href="#os.confstr" title="os.confstr"><code class="xref py py-func docutils literal notranslate"><span class="pre">confstr()</span></code></a> apply here as well; the dictionary that
  5372. provides information on the known names is given by <code class="docutils literal notranslate"><span class="pre">sysconf_names</span></code>.</p>
  5373. <div class="availability docutils container">
  5374. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  5375. </div>
  5376. </dd></dl>
  5377. <dl class="py data">
  5378. <dt class="sig sig-object py" id="os.sysconf_names">
  5379. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sysconf_names</span></span><a class="headerlink" href="#os.sysconf_names" title="Link to this definition">¶</a></dt>
  5380. <dd><p>Dictionary mapping names accepted by <a class="reference internal" href="#os.sysconf" title="os.sysconf"><code class="xref py py-func docutils literal notranslate"><span class="pre">sysconf()</span></code></a> to the integer values
  5381. defined for those names by the host operating system. This can be used to
  5382. determine the set of names known to the system.</p>
  5383. <div class="availability docutils container">
  5384. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  5385. </div>
  5386. <div class="versionchanged">
  5387. <p><span class="versionmodified changed">Changed in version 3.11: </span>Add <code class="docutils literal notranslate"><span class="pre">'SC_MINSIGSTKSZ'</span></code> name.</p>
  5388. </div>
  5389. </dd></dl>
  5390. <p>The following data values are used to support path manipulation operations. These
  5391. are defined for all platforms.</p>
  5392. <p>Higher-level operations on pathnames are defined in the <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a> module.</p>
  5393. <dl class="py data" id="index-45">
  5394. <dt class="sig sig-object py" id="os.curdir">
  5395. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">curdir</span></span><a class="headerlink" href="#os.curdir" title="Link to this definition">¶</a></dt>
  5396. <dd><p>The constant string used by the operating system to refer to the current
  5397. directory. This is <code class="docutils literal notranslate"><span class="pre">'.'</span></code> for Windows and POSIX. Also available via
  5398. <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a>.</p>
  5399. </dd></dl>
  5400. <dl class="py data" id="index-46">
  5401. <dt class="sig sig-object py" id="os.pardir">
  5402. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pardir</span></span><a class="headerlink" href="#os.pardir" title="Link to this definition">¶</a></dt>
  5403. <dd><p>The constant string used by the operating system to refer to the parent
  5404. directory. This is <code class="docutils literal notranslate"><span class="pre">'..'</span></code> for Windows and POSIX. Also available via
  5405. <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a>.</p>
  5406. </dd></dl>
  5407. <dl class="py data" id="index-48">
  5408. <span id="index-47"></span><dt class="sig sig-object py" id="os.sep">
  5409. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">sep</span></span><a class="headerlink" href="#os.sep" title="Link to this definition">¶</a></dt>
  5410. <dd><p>The character used by the operating system to separate pathname components.
  5411. This is <code class="docutils literal notranslate"><span class="pre">'/'</span></code> for POSIX and <code class="docutils literal notranslate"><span class="pre">'\\'</span></code> for Windows. Note that knowing this
  5412. is not sufficient to be able to parse or concatenate pathnames — use
  5413. <a class="reference internal" href="os.path.html#os.path.split" title="os.path.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.split()</span></code></a> and <a class="reference internal" href="os.path.html#os.path.join" title="os.path.join"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.join()</span></code></a> — but it is occasionally
  5414. useful. Also available via <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a>.</p>
  5415. </dd></dl>
  5416. <dl class="py data" id="index-49">
  5417. <dt class="sig sig-object py" id="os.altsep">
  5418. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">altsep</span></span><a class="headerlink" href="#os.altsep" title="Link to this definition">¶</a></dt>
  5419. <dd><p>An alternative character used by the operating system to separate pathname
  5420. components, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if only one separator character exists. This is set to
  5421. <code class="docutils literal notranslate"><span class="pre">'/'</span></code> on Windows systems where <code class="docutils literal notranslate"><span class="pre">sep</span></code> is a backslash. Also available via
  5422. <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a>.</p>
  5423. </dd></dl>
  5424. <dl class="py data" id="index-50">
  5425. <dt class="sig sig-object py" id="os.extsep">
  5426. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">extsep</span></span><a class="headerlink" href="#os.extsep" title="Link to this definition">¶</a></dt>
  5427. <dd><p>The character which separates the base filename from the extension; for example,
  5428. the <code class="docutils literal notranslate"><span class="pre">'.'</span></code> in <code class="file docutils literal notranslate"><span class="pre">os.py</span></code>. Also available via <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a>.</p>
  5429. </dd></dl>
  5430. <dl class="py data" id="index-51">
  5431. <dt class="sig sig-object py" id="os.pathsep">
  5432. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">pathsep</span></span><a class="headerlink" href="#os.pathsep" title="Link to this definition">¶</a></dt>
  5433. <dd><p>The character conventionally used by the operating system to separate search
  5434. path components (as in <span class="target" id="index-52"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>), such as <code class="docutils literal notranslate"><span class="pre">':'</span></code> for POSIX or <code class="docutils literal notranslate"><span class="pre">';'</span></code> for
  5435. Windows. Also available via <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a>.</p>
  5436. </dd></dl>
  5437. <dl class="py data">
  5438. <dt class="sig sig-object py" id="os.defpath">
  5439. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">defpath</span></span><a class="headerlink" href="#os.defpath" title="Link to this definition">¶</a></dt>
  5440. <dd><p>The default search path used by <a class="reference internal" href="#os.execl" title="os.execl"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec*p*</span></code></a> and
  5441. <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*p*</span></code></a> if the environment doesn’t have a <code class="docutils literal notranslate"><span class="pre">'PATH'</span></code>
  5442. key. Also available via <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a>.</p>
  5443. </dd></dl>
  5444. <dl class="py data">
  5445. <dt class="sig sig-object py" id="os.linesep">
  5446. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">linesep</span></span><a class="headerlink" href="#os.linesep" title="Link to this definition">¶</a></dt>
  5447. <dd><p>The string used to separate (or, rather, terminate) lines on the current
  5448. platform. This may be a single character, such as <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> for POSIX, or
  5449. multiple characters, for example, <code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code> for Windows. Do not use
  5450. <em>os.linesep</em> as a line terminator when writing files opened in text mode (the
  5451. default); use a single <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> instead, on all platforms.</p>
  5452. </dd></dl>
  5453. <dl class="py data">
  5454. <dt class="sig sig-object py" id="os.devnull">
  5455. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">devnull</span></span><a class="headerlink" href="#os.devnull" title="Link to this definition">¶</a></dt>
  5456. <dd><p>The file path of the null device. For example: <code class="docutils literal notranslate"><span class="pre">'/dev/null'</span></code> for
  5457. POSIX, <code class="docutils literal notranslate"><span class="pre">'nul'</span></code> for Windows. Also available via <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a>.</p>
  5458. </dd></dl>
  5459. <dl class="py data">
  5460. <dt class="sig sig-object py" id="os.RTLD_LAZY">
  5461. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RTLD_LAZY</span></span><a class="headerlink" href="#os.RTLD_LAZY" title="Link to this definition">¶</a></dt>
  5462. <dt class="sig sig-object py" id="os.RTLD_NOW">
  5463. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RTLD_NOW</span></span><a class="headerlink" href="#os.RTLD_NOW" title="Link to this definition">¶</a></dt>
  5464. <dt class="sig sig-object py" id="os.RTLD_GLOBAL">
  5465. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RTLD_GLOBAL</span></span><a class="headerlink" href="#os.RTLD_GLOBAL" title="Link to this definition">¶</a></dt>
  5466. <dt class="sig sig-object py" id="os.RTLD_LOCAL">
  5467. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RTLD_LOCAL</span></span><a class="headerlink" href="#os.RTLD_LOCAL" title="Link to this definition">¶</a></dt>
  5468. <dt class="sig sig-object py" id="os.RTLD_NODELETE">
  5469. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RTLD_NODELETE</span></span><a class="headerlink" href="#os.RTLD_NODELETE" title="Link to this definition">¶</a></dt>
  5470. <dt class="sig sig-object py" id="os.RTLD_NOLOAD">
  5471. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RTLD_NOLOAD</span></span><a class="headerlink" href="#os.RTLD_NOLOAD" title="Link to this definition">¶</a></dt>
  5472. <dt class="sig sig-object py" id="os.RTLD_DEEPBIND">
  5473. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">RTLD_DEEPBIND</span></span><a class="headerlink" href="#os.RTLD_DEEPBIND" title="Link to this definition">¶</a></dt>
  5474. <dd><p>Flags for use with the <a class="reference internal" href="sys.html#sys.setdlopenflags" title="sys.setdlopenflags"><code class="xref py py-func docutils literal notranslate"><span class="pre">setdlopenflags()</span></code></a> and
  5475. <a class="reference internal" href="sys.html#sys.getdlopenflags" title="sys.getdlopenflags"><code class="xref py py-func docutils literal notranslate"><span class="pre">getdlopenflags()</span></code></a> functions. See the Unix manual page
  5476. <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/dlopen(3)">dlopen(3)</a></em> for what the different flags mean.</p>
  5477. <div class="versionadded">
  5478. <p><span class="versionmodified added">New in version 3.3.</span></p>
  5479. </div>
  5480. </dd></dl>
  5481. </section>
  5482. <section id="random-numbers">
  5483. <h2>Random numbers<a class="headerlink" href="#random-numbers" title="Link to this heading">¶</a></h2>
  5484. <dl class="py function">
  5485. <dt class="sig sig-object py" id="os.getrandom">
  5486. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">getrandom</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.getrandom" title="Link to this definition">¶</a></dt>
  5487. <dd><p>Get up to <em>size</em> random bytes. The function can return less bytes than
  5488. requested.</p>
  5489. <p>These bytes can be used to seed user-space random number generators or for
  5490. cryptographic purposes.</p>
  5491. <p><code class="docutils literal notranslate"><span class="pre">getrandom()</span></code> relies on entropy gathered from device drivers and other
  5492. sources of environmental noise. Unnecessarily reading large quantities of
  5493. data will have a negative impact on other users of the <code class="docutils literal notranslate"><span class="pre">/dev/random</span></code> and
  5494. <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code> devices.</p>
  5495. <p>The flags argument is a bit mask that can contain zero or more of the
  5496. following values ORed together: <a class="reference internal" href="#os.GRND_RANDOM" title="os.GRND_RANDOM"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.GRND_RANDOM</span></code></a> and
  5497. <a class="reference internal" href="#os.GRND_NONBLOCK" title="os.GRND_NONBLOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">GRND_NONBLOCK</span></code></a>.</p>
  5498. <p>See also the <a class="reference external" href="https://man7.org/linux/man-pages/man2/getrandom.2.html">Linux getrandom() manual page</a>.</p>
  5499. <div class="availability docutils container">
  5500. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux &gt;= 3.17.</p>
  5501. </div>
  5502. <div class="versionadded">
  5503. <p><span class="versionmodified added">New in version 3.6.</span></p>
  5504. </div>
  5505. </dd></dl>
  5506. <dl class="py function">
  5507. <dt class="sig sig-object py" id="os.urandom">
  5508. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">urandom</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#os.urandom" title="Link to this definition">¶</a></dt>
  5509. <dd><p>Return a bytestring of <em>size</em> random bytes suitable for cryptographic use.</p>
  5510. <p>This function returns random bytes from an OS-specific randomness source. The
  5511. returned data should be unpredictable enough for cryptographic applications,
  5512. though its exact quality depends on the OS implementation.</p>
  5513. <p>On Linux, if the <code class="docutils literal notranslate"><span class="pre">getrandom()</span></code> syscall is available, it is used in
  5514. blocking mode: block until the system urandom entropy pool is initialized
  5515. (128 bits of entropy are collected by the kernel). See the <span class="target" id="index-53"></span><a class="pep reference external" href="https://peps.python.org/pep-0524/"><strong>PEP 524</strong></a> for
  5516. the rationale. On Linux, the <a class="reference internal" href="#os.getrandom" title="os.getrandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">getrandom()</span></code></a> function can be used to get
  5517. random bytes in non-blocking mode (using the <a class="reference internal" href="#os.GRND_NONBLOCK" title="os.GRND_NONBLOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">GRND_NONBLOCK</span></code></a> flag) or
  5518. to poll until the system urandom entropy pool is initialized.</p>
  5519. <p>On a Unix-like system, random bytes are read from the <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code>
  5520. device. If the <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code> device is not available or not readable, the
  5521. <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> exception is raised.</p>
  5522. <p>On Windows, it will use <code class="docutils literal notranslate"><span class="pre">BCryptGenRandom()</span></code>.</p>
  5523. <div class="admonition seealso">
  5524. <p class="admonition-title">See also</p>
  5525. <p>The <a class="reference internal" href="secrets.html#module-secrets" title="secrets: Generate secure random numbers for managing secrets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code></a> module provides higher level functions. For an
  5526. easy-to-use interface to the random number generator provided by your
  5527. platform, please see <a class="reference internal" href="random.html#random.SystemRandom" title="random.SystemRandom"><code class="xref py py-class docutils literal notranslate"><span class="pre">random.SystemRandom</span></code></a>.</p>
  5528. </div>
  5529. <div class="versionchanged">
  5530. <p><span class="versionmodified changed">Changed in version 3.5: </span>On Linux 3.17 and newer, the <code class="docutils literal notranslate"><span class="pre">getrandom()</span></code> syscall is now used
  5531. when available. On OpenBSD 5.6 and newer, the C <code class="docutils literal notranslate"><span class="pre">getentropy()</span></code>
  5532. function is now used. These functions avoid the usage of an internal file
  5533. descriptor.</p>
  5534. </div>
  5535. <div class="versionchanged">
  5536. <p><span class="versionmodified changed">Changed in version 3.5.2: </span>On Linux, if the <code class="docutils literal notranslate"><span class="pre">getrandom()</span></code> syscall blocks (the urandom entropy pool
  5537. is not initialized yet), fall back on reading <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code>.</p>
  5538. </div>
  5539. <div class="versionchanged">
  5540. <p><span class="versionmodified changed">Changed in version 3.6: </span>On Linux, <code class="docutils literal notranslate"><span class="pre">getrandom()</span></code> is now used in blocking mode to increase the
  5541. security.</p>
  5542. </div>
  5543. <div class="versionchanged">
  5544. <p><span class="versionmodified changed">Changed in version 3.11: </span>On Windows, <code class="docutils literal notranslate"><span class="pre">BCryptGenRandom()</span></code> is used instead of <code class="docutils literal notranslate"><span class="pre">CryptGenRandom()</span></code>
  5545. which is deprecated.</p>
  5546. </div>
  5547. </dd></dl>
  5548. <dl class="py data">
  5549. <dt class="sig sig-object py" id="os.GRND_NONBLOCK">
  5550. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">GRND_NONBLOCK</span></span><a class="headerlink" href="#os.GRND_NONBLOCK" title="Link to this definition">¶</a></dt>
  5551. <dd><p>By default, when reading from <code class="docutils literal notranslate"><span class="pre">/dev/random</span></code>, <a class="reference internal" href="#os.getrandom" title="os.getrandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">getrandom()</span></code></a> blocks if
  5552. no random bytes are available, and when reading from <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code>, it blocks
  5553. if the entropy pool has not yet been initialized.</p>
  5554. <p>If the <a class="reference internal" href="#os.GRND_NONBLOCK" title="os.GRND_NONBLOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">GRND_NONBLOCK</span></code></a> flag is set, then <a class="reference internal" href="#os.getrandom" title="os.getrandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">getrandom()</span></code></a> does not
  5555. block in these cases, but instead immediately raises <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a>.</p>
  5556. <div class="versionadded">
  5557. <p><span class="versionmodified added">New in version 3.6.</span></p>
  5558. </div>
  5559. </dd></dl>
  5560. <dl class="py data">
  5561. <dt class="sig sig-object py" id="os.GRND_RANDOM">
  5562. <span class="sig-prename descclassname"><span class="pre">os.</span></span><span class="sig-name descname"><span class="pre">GRND_RANDOM</span></span><a class="headerlink" href="#os.GRND_RANDOM" title="Link to this definition">¶</a></dt>
  5563. <dd><p>If this bit is set, then random bytes are drawn from the
  5564. <code class="docutils literal notranslate"><span class="pre">/dev/random</span></code> pool instead of the <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code> pool.</p>
  5565. <div class="versionadded">
  5566. <p><span class="versionmodified added">New in version 3.6.</span></p>
  5567. </div>
  5568. </dd></dl>
  5569. </section>
  5570. </section>
  5571. <div class="clearer"></div>
  5572. </div>
  5573. </div>
  5574. </div>
  5575. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  5576. <div class="sphinxsidebarwrapper">
  5577. <div>
  5578. <h3><a href="../contents.html">Table of Contents</a></h3>
  5579. <ul>
  5580. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code> — Miscellaneous operating system interfaces</a><ul>
  5581. <li><a class="reference internal" href="#file-names-command-line-arguments-and-environment-variables">File Names, Command Line Arguments, and Environment Variables</a></li>
  5582. <li><a class="reference internal" href="#python-utf-8-mode">Python UTF-8 Mode</a></li>
  5583. <li><a class="reference internal" href="#process-parameters">Process Parameters</a></li>
  5584. <li><a class="reference internal" href="#file-object-creation">File Object Creation</a></li>
  5585. <li><a class="reference internal" href="#file-descriptor-operations">File Descriptor Operations</a><ul>
  5586. <li><a class="reference internal" href="#querying-the-size-of-a-terminal">Querying the size of a terminal</a></li>
  5587. <li><a class="reference internal" href="#inheritance-of-file-descriptors">Inheritance of File Descriptors</a></li>
  5588. </ul>
  5589. </li>
  5590. <li><a class="reference internal" href="#files-and-directories">Files and Directories</a><ul>
  5591. <li><a class="reference internal" href="#linux-extended-attributes">Linux extended attributes</a></li>
  5592. </ul>
  5593. </li>
  5594. <li><a class="reference internal" href="#process-management">Process Management</a></li>
  5595. <li><a class="reference internal" href="#interface-to-the-scheduler">Interface to the scheduler</a></li>
  5596. <li><a class="reference internal" href="#miscellaneous-system-information">Miscellaneous System Information</a></li>
  5597. <li><a class="reference internal" href="#random-numbers">Random numbers</a></li>
  5598. </ul>
  5599. </li>
  5600. </ul>
  5601. </div>
  5602. <div>
  5603. <h4>Previous topic</h4>
  5604. <p class="topless"><a href="allos.html"
  5605. title="previous chapter">Generic Operating System Services</a></p>
  5606. </div>
  5607. <div>
  5608. <h4>Next topic</h4>
  5609. <p class="topless"><a href="io.html"
  5610. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code> — Core tools for working with streams</a></p>
  5611. </div>
  5612. <div role="note" aria-label="source link">
  5613. <h3>This Page</h3>
  5614. <ul class="this-page-menu">
  5615. <li><a href="../bugs.html">Report a Bug</a></li>
  5616. <li>
  5617. <a href="https://github.com/python/cpython/blob/main/Doc/library/os.rst"
  5618. rel="nofollow">Show Source
  5619. </a>
  5620. </li>
  5621. </ul>
  5622. </div>
  5623. </div>
  5624. <div id="sidebarbutton" title="Collapse sidebar">
  5625. <span>«</span>
  5626. </div>
  5627. </div>
  5628. <div class="clearer"></div>
  5629. </div>
  5630. <div class="related" role="navigation" aria-label="related navigation">
  5631. <h3>Navigation</h3>
  5632. <ul>
  5633. <li class="right" style="margin-right: 10px">
  5634. <a href="../genindex.html" title="General Index"
  5635. >index</a></li>
  5636. <li class="right" >
  5637. <a href="../py-modindex.html" title="Python Module Index"
  5638. >modules</a> |</li>
  5639. <li class="right" >
  5640. <a href="io.html" title="io — Core tools for working with streams"
  5641. >next</a> |</li>
  5642. <li class="right" >
  5643. <a href="allos.html" title="Generic Operating System Services"
  5644. >previous</a> |</li>
  5645. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  5646. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  5647. <li class="switchers">
  5648. <div class="language_switcher_placeholder"></div>
  5649. <div class="version_switcher_placeholder"></div>
  5650. </li>
  5651. <li>
  5652. </li>
  5653. <li id="cpython-language-and-version">
  5654. <a href="../index.html">3.12.3 Documentation</a> &#187;
  5655. </li>
  5656. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  5657. <li class="nav-item nav-item-2"><a href="allos.html" >Generic Operating System Services</a> &#187;</li>
  5658. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code> — Miscellaneous operating system interfaces</a></li>
  5659. <li class="right">
  5660. <div class="inline-search" role="search">
  5661. <form class="inline-search" action="../search.html" method="get">
  5662. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  5663. <input type="submit" value="Go" />
  5664. </form>
  5665. </div>
  5666. |
  5667. </li>
  5668. <li class="right">
  5669. <label class="theme-selector-label">
  5670. Theme
  5671. <select class="theme-selector" oninput="activateTheme(this.value)">
  5672. <option value="auto" selected>Auto</option>
  5673. <option value="light">Light</option>
  5674. <option value="dark">Dark</option>
  5675. </select>
  5676. </label> |</li>
  5677. </ul>
  5678. </div>
  5679. <div class="footer">
  5680. &copy;
  5681. <a href="../copyright.html">
  5682. Copyright
  5683. </a>
  5684. 2001-2024, Python Software Foundation.
  5685. <br />
  5686. This page is licensed under the Python Software Foundation License Version 2.
  5687. <br />
  5688. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  5689. <br />
  5690. See <a href="/license.html">History and License</a> for more information.<br />
  5691. <br />
  5692. The Python Software Foundation is a non-profit corporation.
  5693. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  5694. <br />
  5695. <br />
  5696. Last updated on Apr 09, 2024 (13:47 UTC).
  5697. <a href="/bugs.html">Found a bug</a>?
  5698. <br />
  5699. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  5700. </div>
  5701. </body>
  5702. </html>
上海开阖软件有限公司 沪ICP备12045867号-1