![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /proc/self/root/proc/thread-self/root/usr/share/doc/python2-docs/html/library/ |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>15.1. os — Miscellaneous operating system interfaces — Python 2.7.16 documentation</title> <link rel="stylesheet" href="../_static/classic.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.16 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="15.2. io — Core tools for working with streams" href="io.html" /> <link rel="prev" title="15. Generic Operating System Services" href="allos.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/2/library/os.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="io.html" title="15.2. io — Core tools for working with streams" accesskey="N">next</a> |</li> <li class="right" > <a href="allos.html" title="15. Generic Operating System Services" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.16 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="allos.html" accesskey="U">15. Generic Operating System Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-os"> <span id="os-miscellaneous-operating-system-interfaces"></span><h1>15.1. <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="Permalink to this headline">¶</a></h1> <p>This module provides a portable way of using operating system dependent 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 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 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> 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> 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> module.</p> <p>Notes on the availability of these functions:</p> <ul class="simple"> <li>The design of all built-in operating system dependent modules of Python is such that as long as the same functionality is available, it uses the same interface; for example, the function <code class="docutils literal notranslate"><span class="pre">os.stat(path)</span></code> returns stat information about <em>path</em> in the same format (which happens to have originated with the POSIX interface).</li> <li>Extensions peculiar to a particular operating system are also available 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 portability.</li> <li>An “Availability: Unix” note means that this function is commonly found on Unix systems. It does not make any claims about its existence on a specific operating system.</li> <li>If not separately noted, all functions that claim “Availability: Unix” are supported on Mac OS X, which builds on a Unix core.</li> </ul> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">All functions in this module raise <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.</p> </div> <dl class="exception"> <dt id="os.error"> <em class="property">exception </em><code class="descclassname">os.</code><code class="descname">error</code><a class="headerlink" href="#os.error" title="Permalink to this definition">¶</a></dt> <dd><p>An alias for the built-in <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> exception.</p> </dd></dl> <dl class="data"> <dt id="os.name"> <code class="descclassname">os.</code><code class="descname">name</code><a class="headerlink" href="#os.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the operating system dependent module imported. The following 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>, <code class="docutils literal notranslate"><span class="pre">'os2'</span></code>, <code class="docutils literal notranslate"><span class="pre">'ce'</span></code>, <code class="docutils literal notranslate"><span class="pre">'java'</span></code>, <code class="docutils literal notranslate"><span class="pre">'riscos'</span></code>.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p><a class="reference internal" href="sys.html#sys.platform" title="sys.platform"><code class="xref py py-attr 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 system-dependent version information.</p> <p class="last">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 system’s identity.</p> </div> </dd></dl> <div class="section" id="process-parameters"> <span id="os-procinfo"></span><h2>15.1.1. Process Parameters<a class="headerlink" href="#process-parameters" title="Permalink to this headline">¶</a></h2> <p>These functions and data items provide information and operate on the current process and user.</p> <dl class="data"> <dt id="os.environ"> <code class="descclassname">os.</code><code class="descname">environ</code><a class="headerlink" href="#os.environ" title="Permalink to this definition">¶</a></dt> <dd><p>A <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mapping</span></a> object representing the string environment. For example, <code class="docutils literal notranslate"><span class="pre">environ['HOME']</span></code> is the pathname of your home directory (on some platforms), and is equivalent to <code class="docutils literal notranslate"><span class="pre">getenv("HOME")</span></code> in C.</p> <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, typically during Python startup as part of processing <code class="file docutils literal notranslate"><span class="pre">site.py</span></code>. Changes to the environment made after this time are not reflected in <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>, except for changes made by modifying <code class="docutils literal notranslate"><span class="pre">os.environ</span></code> directly.</p> <p>If the platform supports the <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> function, this mapping may be used to modify the environment as well as query the 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 is modified.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">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 <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>, so it’s better to modify <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">On some platforms, including FreeBSD and Mac OS X, setting <code class="docutils literal notranslate"><span class="pre">environ</span></code> may 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> </div> <p>If <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> is not provided, a modified copy of this mapping may be passed to the appropriate process-creation functions to cause child processes to use a modified environment.</p> <p>If the platform supports the <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> function, you can delete items in this mapping to unset environment variables. <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 <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>, 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 called.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.6: </span>Also unset environment variables when calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">os.environ.clear()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">os.environ.pop()</span></code>.</p> </div> </dd></dl> <dl class="function"> <dt> <code class="descclassname">os.</code><code class="descname">chdir</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span></dt> <dt> <code class="descclassname">os.</code><code class="descname">fchdir</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span></dt> <dt> <code class="descclassname">os.</code><code class="descname">getcwd</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt> <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> </dd></dl> <dl class="function"> <dt id="os.ctermid"> <code class="descclassname">os.</code><code class="descname">ctermid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.ctermid" title="Permalink to this definition">¶</a></dt> <dd><p>Return the filename corresponding to the controlling terminal of the process.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.getegid"> <code class="descclassname">os.</code><code class="descname">getegid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getegid" title="Permalink to this definition">¶</a></dt> <dd><p>Return the effective group id of the current process. This corresponds to the “set id” bit on the file being executed in the current process.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.geteuid"> <code class="descclassname">os.</code><code class="descname">geteuid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.geteuid" title="Permalink to this definition">¶</a></dt> <dd><p id="index-0">Return the current process’s effective user id.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.getgid"> <code class="descclassname">os.</code><code class="descname">getgid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getgid" title="Permalink to this definition">¶</a></dt> <dd><p id="index-1">Return the real group id of the current process.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.getgroups"> <code class="descclassname">os.</code><code class="descname">getgroups</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getgroups" title="Permalink to this definition">¶</a></dt> <dd><p>Return list of supplemental group ids associated with the current process.</p> <p>Availability: Unix.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">On Mac OS X, <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 other Unix platforms. If the Python interpreter was built with a deployment target of <code class="xref py py-const 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 the list of effective group ids associated with the current user process; this list is limited to a system-defined number of entries, typically 16, 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. If built with a deployment target greater than <code class="xref py py-const docutils literal notranslate"><span class="pre">10.5</span></code>, <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 associated with the effective user id of the process; the group access list may change over the lifetime of the process, it is not affected 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>, and its length is not limited to 16. The deployment target value, <code class="xref py py-const docutils literal notranslate"><span class="pre">MACOSX_DEPLOYMENT_TARGET</span></code>, can be 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> </div> </dd></dl> <dl class="function"> <dt id="os.initgroups"> <code class="descclassname">os.</code><code class="descname">initgroups</code><span class="sig-paren">(</span><em>username</em>, <em>gid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.initgroups" title="Permalink to this definition">¶</a></dt> <dd><p>Call the system initgroups() to initialize the group access list with all of the groups of which the specified username is a member, plus the specified group id.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.getlogin"> <code class="descclassname">os.</code><code class="descname">getlogin</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getlogin" title="Permalink to this definition">¶</a></dt> <dd><p>Return the name of the user logged in on the controlling terminal of the process. For most purposes, it is more useful to use the environment variable <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LOGNAME</span></code> to find out who the user is, or <code class="docutils literal notranslate"><span class="pre">pwd.getpwuid(os.getuid())[0]</span></code> to get the login name of the process’s real user id.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.getpgid"> <code class="descclassname">os.</code><code class="descname">getpgid</code><span class="sig-paren">(</span><em>pid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.getpgid" title="Permalink to this definition">¶</a></dt> <dd><p>Return the process group id of the process with process id <em>pid</em>. If <em>pid</em> is 0, the process group id of the current process is returned.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.getpgrp"> <code class="descclassname">os.</code><code class="descname">getpgrp</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getpgrp" title="Permalink to this definition">¶</a></dt> <dd><p id="index-3">Return the id of the current process group.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.getpid"> <code class="descclassname">os.</code><code class="descname">getpid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getpid" title="Permalink to this definition">¶</a></dt> <dd><p id="index-4">Return the current process id.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.getppid"> <code class="descclassname">os.</code><code class="descname">getppid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getppid" title="Permalink to this definition">¶</a></dt> <dd><p id="index-5">Return the parent’s process id.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.getresuid"> <code class="descclassname">os.</code><code class="descname">getresuid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getresuid" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple (ruid, euid, suid) denoting the current process’s real, effective, and saved user ids.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.getresgid"> <code class="descclassname">os.</code><code class="descname">getresgid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getresgid" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple (rgid, egid, sgid) denoting the current process’s real, effective, and saved group ids.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.getuid"> <code class="descclassname">os.</code><code class="descname">getuid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getuid" title="Permalink to this definition">¶</a></dt> <dd><p id="index-6">Return the current process’s real user id.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.getenv"> <code class="descclassname">os.</code><code class="descname">getenv</code><span class="sig-paren">(</span><em>varname</em><span class="optional">[</span>, <em>value</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getenv" title="Permalink to this definition">¶</a></dt> <dd><p>Return the value of the environment variable <em>varname</em> if it exists, or <em>value</em> if it doesn’t. <em>value</em> defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <p>Availability: most flavors of Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.putenv"> <code class="descclassname">os.</code><code class="descname">putenv</code><span class="sig-paren">(</span><em>varname</em>, <em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#os.putenv" title="Permalink to this definition">¶</a></dt> <dd><p id="index-7">Set the environment variable named <em>varname</em> to the string <em>value</em>. Such 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>, <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> <p>Availability: most flavors of Unix, Windows.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">On some platforms, including FreeBSD and Mac OS X, setting <code class="docutils literal notranslate"><span class="pre">environ</span></code> may cause memory leaks. Refer to the system documentation for putenv.</p> </div> <p>When <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> is supported, assignments to items in <code class="docutils literal notranslate"><span class="pre">os.environ</span></code> are automatically translated into 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> don’t update <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>, so it is actually preferable to assign to items of <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>.</p> </dd></dl> <dl class="function"> <dt id="os.setegid"> <code class="descclassname">os.</code><code class="descname">setegid</code><span class="sig-paren">(</span><em>egid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.setegid" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current process’s effective group id.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.seteuid"> <code class="descclassname">os.</code><code class="descname">seteuid</code><span class="sig-paren">(</span><em>euid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.seteuid" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current process’s effective user id.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.setgid"> <code class="descclassname">os.</code><code class="descname">setgid</code><span class="sig-paren">(</span><em>gid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.setgid" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current process’ group id.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.setgroups"> <code class="descclassname">os.</code><code class="descname">setgroups</code><span class="sig-paren">(</span><em>groups</em><span class="sig-paren">)</span><a class="headerlink" href="#os.setgroups" title="Permalink to this definition">¶</a></dt> <dd><p>Set the list of supplemental group ids associated with the current process to <em>groups</em>. <em>groups</em> must be a sequence, and each element must be an integer identifying a group. This operation is typically available only to the superuser.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.2.</span></p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">On Mac OS X, the length of <em>groups</em> may not exceed the system-defined maximum number of effective group ids, typically 16. 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 return the same group list set by calling setgroups().</p> </div> </dd></dl> <dl class="function"> <dt id="os.setpgrp"> <code class="descclassname">os.</code><code class="descname">setpgrp</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.setpgrp" title="Permalink to this definition">¶</a></dt> <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 which version is implemented (if any). See the Unix manual for the semantics.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.setpgid"> <code class="descclassname">os.</code><code class="descname">setpgid</code><span class="sig-paren">(</span><em>pid</em>, <em>pgrp</em><span class="sig-paren">)</span><a class="headerlink" href="#os.setpgid" title="Permalink to this definition">¶</a></dt> <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 process with id <em>pid</em> to the process group with id <em>pgrp</em>. See the Unix manual for the semantics.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.setregid"> <code class="descclassname">os.</code><code class="descname">setregid</code><span class="sig-paren">(</span><em>rgid</em>, <em>egid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.setregid" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current process’s real and effective group ids.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.setresgid"> <code class="descclassname">os.</code><code class="descname">setresgid</code><span class="sig-paren">(</span><em>rgid</em>, <em>egid</em>, <em>sgid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.setresgid" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current process’s real, effective, and saved group ids.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.setresuid"> <code class="descclassname">os.</code><code class="descname">setresuid</code><span class="sig-paren">(</span><em>ruid</em>, <em>euid</em>, <em>suid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.setresuid" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current process’s real, effective, and saved user ids.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.setreuid"> <code class="descclassname">os.</code><code class="descname">setreuid</code><span class="sig-paren">(</span><em>ruid</em>, <em>euid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.setreuid" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current process’s real and effective user ids.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.getsid"> <code class="descclassname">os.</code><code class="descname">getsid</code><span class="sig-paren">(</span><em>pid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.getsid" title="Permalink to this definition">¶</a></dt> <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> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.4.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.setsid"> <code class="descclassname">os.</code><code class="descname">setsid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.setsid" title="Permalink to this definition">¶</a></dt> <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> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.setuid"> <code class="descclassname">os.</code><code class="descname">setuid</code><span class="sig-paren">(</span><em>uid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.setuid" title="Permalink to this definition">¶</a></dt> <dd><p id="index-8">Set the current process’s user id.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.strerror"> <code class="descclassname">os.</code><code class="descname">strerror</code><span class="sig-paren">(</span><em>code</em><span class="sig-paren">)</span><a class="headerlink" href="#os.strerror" title="Permalink to this definition">¶</a></dt> <dd><p>Return the error message corresponding to the error code in <em>code</em>. 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 error number, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.umask"> <code class="descclassname">os.</code><code class="descname">umask</code><span class="sig-paren">(</span><em>mask</em><span class="sig-paren">)</span><a class="headerlink" href="#os.umask" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current numeric umask and return the previous umask.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.uname"> <code class="descclassname">os.</code><code class="descname">uname</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.uname" title="Permalink to this definition">¶</a></dt> <dd><p id="index-9">Return a 5-tuple containing information identifying the current operating system. The tuple contains 5 strings: <code class="docutils literal notranslate"><span class="pre">(sysname,</span> <span class="pre">nodename,</span> <span class="pre">release,</span> <span class="pre">version,</span> <span class="pre">machine)</span></code>. Some systems truncate the nodename to 8 characters or to the leading component; a better way to get the hostname is <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 <code class="docutils literal notranslate"><span class="pre">socket.gethostbyaddr(socket.gethostname())</span></code>.</p> <p>Availability: recent flavors of Unix.</p> </dd></dl> <dl class="function"> <dt id="os.unsetenv"> <code class="descclassname">os.</code><code class="descname">unsetenv</code><span class="sig-paren">(</span><em>varname</em><span class="sig-paren">)</span><a class="headerlink" href="#os.unsetenv" title="Permalink to this definition">¶</a></dt> <dd><p id="index-10">Unset (delete) the environment variable named <em>varname</em>. Such 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>, <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> <p>When <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> is supported, deletion of items in <code class="docutils literal notranslate"><span class="pre">os.environ</span></code> is automatically translated into a 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> don’t update <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>, so it is actually preferable to delete items of <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>.</p> <p>Availability: most flavors of Unix, Windows.</p> </dd></dl> </div> <div class="section" id="file-object-creation"> <span id="os-newstreams"></span><h2>15.1.2. File Object Creation<a class="headerlink" href="#file-object-creation" title="Permalink to this headline">¶</a></h2> <p>These functions create new file objects. (See also <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>.)</p> <dl class="function"> <dt id="os.fdopen"> <code class="descclassname">os.</code><code class="descname">fdopen</code><span class="sig-paren">(</span><em>fd</em><span class="optional">[</span>, <em>mode</em><span class="optional">[</span>, <em>bufsize</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.fdopen" title="Permalink to this definition">¶</a></dt> <dd><p id="index-11">Return an open file object connected to the file descriptor <em>fd</em>. The <em>mode</em> and <em>bufsize</em> arguments have the same meaning as the corresponding arguments 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. If <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> raises an exception, it leaves <em>fd</em> untouched (unclosed).</p> <p>Availability: Unix, Windows.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.3: </span>When specified, the <em>mode</em> argument must now start with one of the letters <code class="docutils literal notranslate"><span class="pre">'r'</span></code>, <code class="docutils literal notranslate"><span class="pre">'w'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'a'</span></code>, otherwise a <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.5: </span>On Unix, when the <em>mode</em> argument starts with <code class="docutils literal notranslate"><span class="pre">'a'</span></code>, the <em>O_APPEND</em> flag is set on the file descriptor (which the <code class="xref c c-func docutils literal notranslate"><span class="pre">fdopen()</span></code> implementation already does on most platforms).</p> </div> </dd></dl> <dl class="function"> <dt id="os.popen"> <code class="descclassname">os.</code><code class="descname">popen</code><span class="sig-paren">(</span><em>command</em><span class="optional">[</span>, <em>mode</em><span class="optional">[</span>, <em>bufsize</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.popen" title="Permalink to this definition">¶</a></dt> <dd><p>Open a pipe to or from <em>command</em>. The return value is an open file object connected to the pipe, which can be read or written depending on whether <em>mode</em> 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>. The <em>bufsize</em> argument has the same meaning as 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 exit status of the command (encoded in the 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>) is available as the return value of the <a class="reference internal" href="stdtypes.html#file.close" title="file.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method of the file object, except that when the exit status is zero (termination without errors), <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p> <p>Availability: Unix, Windows.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.6: </span>This function is obsolete. Use 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. Check especially 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.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.0: </span>This function worked unreliably under Windows in earlier versions of Python. This was due to the use of the <code class="xref c c-func docutils literal notranslate"><span class="pre">_popen()</span></code> function from the libraries provided with Windows. Newer versions of Python do not use the broken implementation from the Windows libraries.</p> </div> </dd></dl> <dl class="function"> <dt id="os.tmpfile"> <code class="descclassname">os.</code><code class="descname">tmpfile</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.tmpfile" title="Permalink to this definition">¶</a></dt> <dd><p>Return a new file object opened in update mode (<code class="docutils literal notranslate"><span class="pre">w+b</span></code>). The file has no directory entries associated with it and will be automatically deleted once there are no file descriptors for the file.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <p>There are a number of different <code class="xref py py-func docutils literal notranslate"><span class="pre">popen*()</span></code> functions that provide slightly different ways to create subprocesses.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.6: </span>All of the <code class="xref py py-func docutils literal notranslate"><span class="pre">popen*()</span></code> functions are obsolete. Use 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.</p> </div> <p>For each of the <code class="xref py py-func docutils literal notranslate"><span class="pre">popen*()</span></code> variants, if <em>bufsize</em> is specified, it specifies the buffer size for the I/O pipes. <em>mode</em>, if provided, should be the string <code class="docutils literal notranslate"><span class="pre">'b'</span></code> or <code class="docutils literal notranslate"><span class="pre">'t'</span></code>; on Windows this is needed to determine whether the file objects should be opened in binary or text mode. The default value for <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">'t'</span></code>.</p> <p>Also, for each of these variants, on Unix, <em>cmd</em> may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with <a class="reference internal" href="#os.spawnv" title="os.spawnv"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.spawnv()</span></code></a>). If <em>cmd</em> is a string it will be passed to the shell (as 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>).</p> <p>These methods do not make it possible to retrieve the exit status from the child processes. The only way to control the input and output streams and also retrieve the return codes is to use 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; these are only available on Unix.</p> <p>For a discussion of possible deadlock conditions related to the use of these functions, see <a class="reference internal" href="popen2.html#popen2-flow-control"><span class="std std-ref">Flow Control Issues</span></a>.</p> <dl class="function"> <dt id="os.popen2"> <code class="descclassname">os.</code><code class="descname">popen2</code><span class="sig-paren">(</span><em>cmd</em><span class="optional">[</span>, <em>mode</em><span class="optional">[</span>, <em>bufsize</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.popen2" title="Permalink to this definition">¶</a></dt> <dd><p>Execute <em>cmd</em> as a sub-process and return the file objects <code class="docutils literal notranslate"><span class="pre">(child_stdin,</span> <span class="pre">child_stdout)</span></code>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.6: </span>This function is obsolete. Use 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. Check especially 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.</p> </div> <p>Availability: Unix, Windows.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.0.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.popen3"> <code class="descclassname">os.</code><code class="descname">popen3</code><span class="sig-paren">(</span><em>cmd</em><span class="optional">[</span>, <em>mode</em><span class="optional">[</span>, <em>bufsize</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.popen3" title="Permalink to this definition">¶</a></dt> <dd><p>Execute <em>cmd</em> as a sub-process and return the file objects <code class="docutils literal notranslate"><span class="pre">(child_stdin,</span> <span class="pre">child_stdout,</span> <span class="pre">child_stderr)</span></code>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.6: </span>This function is obsolete. Use 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. Check especially 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.</p> </div> <p>Availability: Unix, Windows.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.0.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.popen4"> <code class="descclassname">os.</code><code class="descname">popen4</code><span class="sig-paren">(</span><em>cmd</em><span class="optional">[</span>, <em>mode</em><span class="optional">[</span>, <em>bufsize</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.popen4" title="Permalink to this definition">¶</a></dt> <dd><p>Execute <em>cmd</em> as a sub-process and return the file objects <code class="docutils literal notranslate"><span class="pre">(child_stdin,</span> <span class="pre">child_stdout_and_stderr)</span></code>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.6: </span>This function is obsolete. Use 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. Check especially 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.</p> </div> <p>Availability: Unix, Windows.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.0.</span></p> </div> </dd></dl> <p>(Note that <code class="docutils literal notranslate"><span class="pre">child_stdin,</span> <span class="pre">child_stdout,</span> <span class="pre">and</span> <span class="pre">child_stderr</span></code> are named from the point of view of the child process, so <em>child_stdin</em> is the child’s standard input.)</p> <p>This functionality is also available in the <a class="reference internal" href="popen2.html#module-popen2" title="popen2: Subprocesses with accessible standard I/O streams. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code></a> module using functions of the same names, but the return values of those functions have a different order.</p> </div> <div class="section" id="file-descriptor-operations"> <span id="os-fd-ops"></span><h2>15.1.3. File Descriptor Operations<a class="headerlink" href="#file-descriptor-operations" title="Permalink to this headline">¶</a></h2> <p>These functions operate on I/O streams referenced using file descriptors.</p> <p>File descriptors are small integers corresponding to a file that has been opened by the current process. For example, standard input is usually file descriptor 0, standard output is 1, and standard error is 2. Further files opened by a process will then be assigned 3, 4, 5, and so forth. The name “file descriptor” is slightly deceptive; on Unix platforms, sockets and pipes are also referenced by file descriptors.</p> <p>The <a class="reference internal" href="stdtypes.html#file.fileno" title="file.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 associated with a file object when required. Note that using the file descriptor directly will bypass the file object methods, ignoring aspects such as internal buffering of data.</p> <dl class="function"> <dt id="os.close"> <code class="descclassname">os.</code><code class="descname">close</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.close" title="Permalink to this definition">¶</a></dt> <dd><p>Close file descriptor <em>fd</em>.</p> <p>Availability: Unix, Windows.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This function is intended for low-level I/O and must be applied to a 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> 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 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 <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> </div> </dd></dl> <dl class="function"> <dt id="os.closerange"> <code class="descclassname">os.</code><code class="descname">closerange</code><span class="sig-paren">(</span><em>fd_low</em>, <em>fd_high</em><span class="sig-paren">)</span><a class="headerlink" href="#os.closerange" title="Permalink to this definition">¶</a></dt> <dd><p>Close all file descriptors from <em>fd_low</em> (inclusive) to <em>fd_high</em> (exclusive), ignoring errors. Equivalent to:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">fd</span> <span class="ow">in</span> <span class="n">xrange</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> <span class="k">try</span><span class="p">:</span> <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> <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span> <span class="k">pass</span> </pre></div> </div> <p>Availability: Unix, Windows.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.6.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.dup"> <code class="descclassname">os.</code><code class="descname">dup</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.dup" title="Permalink to this definition">¶</a></dt> <dd><p>Return a duplicate of file descriptor <em>fd</em>.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.dup2"> <code class="descclassname">os.</code><code class="descname">dup2</code><span class="sig-paren">(</span><em>fd</em>, <em>fd2</em><span class="sig-paren">)</span><a class="headerlink" href="#os.dup2" title="Permalink to this definition">¶</a></dt> <dd><p>Duplicate file descriptor <em>fd</em> to <em>fd2</em>, closing the latter first if necessary.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.fchmod"> <code class="descclassname">os.</code><code class="descname">fchmod</code><span class="sig-paren">(</span><em>fd</em>, <em>mode</em><span class="sig-paren">)</span><a class="headerlink" href="#os.fchmod" title="Permalink to this definition">¶</a></dt> <dd><p>Change the mode of the file given by <em>fd</em> to the numeric <em>mode</em>. 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> for possible values of <em>mode</em>.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.6.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.fchown"> <code class="descclassname">os.</code><code class="descname">fchown</code><span class="sig-paren">(</span><em>fd</em>, <em>uid</em>, <em>gid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.fchown" title="Permalink to this definition">¶</a></dt> <dd><p>Change the owner and group id of the file given by <em>fd</em> to the numeric <em>uid</em> and <em>gid</em>. To leave one of the ids unchanged, set it to -1.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.6.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.fdatasync"> <code class="descclassname">os.</code><code class="descname">fdatasync</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.fdatasync" title="Permalink to this definition">¶</a></dt> <dd><p>Force write of file with filedescriptor <em>fd</em> to disk. Does not force update of metadata.</p> <p>Availability: Unix.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This function is not available on MacOS.</p> </div> </dd></dl> <dl class="function"> <dt id="os.fpathconf"> <code class="descclassname">os.</code><code class="descname">fpathconf</code><span class="sig-paren">(</span><em>fd</em>, <em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#os.fpathconf" title="Permalink to this definition">¶</a></dt> <dd><p>Return system configuration information relevant to an open file. <em>name</em> specifies the configuration value to retrieve; it may be a string which is the name of a defined system value; these names are specified in a number of standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define additional names as well. The names known to the host operating system are given in the <code class="docutils literal notranslate"><span class="pre">pathconf_names</span></code> dictionary. For configuration variables not included in that mapping, passing an integer for <em>name</em> is also accepted.</p> <p>If <em>name</em> is a string and is not known, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. If a specific value for <em>name</em> is not supported by the host system, even if it is included in <code class="docutils literal notranslate"><span class="pre">pathconf_names</span></code>, an <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised with <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> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.fstat"> <code class="descclassname">os.</code><code class="descname">fstat</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.fstat" title="Permalink to this definition">¶</a></dt> <dd><p>Return status for file descriptor <em>fd</em>, like <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> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.fstatvfs"> <code class="descclassname">os.</code><code class="descname">fstatvfs</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.fstatvfs" title="Permalink to this definition">¶</a></dt> <dd><p>Return information about the filesystem containing the file associated with file descriptor <em>fd</em>, like <a class="reference internal" href="statvfs.html#module-statvfs" title="statvfs: Constants for interpreting the result of os.statvfs(). (deprecated)"><code class="xref py py-func docutils literal notranslate"><span class="pre">statvfs()</span></code></a>.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.fsync"> <code class="descclassname">os.</code><code class="descname">fsync</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.fsync" title="Permalink to this definition">¶</a></dt> <dd><p>Force write of file with filedescriptor <em>fd</em> to disk. On Unix, this calls the 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> <p>If you’re starting with a Python file object <em>f</em>, first do <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 buffers associated with <em>f</em> are written to disk.</p> <p>Availability: Unix, and Windows starting in 2.2.3.</p> </dd></dl> <dl class="function"> <dt id="os.ftruncate"> <code class="descclassname">os.</code><code class="descname">ftruncate</code><span class="sig-paren">(</span><em>fd</em>, <em>length</em><span class="sig-paren">)</span><a class="headerlink" href="#os.ftruncate" title="Permalink to this definition">¶</a></dt> <dd><p>Truncate the file corresponding to file descriptor <em>fd</em>, so that it is at most <em>length</em> bytes in size.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.isatty"> <code class="descclassname">os.</code><code class="descname">isatty</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.isatty" title="Permalink to this definition">¶</a></dt> <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 tty(-like) device, else <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> </dd></dl> <dl class="function"> <dt id="os.lseek"> <code class="descclassname">os.</code><code class="descname">lseek</code><span class="sig-paren">(</span><em>fd</em>, <em>pos</em>, <em>how</em><span class="sig-paren">)</span><a class="headerlink" href="#os.lseek" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current position of file descriptor <em>fd</em> to position <em>pos</em>, modified by <em>how</em>: <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> to set the position relative to the beginning of the file; <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> to set it relative to the current position; <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> to set it relative to the end of the file. Return the new cursor position in bytes, starting from the beginning.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="data"> <dt id="os.SEEK_SET"> <code class="descclassname">os.</code><code class="descname">SEEK_SET</code><a class="headerlink" href="#os.SEEK_SET" title="Permalink to this definition">¶</a></dt> <dt id="os.SEEK_CUR"> <code class="descclassname">os.</code><code class="descname">SEEK_CUR</code><a class="headerlink" href="#os.SEEK_CUR" title="Permalink to this definition">¶</a></dt> <dt id="os.SEEK_END"> <code class="descclassname">os.</code><code class="descname">SEEK_END</code><a class="headerlink" href="#os.SEEK_END" title="Permalink to this definition">¶</a></dt> <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. Their values are 0, 1, and 2, respectively.</p> <p>Availability: Windows, Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.open"> <code class="descclassname">os.</code><code class="descname">open</code><span class="sig-paren">(</span><em>file</em>, <em>flags</em><span class="optional">[</span>, <em>mode</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.open" title="Permalink to this definition">¶</a></dt> <dd><p>Open the file <em>file</em> and set various flags according to <em>flags</em> and possibly its mode according to <em>mode</em>. The default <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">0777</span></code> (octal), and the current umask value is first masked out. Return the file descriptor for the newly opened file.</p> <p>For a description of the flag and mode values, see the C run-time documentation; 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 this module too (see <a class="reference internal" href="#open-constants"><span class="std std-ref">open() flag constants</span></a>). In particular, on Windows adding <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> <p>Availability: Unix, Windows.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This function is intended for low-level I/O. For normal usage, use 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>, which returns a “file object” with <a class="reference internal" href="stdtypes.html#file.read" title="file.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> and <a class="reference internal" href="stdtypes.html#file.write" title="file.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> methods (and many more). To 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> </div> </dd></dl> <dl class="function"> <dt id="os.openpty"> <code class="descclassname">os.</code><code class="descname">openpty</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.openpty" title="Permalink to this definition">¶</a></dt> <dd><p id="index-12">Open a new pseudo-terminal pair. Return a pair of file descriptors <code class="docutils literal notranslate"><span class="pre">(master,</span> <span class="pre">slave)</span></code> for the pty and the tty, respectively. For a (slightly) more portable approach, use the <a class="reference internal" href="pty.html#module-pty" title="pty: Pseudo-Terminal Handling for Linux. (Linux)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pty</span></code></a> module.</p> <p>Availability: some flavors of Unix.</p> </dd></dl> <dl class="function"> <dt id="os.pipe"> <code class="descclassname">os.</code><code class="descname">pipe</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.pipe" title="Permalink to this definition">¶</a></dt> <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 reading and writing, respectively.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.read"> <code class="descclassname">os.</code><code class="descname">read</code><span class="sig-paren">(</span><em>fd</em>, <em>n</em><span class="sig-paren">)</span><a class="headerlink" href="#os.read" title="Permalink to this definition">¶</a></dt> <dd><p>Read at most <em>n</em> bytes from file descriptor <em>fd</em>. Return a string containing the bytes read. If the end of the file referred to by <em>fd</em> has been reached, an empty string is returned.</p> <p>Availability: Unix, Windows.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This function is intended for low-level I/O and must be applied to a 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> 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 “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 <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 <a class="reference internal" href="stdtypes.html#file.read" title="file.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> or <a class="reference internal" href="stdtypes.html#file.readline" title="file.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a> methods.</p> </div> </dd></dl> <dl class="function"> <dt id="os.tcgetpgrp"> <code class="descclassname">os.</code><code class="descname">tcgetpgrp</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.tcgetpgrp" title="Permalink to this definition">¶</a></dt> <dd><p>Return the process group associated with the terminal given by <em>fd</em> (an open 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> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.tcsetpgrp"> <code class="descclassname">os.</code><code class="descname">tcsetpgrp</code><span class="sig-paren">(</span><em>fd</em>, <em>pg</em><span class="sig-paren">)</span><a class="headerlink" href="#os.tcsetpgrp" title="Permalink to this definition">¶</a></dt> <dd><p>Set the process group associated with the terminal given by <em>fd</em> (an open 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>) to <em>pg</em>.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.ttyname"> <code class="descclassname">os.</code><code class="descname">ttyname</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.ttyname" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string which specifies the terminal device associated with file descriptor <em>fd</em>. If <em>fd</em> is not associated with a terminal device, an exception is raised.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.write"> <code class="descclassname">os.</code><code class="descname">write</code><span class="sig-paren">(</span><em>fd</em>, <em>str</em><span class="sig-paren">)</span><a class="headerlink" href="#os.write" title="Permalink to this definition">¶</a></dt> <dd><p>Write the string <em>str</em> to file descriptor <em>fd</em>. Return the number of bytes actually written.</p> <p>Availability: Unix, Windows.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This function is intended for low-level I/O and must be applied to a 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> 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 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 <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 <a class="reference internal" href="stdtypes.html#file.write" title="file.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> method.</p> </div> </dd></dl> <div class="section" id="open-flag-constants"> <span id="open-constants"></span><h3>15.1.3.1. <code class="docutils literal notranslate"><span class="pre">open()</span></code> flag constants<a class="headerlink" href="#open-flag-constants" title="Permalink to this headline">¶</a></h3> <p>The following constants are options for the <em>flags</em> parameter to the <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 <code class="docutils literal notranslate"><span class="pre">|</span></code>. Some of them are not available on all platforms. For descriptions of their availability and use, consult the <em class="manpage">open(2)</em> manual page on Unix or <a class="reference external" href="http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx">the MSDN</a> on Windows.</p> <dl class="data"> <dt id="os.O_RDONLY"> <code class="descclassname">os.</code><code class="descname">O_RDONLY</code><a class="headerlink" href="#os.O_RDONLY" title="Permalink to this definition">¶</a></dt> <dt id="os.O_WRONLY"> <code class="descclassname">os.</code><code class="descname">O_WRONLY</code><a class="headerlink" href="#os.O_WRONLY" title="Permalink to this definition">¶</a></dt> <dt id="os.O_RDWR"> <code class="descclassname">os.</code><code class="descname">O_RDWR</code><a class="headerlink" href="#os.O_RDWR" title="Permalink to this definition">¶</a></dt> <dt id="os.O_APPEND"> <code class="descclassname">os.</code><code class="descname">O_APPEND</code><a class="headerlink" href="#os.O_APPEND" title="Permalink to this definition">¶</a></dt> <dt id="os.O_CREAT"> <code class="descclassname">os.</code><code class="descname">O_CREAT</code><a class="headerlink" href="#os.O_CREAT" title="Permalink to this definition">¶</a></dt> <dt id="os.O_EXCL"> <code class="descclassname">os.</code><code class="descname">O_EXCL</code><a class="headerlink" href="#os.O_EXCL" title="Permalink to this definition">¶</a></dt> <dt id="os.O_TRUNC"> <code class="descclassname">os.</code><code class="descname">O_TRUNC</code><a class="headerlink" href="#os.O_TRUNC" title="Permalink to this definition">¶</a></dt> <dd><p>The above constants are available on Unix and Windows.</p> </dd></dl> <dl class="data"> <dt id="os.O_DSYNC"> <code class="descclassname">os.</code><code class="descname">O_DSYNC</code><a class="headerlink" href="#os.O_DSYNC" title="Permalink to this definition">¶</a></dt> <dt id="os.O_RSYNC"> <code class="descclassname">os.</code><code class="descname">O_RSYNC</code><a class="headerlink" href="#os.O_RSYNC" title="Permalink to this definition">¶</a></dt> <dt id="os.O_SYNC"> <code class="descclassname">os.</code><code class="descname">O_SYNC</code><a class="headerlink" href="#os.O_SYNC" title="Permalink to this definition">¶</a></dt> <dt id="os.O_NDELAY"> <code class="descclassname">os.</code><code class="descname">O_NDELAY</code><a class="headerlink" href="#os.O_NDELAY" title="Permalink to this definition">¶</a></dt> <dt id="os.O_NONBLOCK"> <code class="descclassname">os.</code><code class="descname">O_NONBLOCK</code><a class="headerlink" href="#os.O_NONBLOCK" title="Permalink to this definition">¶</a></dt> <dt id="os.O_NOCTTY"> <code class="descclassname">os.</code><code class="descname">O_NOCTTY</code><a class="headerlink" href="#os.O_NOCTTY" title="Permalink to this definition">¶</a></dt> <dd><p>The above constants are only available on Unix.</p> </dd></dl> <dl class="data"> <dt id="os.O_BINARY"> <code class="descclassname">os.</code><code class="descname">O_BINARY</code><a class="headerlink" href="#os.O_BINARY" title="Permalink to this definition">¶</a></dt> <dt id="os.O_NOINHERIT"> <code class="descclassname">os.</code><code class="descname">O_NOINHERIT</code><a class="headerlink" href="#os.O_NOINHERIT" title="Permalink to this definition">¶</a></dt> <dt id="os.O_SHORT_LIVED"> <code class="descclassname">os.</code><code class="descname">O_SHORT_LIVED</code><a class="headerlink" href="#os.O_SHORT_LIVED" title="Permalink to this definition">¶</a></dt> <dt id="os.O_TEMPORARY"> <code class="descclassname">os.</code><code class="descname">O_TEMPORARY</code><a class="headerlink" href="#os.O_TEMPORARY" title="Permalink to this definition">¶</a></dt> <dt id="os.O_RANDOM"> <code class="descclassname">os.</code><code class="descname">O_RANDOM</code><a class="headerlink" href="#os.O_RANDOM" title="Permalink to this definition">¶</a></dt> <dt id="os.O_SEQUENTIAL"> <code class="descclassname">os.</code><code class="descname">O_SEQUENTIAL</code><a class="headerlink" href="#os.O_SEQUENTIAL" title="Permalink to this definition">¶</a></dt> <dt id="os.O_TEXT"> <code class="descclassname">os.</code><code class="descname">O_TEXT</code><a class="headerlink" href="#os.O_TEXT" title="Permalink to this definition">¶</a></dt> <dd><p>The above constants are only available on Windows.</p> </dd></dl> <dl class="data"> <dt id="os.O_ASYNC"> <code class="descclassname">os.</code><code class="descname">O_ASYNC</code><a class="headerlink" href="#os.O_ASYNC" title="Permalink to this definition">¶</a></dt> <dt id="os.O_DIRECT"> <code class="descclassname">os.</code><code class="descname">O_DIRECT</code><a class="headerlink" href="#os.O_DIRECT" title="Permalink to this definition">¶</a></dt> <dt id="os.O_DIRECTORY"> <code class="descclassname">os.</code><code class="descname">O_DIRECTORY</code><a class="headerlink" href="#os.O_DIRECTORY" title="Permalink to this definition">¶</a></dt> <dt id="os.O_NOFOLLOW"> <code class="descclassname">os.</code><code class="descname">O_NOFOLLOW</code><a class="headerlink" href="#os.O_NOFOLLOW" title="Permalink to this definition">¶</a></dt> <dt id="os.O_NOATIME"> <code class="descclassname">os.</code><code class="descname">O_NOATIME</code><a class="headerlink" href="#os.O_NOATIME" title="Permalink to this definition">¶</a></dt> <dt id="os.O_SHLOCK"> <code class="descclassname">os.</code><code class="descname">O_SHLOCK</code><a class="headerlink" href="#os.O_SHLOCK" title="Permalink to this definition">¶</a></dt> <dt id="os.O_EXLOCK"> <code class="descclassname">os.</code><code class="descname">O_EXLOCK</code><a class="headerlink" href="#os.O_EXLOCK" title="Permalink to this definition">¶</a></dt> <dd><p>The above constants are extensions and not present if they are not defined by the C library.</p> </dd></dl> </div> </div> <div class="section" id="files-and-directories"> <span id="os-file-dir"></span><h2>15.1.4. Files and Directories<a class="headerlink" href="#files-and-directories" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="os.access"> <code class="descclassname">os.</code><code class="descname">access</code><span class="sig-paren">(</span><em>path</em>, <em>mode</em><span class="sig-paren">)</span><a class="headerlink" href="#os.access" title="Permalink to this definition">¶</a></dt> <dd><p>Use the real uid/gid to test for access to <em>path</em>. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to <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 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 <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, <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">access(2)</em> for more information.</p> <p>Availability: Unix, Windows.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <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 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, because the user might exploit the short time interval between checking 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> techniques. For example:</p> <div class="highlight-default 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">"myfile"</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">R_OK</span><span class="p">):</span> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"myfile"</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span> <span class="k">return</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="k">return</span> <span class="s2">"some default data"</span> </pre></div> </div> <p>is better written as:</p> <div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span> <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"myfile"</span><span class="p">)</span> <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span> <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">errno</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">EACCES</span><span class="p">:</span> <span class="k">return</span> <span class="s2">"some default data"</span> <span class="c1"># Not a permission error.</span> <span class="k">raise</span> <span class="k">else</span><span class="p">:</span> <span class="k">with</span> <span class="n">fp</span><span class="p">:</span> <span class="k">return</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> </pre></div> </div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">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 succeed, particularly for operations on network filesystems which may have permissions semantics beyond the usual POSIX permission-bit model.</p> </div> </dd></dl> <dl class="data"> <dt id="os.F_OK"> <code class="descclassname">os.</code><code class="descname">F_OK</code><a class="headerlink" href="#os.F_OK" title="Permalink to this definition">¶</a></dt> <dd><p>Value 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 existence of <em>path</em>.</p> </dd></dl> <dl class="data"> <dt id="os.R_OK"> <code class="descclassname">os.</code><code class="descname">R_OK</code><a class="headerlink" href="#os.R_OK" title="Permalink to this definition">¶</a></dt> <dd><p>Value to include in 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 readability of <em>path</em>.</p> </dd></dl> <dl class="data"> <dt id="os.W_OK"> <code class="descclassname">os.</code><code class="descname">W_OK</code><a class="headerlink" href="#os.W_OK" title="Permalink to this definition">¶</a></dt> <dd><p>Value to include in 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 writability of <em>path</em>.</p> </dd></dl> <dl class="data"> <dt id="os.X_OK"> <code class="descclassname">os.</code><code class="descname">X_OK</code><a class="headerlink" href="#os.X_OK" title="Permalink to this definition">¶</a></dt> <dd><p>Value to include in 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 determine if <em>path</em> can be executed.</p> </dd></dl> <dl class="function"> <dt id="os.chdir"> <code class="descclassname">os.</code><code class="descname">chdir</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.chdir" title="Permalink to this definition">¶</a></dt> <dd><p id="index-13">Change the current working directory to <em>path</em>.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.fchdir"> <code class="descclassname">os.</code><code class="descname">fchdir</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#os.fchdir" title="Permalink to this definition">¶</a></dt> <dd><p>Change the current working directory to the directory represented by the file descriptor <em>fd</em>. The descriptor must refer to an opened directory, not an open file.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.getcwd"> <code class="descclassname">os.</code><code class="descname">getcwd</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getcwd" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the current working directory.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.getcwdu"> <code class="descclassname">os.</code><code class="descname">getcwdu</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getcwdu" title="Permalink to this definition">¶</a></dt> <dd><p>Return a Unicode object representing the current working directory.</p> <p>Availability: Unix, Windows.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.chflags"> <code class="descclassname">os.</code><code class="descname">chflags</code><span class="sig-paren">(</span><em>path</em>, <em>flags</em><span class="sig-paren">)</span><a class="headerlink" href="#os.chflags" title="Permalink to this definition">¶</a></dt> <dd><p>Set the flags of <em>path</em> to the numeric <em>flags</em>. <em>flags</em> may take a combination (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> <ul class="simple"> <li><a class="reference internal" href="stat.html#stat.UF_NODUMP" title="stat.UF_NODUMP"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.UF_NODUMP</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.UF_IMMUTABLE" title="stat.UF_IMMUTABLE"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.UF_IMMUTABLE</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.UF_APPEND" title="stat.UF_APPEND"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.UF_APPEND</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.UF_OPAQUE" title="stat.UF_OPAQUE"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.UF_OPAQUE</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.UF_NOUNLINK" title="stat.UF_NOUNLINK"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.UF_NOUNLINK</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.UF_COMPRESSED" title="stat.UF_COMPRESSED"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.UF_COMPRESSED</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.UF_HIDDEN" title="stat.UF_HIDDEN"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.UF_HIDDEN</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.SF_ARCHIVED" title="stat.SF_ARCHIVED"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.SF_ARCHIVED</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.SF_IMMUTABLE" title="stat.SF_IMMUTABLE"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.SF_IMMUTABLE</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.SF_APPEND" title="stat.SF_APPEND"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.SF_APPEND</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.SF_NOUNLINK" title="stat.SF_NOUNLINK"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.SF_NOUNLINK</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.SF_SNAPSHOT" title="stat.SF_SNAPSHOT"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.SF_SNAPSHOT</span></code></a></li> </ul> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.6.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.chroot"> <code class="descclassname">os.</code><code class="descname">chroot</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.chroot" title="Permalink to this definition">¶</a></dt> <dd><p>Change the root directory of the current process to <em>path</em>. Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.2.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.chmod"> <code class="descclassname">os.</code><code class="descname">chmod</code><span class="sig-paren">(</span><em>path</em>, <em>mode</em><span class="sig-paren">)</span><a class="headerlink" href="#os.chmod" title="Permalink to this definition">¶</a></dt> <dd><p>Change the mode of <em>path</em> to the numeric <em>mode</em>. <em>mode</em> may take one 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) or bitwise ORed combinations of them:</p> <ul class="simple"> <li><a class="reference internal" href="stat.html#stat.S_ISUID" title="stat.S_ISUID"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_ISUID</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_ISGID" title="stat.S_ISGID"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_ISGID</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_ENFMT" title="stat.S_ENFMT"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_ENFMT</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_ISVTX" title="stat.S_ISVTX"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_ISVTX</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IREAD" title="stat.S_IREAD"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IREAD</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IWRITE" title="stat.S_IWRITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IWRITE</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IEXEC" title="stat.S_IEXEC"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IEXEC</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IRWXU" title="stat.S_IRWXU"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IRWXU</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IRUSR" title="stat.S_IRUSR"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IRUSR</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IWUSR" title="stat.S_IWUSR"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IWUSR</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IXUSR" title="stat.S_IXUSR"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IXUSR</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IRWXG" title="stat.S_IRWXG"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IRWXG</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IRGRP" title="stat.S_IRGRP"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IRGRP</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IWGRP" title="stat.S_IWGRP"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IWGRP</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IXGRP" title="stat.S_IXGRP"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IXGRP</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IRWXO" title="stat.S_IRWXO"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IRWXO</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IROTH" title="stat.S_IROTH"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IROTH</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IWOTH" title="stat.S_IWOTH"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IWOTH</span></code></a></li> <li><a class="reference internal" href="stat.html#stat.S_IXOTH" title="stat.S_IXOTH"><code class="xref py py-data docutils literal notranslate"><span class="pre">stat.S_IXOTH</span></code></a></li> </ul> <p>Availability: Unix, Windows.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">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 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> constants or a corresponding integer value). All other bits are ignored.</p> </div> </dd></dl> <dl class="function"> <dt id="os.chown"> <code class="descclassname">os.</code><code class="descname">chown</code><span class="sig-paren">(</span><em>path</em>, <em>uid</em>, <em>gid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.chown" title="Permalink to this definition">¶</a></dt> <dd><p>Change the owner and group id of <em>path</em> to the numeric <em>uid</em> and <em>gid</em>. To leave one of the ids unchanged, set it to -1.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.lchflags"> <code class="descclassname">os.</code><code class="descname">lchflags</code><span class="sig-paren">(</span><em>path</em>, <em>flags</em><span class="sig-paren">)</span><a class="headerlink" href="#os.lchflags" title="Permalink to this definition">¶</a></dt> <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 not follow symbolic links.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.6.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.lchmod"> <code class="descclassname">os.</code><code class="descname">lchmod</code><span class="sig-paren">(</span><em>path</em>, <em>mode</em><span class="sig-paren">)</span><a class="headerlink" href="#os.lchmod" title="Permalink to this definition">¶</a></dt> <dd><p>Change the mode of <em>path</em> to the numeric <em>mode</em>. If path is a symlink, this 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> for possible values of <em>mode</em>.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.6.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.lchown"> <code class="descclassname">os.</code><code class="descname">lchown</code><span class="sig-paren">(</span><em>path</em>, <em>uid</em>, <em>gid</em><span class="sig-paren">)</span><a class="headerlink" href="#os.lchown" title="Permalink to this definition">¶</a></dt> <dd><p>Change the owner and group id of <em>path</em> to the numeric <em>uid</em> and <em>gid</em>. This function will not follow symbolic links.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.link"> <code class="descclassname">os.</code><code class="descname">link</code><span class="sig-paren">(</span><em>source</em>, <em>link_name</em><span class="sig-paren">)</span><a class="headerlink" href="#os.link" title="Permalink to this definition">¶</a></dt> <dd><p>Create a hard link pointing to <em>source</em> named <em>link_name</em>.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.listdir"> <code class="descclassname">os.</code><code class="descname">listdir</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.listdir" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list containing the names of the entries in the directory given by <em>path</em>. The list is in arbitrary order. It does not include the special 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.</p> <p>Availability: Unix, Windows.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.3: </span>On Windows NT/2k/XP and Unix, if <em>path</em> is a Unicode object, the result will be a list of Unicode objects. Undecodable filenames will still be returned as string objects.</p> </div> </dd></dl> <dl class="function"> <dt id="os.lstat"> <code class="descclassname">os.</code><code class="descname">lstat</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.lstat" title="Permalink to this definition">¶</a></dt> <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. 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. On platforms that do not support symbolic links, this is an alias 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> </dd></dl> <dl class="function"> <dt id="os.mkfifo"> <code class="descclassname">os.</code><code class="descname">mkfifo</code><span class="sig-paren">(</span><em>path</em><span class="optional">[</span>, <em>mode</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.mkfifo" title="Permalink to this definition">¶</a></dt> <dd><p>Create a FIFO (a named pipe) named <em>path</em> with numeric mode <em>mode</em>. The default <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">0666</span></code> (octal). The current umask value is first masked out from the mode.</p> <p>Availability: Unix.</p> <p>FIFOs are pipes that can be accessed like regular files. FIFOs exist until they 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 rendezvous between “client” and “server” type processes: the server opens the 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> doesn’t open the FIFO — it just creates the rendezvous point.</p> </dd></dl> <dl class="function"> <dt id="os.mknod"> <code class="descclassname">os.</code><code class="descname">mknod</code><span class="sig-paren">(</span><em>filename</em><span class="optional">[</span>, <em>mode=0600</em><span class="optional">[</span>, <em>device=0</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.mknod" title="Permalink to this definition">¶</a></dt> <dd><p>Create a filesystem node (file, device special file or named pipe) named <em>filename</em>. <em>mode</em> specifies both the permissions to use and the type of node to be created, being combined (bitwise OR) with one of <code class="docutils literal notranslate"><span class="pre">stat.S_IFREG</span></code>, <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 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>, <em>device</em> defines the newly created device special file (probably using <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> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.major"> <code class="descclassname">os.</code><code class="descname">major</code><span class="sig-paren">(</span><em>device</em><span class="sig-paren">)</span><a class="headerlink" href="#os.major" title="Permalink to this definition">¶</a></dt> <dd><p>Extract the device major number from a raw device number (usually the <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-type docutils literal notranslate"><span class="pre">stat</span></code>).</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.minor"> <code class="descclassname">os.</code><code class="descname">minor</code><span class="sig-paren">(</span><em>device</em><span class="sig-paren">)</span><a class="headerlink" href="#os.minor" title="Permalink to this definition">¶</a></dt> <dd><p>Extract the device minor number from a raw device number (usually the <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-type docutils literal notranslate"><span class="pre">stat</span></code>).</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.makedev"> <code class="descclassname">os.</code><code class="descname">makedev</code><span class="sig-paren">(</span><em>major</em>, <em>minor</em><span class="sig-paren">)</span><a class="headerlink" href="#os.makedev" title="Permalink to this definition">¶</a></dt> <dd><p>Compose a raw device number from the major and minor device numbers.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.mkdir"> <code class="descclassname">os.</code><code class="descname">mkdir</code><span class="sig-paren">(</span><em>path</em><span class="optional">[</span>, <em>mode</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.mkdir" title="Permalink to this definition">¶</a></dt> <dd><p>Create a directory named <em>path</em> with numeric mode <em>mode</em>. The default <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">0777</span></code> (octal). If the directory already exists, <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised.</p> <p id="mkdir-modebits">On some systems, <em>mode</em> is ignored. Where it is used, the current umask value is first masked out. If bits other than the last 9 (i.e. the last 3 digits of the octal representation of the <em>mode</em>) are set, their meaning is platform-dependent. On some platforms, they are ignored and you should call <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> <p>It is also possible to create temporary 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> 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> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.makedirs"> <code class="descclassname">os.</code><code class="descname">makedirs</code><span class="sig-paren">(</span><em>path</em><span class="optional">[</span>, <em>mode</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.makedirs" title="Permalink to this definition">¶</a></dt> <dd><p id="index-14">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 intermediate-level directories needed to contain the leaf directory. Raises an <a class="reference internal" href="#os.error" title="os.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error</span></code></a> exception if the leaf directory already exists or cannot be created. The default <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">0777</span></code> (octal).</p> <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>; see <a class="reference internal" href="#mkdir-modebits"><span class="std std-ref">the mkdir() description</span></a> for how it is interpreted.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><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 include <a class="reference internal" href="#os.pardir" title="os.pardir"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.pardir</span></code></a>.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 1.5.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.3: </span>This function now handles UNC paths correctly.</p> </div> </dd></dl> <dl class="function"> <dt id="os.pathconf"> <code class="descclassname">os.</code><code class="descname">pathconf</code><span class="sig-paren">(</span><em>path</em>, <em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#os.pathconf" title="Permalink to this definition">¶</a></dt> <dd><p>Return system configuration information relevant to a named file. <em>name</em> specifies the configuration value to retrieve; it may be a string which is the name of a defined system value; these names are specified in a number of standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define additional names as well. The names known to the host operating system are given in the <code class="docutils literal notranslate"><span class="pre">pathconf_names</span></code> dictionary. For configuration variables not included in that mapping, passing an integer for <em>name</em> is also accepted.</p> <p>If <em>name</em> is a string and is not known, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. If a specific value for <em>name</em> is not supported by the host system, even if it is included in <code class="docutils literal notranslate"><span class="pre">pathconf_names</span></code>, an <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised with <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> <p>Availability: Unix.</p> </dd></dl> <dl class="data"> <dt id="os.pathconf_names"> <code class="descclassname">os.</code><code class="descname">pathconf_names</code><a class="headerlink" href="#os.pathconf_names" title="Permalink to this definition">¶</a></dt> <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 the integer values defined for those names by the host operating system. This can be used to determine the set of names known to the system. Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.readlink"> <code class="descclassname">os.</code><code class="descname">readlink</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.readlink" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the path to which the symbolic link points. The result may be either an absolute or relative pathname; if it is relative, it may be converted to an absolute pathname using <code class="docutils literal notranslate"><span class="pre">os.path.join(os.path.dirname(path),</span> <span class="pre">result)</span></code>.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.6: </span>If the <em>path</em> is a Unicode object the result will also be a Unicode object.</p> </div> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.remove"> <code class="descclassname">os.</code><code class="descname">remove</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.remove" title="Permalink to this definition">¶</a></dt> <dd><p>Remove (delete) the file <em>path</em>. If <em>path</em> is a directory, <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised; see <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> below to remove a directory. This is identical to the <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> function documented below. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.removedirs"> <code class="descclassname">os.</code><code class="descname">removedirs</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.removedirs" title="Permalink to this definition">¶</a></dt> <dd><p id="index-15">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 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 successively remove every parent directory mentioned in <em>path</em> until an error is raised (which is ignored, because it generally means that a parent directory is not empty). For example, <code class="docutils literal notranslate"><span class="pre">os.removedirs('foo/bar/baz')</span></code> will first remove 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 they are empty. Raises <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if the leaf directory could not be successfully removed.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 1.5.2.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.rename"> <code class="descclassname">os.</code><code class="descname">rename</code><span class="sig-paren">(</span><em>src</em>, <em>dst</em><span class="sig-paren">)</span><a class="headerlink" href="#os.rename" title="Permalink to this definition">¶</a></dt> <dd><p>Rename the file or directory <em>src</em> to <em>dst</em>. If <em>dst</em> is a directory, <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> will be raised. On Unix, if <em>dst</em> exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if <em>src</em> and <em>dst</em> are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if <em>dst</em> already exists, <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> will be raised even if it is a file; there may be no way to implement an atomic rename when <em>dst</em> names an existing file.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.renames"> <code class="descclassname">os.</code><code class="descname">renames</code><span class="sig-paren">(</span><em>old</em>, <em>new</em><span class="sig-paren">)</span><a class="headerlink" href="#os.renames" title="Permalink to this definition">¶</a></dt> <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 creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path 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> <div class="versionadded"> <p><span class="versionmodified">New in version 1.5.2.</span></p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This function can fail with the new directory structure made if you lack permissions needed to remove the leaf directory or file.</p> </div> </dd></dl> <dl class="function"> <dt id="os.rmdir"> <code class="descclassname">os.</code><code class="descname">rmdir</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.rmdir" title="Permalink to this definition">¶</a></dt> <dd><p>Remove (delete) the directory <em>path</em>. Only works when the directory is empty, otherwise, <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised. In order to remove whole directory trees, <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> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.stat"> <code class="descclassname">os.</code><code class="descname">stat</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.stat" title="Permalink to this definition">¶</a></dt> <dd><p>Perform the equivalent of a <code class="xref c c-func docutils literal notranslate"><span class="pre">stat()</span></code> system call on the given path. (This function follows symlinks; to stat a symlink 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> <p>The return value is an object whose attributes correspond to the members of the <code class="xref c c-type docutils literal notranslate"><span class="pre">stat</span></code> structure, namely:</p> <ul class="simple"> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mode</span></code> - protection bits,</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ino</span></code> - inode number,</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_dev</span></code> - device,</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_nlink</span></code> - number of hard links,</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_uid</span></code> - user id of owner,</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_gid</span></code> - group id of owner,</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_size</span></code> - size of file, in bytes,</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime</span></code> - time of most recent access,</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime</span></code> - time of most recent content modification,</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ctime</span></code> - platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows)</li> </ul> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.3: </span>If <a class="reference internal" href="#os.stat_float_times" title="os.stat_float_times"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat_float_times()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">True</span></code>, the time values are floats, measuring seconds. Fractions of a second may be reported if the system supports that. See <a class="reference internal" href="#os.stat_float_times" title="os.stat_float_times"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat_float_times()</span></code></a> for further discussion.</p> </div> <p>On some Unix systems (such as Linux), the following attributes may also be available:</p> <ul class="simple"> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_blocks</span></code> - number of 512-byte blocks allocated for file</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_blksize</span></code> - filesystem blocksize for efficient file system I/O</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_rdev</span></code> - type of device if an inode device</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_flags</span></code> - user defined flags for file</li> </ul> <p>On other Unix systems (such as FreeBSD), the following attributes may be available (but may be only filled out if root tries to use them):</p> <ul class="simple"> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_gen</span></code> - file generation number</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_birthtime</span></code> - time of file creation</li> </ul> <p>On RISCOS systems, the following attributes are also available:</p> <ul class="simple"> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ftype</span></code> (file type)</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_attrs</span></code> (attributes)</li> <li><code class="xref py py-attr docutils literal notranslate"><span class="pre">st_obtype</span></code> (object type).</li> </ul> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The exact meaning and resolution of the <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime</span></code>, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ctime</span></code> attributes depend on the operating system and the file system. For example, on Windows systems using the FAT or FAT32 file systems, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime</span></code> has 2-second resolution, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime</span></code> has only 1-day resolution. See your operating system documentation for details.</p> </div> <p>For backward compatibility, the return value of <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> is also accessible as a tuple of at least 10 integers giving the most important (and portable) members of the <code class="xref c c-type docutils literal notranslate"><span class="pre">stat</span></code> structure, in the order <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mode</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ino</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_dev</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_nlink</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_uid</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_gid</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_size</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_atime</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_mtime</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_ctime</span></code>. More items may be added at the end by some implementations.</p> <p id="index-16">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 useful for extracting information from a <code class="xref c c-type docutils literal notranslate"><span class="pre">stat</span></code> structure. (On Windows, some items are filled with dummy values.)</p> <p>Example:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">os</span> <span class="gp">>>> </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">'somefile.txt'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">statinfo</span> <span class="go">(33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)</span> <span class="gp">>>> </span><span class="n">statinfo</span><span class="o">.</span><span class="n">st_size</span> <span class="go">926</span> </pre></div> </div> <p>Availability: Unix, Windows.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2: </span>Added access to values as attributes of the returned object.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.5: </span>Added <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_gen</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">st_birthtime</span></code>.</p> </div> </dd></dl> <dl class="function"> <dt id="os.stat_float_times"> <code class="descclassname">os.</code><code class="descname">stat_float_times</code><span class="sig-paren">(</span><span class="optional">[</span><em>newvalue</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.stat_float_times" title="Permalink to this definition">¶</a></dt> <dd><p>Determine whether <code class="xref py py-class docutils literal notranslate"><span class="pre">stat_result</span></code> represents time stamps as float objects. If <em>newvalue</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, future calls 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> return floats, if it is <code class="docutils literal notranslate"><span class="pre">False</span></code>, future calls return ints. If <em>newvalue</em> is omitted, return the current setting.</p> <p>For compatibility with older Python versions, accessing <code class="xref py py-class docutils literal notranslate"><span class="pre">stat_result</span></code> as a tuple always returns integers.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.5: </span>Python now returns float values by default. Applications which do not work correctly with floating point time stamps can use this function to restore the old behaviour.</p> </div> <p>The resolution of the timestamps (that is the smallest possible fraction) depends on the system. Some systems only support second resolution; on these systems, the fraction will always be zero.</p> <p>It is recommended that this setting is only changed at program startup time in the <em>__main__</em> module; libraries should never change this setting. If an application uses a library that works incorrectly if floating point time stamps are processed, this application should turn the feature off until the library has been corrected.</p> </dd></dl> <dl class="function"> <dt id="os.statvfs"> <code class="descclassname">os.</code><code class="descname">statvfs</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.statvfs" title="Permalink to this definition">¶</a></dt> <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 an object whose attributes describe the filesystem on the given path, and correspond to the members of the <code class="xref c c-type docutils literal notranslate"><span class="pre">statvfs</span></code> structure, namely: <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>, <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>, <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>.</p> <p id="index-17">For backward compatibility, the return value is also accessible as a tuple whose values correspond to the attributes, in the order given above. The standard module <a class="reference internal" href="statvfs.html#module-statvfs" title="statvfs: Constants for interpreting the result of os.statvfs(). (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">statvfs</span></code></a> defines constants that are useful for extracting information from a <code class="xref c c-type docutils literal notranslate"><span class="pre">statvfs</span></code> structure when accessing it as a sequence; this remains useful when writing code that needs to work with versions of Python that don’t support accessing the fields as attributes.</p> <p>Availability: Unix.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2: </span>Added access to values as attributes of the returned object.</p> </div> </dd></dl> <dl class="function"> <dt id="os.symlink"> <code class="descclassname">os.</code><code class="descname">symlink</code><span class="sig-paren">(</span><em>source</em>, <em>link_name</em><span class="sig-paren">)</span><a class="headerlink" href="#os.symlink" title="Permalink to this definition">¶</a></dt> <dd><p>Create a symbolic link pointing to <em>source</em> named <em>link_name</em>.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.tempnam"> <code class="descclassname">os.</code><code class="descname">tempnam</code><span class="sig-paren">(</span><span class="optional">[</span><em>dir</em><span class="optional">[</span>, <em>prefix</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.tempnam" title="Permalink to this definition">¶</a></dt> <dd><p>Return a unique path name that is reasonable for creating a temporary file. This will be an absolute path that names a potential directory entry in the directory <em>dir</em> or a common location for temporary files if <em>dir</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>. If given and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, <em>prefix</em> is used to provide a short prefix to the filename. Applications are responsible for properly creating and managing files created using paths returned by <a class="reference internal" href="#os.tempnam" title="os.tempnam"><code class="xref py py-func docutils literal notranslate"><span class="pre">tempnam()</span></code></a>; no automatic cleanup is provided. On Unix, the environment variable <span class="target" id="index-18"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TMPDIR</span></code> overrides <em>dir</em>, while on Windows <span class="target" id="index-19"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TMP</span></code> is used. The specific behavior of this function depends on the C library implementation; some aspects are underspecified in system documentation.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Use of <a class="reference internal" href="#os.tempnam" title="os.tempnam"><code class="xref py py-func docutils literal notranslate"><span class="pre">tempnam()</span></code></a> is vulnerable to symlink attacks; consider using <a class="reference internal" href="#os.tmpfile" title="os.tmpfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">tmpfile()</span></code></a> (section <a class="reference internal" href="#os-newstreams"><span class="std std-ref">File Object Creation</span></a>) instead.</p> </div> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.tmpnam"> <code class="descclassname">os.</code><code class="descname">tmpnam</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.tmpnam" title="Permalink to this definition">¶</a></dt> <dd><p>Return a unique path name that is reasonable for creating a temporary file. This will be an absolute path that names a potential directory entry in a common location for temporary files. Applications are responsible for properly creating and managing files created using paths returned by <a class="reference internal" href="#os.tmpnam" title="os.tmpnam"><code class="xref py py-func docutils literal notranslate"><span class="pre">tmpnam()</span></code></a>; no automatic cleanup is provided.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Use of <a class="reference internal" href="#os.tmpnam" title="os.tmpnam"><code class="xref py py-func docutils literal notranslate"><span class="pre">tmpnam()</span></code></a> is vulnerable to symlink attacks; consider using <a class="reference internal" href="#os.tmpfile" title="os.tmpfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">tmpfile()</span></code></a> (section <a class="reference internal" href="#os-newstreams"><span class="std std-ref">File Object Creation</span></a>) instead.</p> </div> <p>Availability: Unix, Windows. This function probably shouldn’t be used on Windows, though: Microsoft’s implementation of <a class="reference internal" href="#os.tmpnam" title="os.tmpnam"><code class="xref py py-func docutils literal notranslate"><span class="pre">tmpnam()</span></code></a> always creates a name in the root directory of the current drive, and that’s generally a poor location for a temp file (depending on privileges, you may not even be able to open a file using this name).</p> </dd></dl> <dl class="data"> <dt id="os.TMP_MAX"> <code class="descclassname">os.</code><code class="descname">TMP_MAX</code><a class="headerlink" href="#os.TMP_MAX" title="Permalink to this definition">¶</a></dt> <dd><p>The maximum number of unique names that <a class="reference internal" href="#os.tmpnam" title="os.tmpnam"><code class="xref py py-func docutils literal notranslate"><span class="pre">tmpnam()</span></code></a> will generate before reusing names.</p> </dd></dl> <dl class="function"> <dt id="os.unlink"> <code class="descclassname">os.</code><code class="descname">unlink</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.unlink" title="Permalink to this definition">¶</a></dt> <dd><p>Remove (delete) the file <em>path</em>. This is the same function as <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 <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> name is its traditional Unix name.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.utime"> <code class="descclassname">os.</code><code class="descname">utime</code><span class="sig-paren">(</span><em>path</em>, <em>times</em><span class="sig-paren">)</span><a class="headerlink" href="#os.utime" title="Permalink to this definition">¶</a></dt> <dd><p>Set the access and modified times of the file specified by <em>path</em>. If <em>times</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, then the file’s access and modified times are set to the current time. (The effect is similar to running the Unix program <strong class="program">touch</strong> on the path.) Otherwise, <em>times</em> must be a 2-tuple of numbers, of the form <code class="docutils literal notranslate"><span class="pre">(atime,</span> <span class="pre">mtime)</span></code> which is used to set the access and modified times, respectively. Whether a directory can be given for <em>path</em> depends on whether the operating system implements directories as files (for example, Windows does not). Note that the exact times you set here may not be returned by a subsequent <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 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>.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.0: </span>Added support for <code class="docutils literal notranslate"><span class="pre">None</span></code> for <em>times</em>.</p> </div> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.walk"> <code class="descclassname">os.</code><code class="descname">walk</code><span class="sig-paren">(</span><em>top</em>, <em>topdown=True</em>, <em>onerror=None</em>, <em>followlinks=False</em><span class="sig-paren">)</span><a class="headerlink" href="#os.walk" title="Permalink to this definition">¶</a></dt> <dd><p id="index-20">Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory <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> <span class="pre">filenames)</span></code>.</p> <p><em>dirpath</em> is a string, the path to the directory. <em>dirnames</em> is a list of the names of the subdirectories in <em>dirpath</em> (excluding <code class="docutils literal notranslate"><span class="pre">'.'</span></code> and <code class="docutils literal notranslate"><span class="pre">'..'</span></code>). <em>filenames</em> is a list of the names of the non-directory files in <em>dirpath</em>. Note that the names in the lists contain no path components. To get a full path (which begins with <em>top</em>) to a file or directory in <em>dirpath</em>, do <code class="docutils literal notranslate"><span class="pre">os.path.join(dirpath,</span> <span class="pre">name)</span></code>.</p> <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 directory is generated before the triples for any of its subdirectories (directories are generated top-down). If <em>topdown</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom-up). No matter the value of <em>topdown</em>, the list of subdirectories is retrieved before the tuples for the directory and its subdirectories are generated.</p> <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 (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 recurse into the subdirectories whose names remain in <em>dirnames</em>; this can be used to prune the search, impose a specific order of visiting, or even to inform <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 <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 no effect on the behavior of the walk, because in bottom-up mode the directories in <em>dirnames</em> are generated before <em>dirpath</em> itself is generated.</p> <p>By default, errors from the <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> call are ignored. If optional argument <em>onerror</em> is specified, it should be a function; it will be called with one argument, an <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.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 with the walk, or raise the exception to abort the walk. Note that the filename is available as the <code class="docutils literal notranslate"><span class="pre">filename</span></code> attribute of the exception object.</p> <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 directories. Set <em>followlinks</em> to <code class="docutils literal notranslate"><span class="pre">True</span></code> to visit directories pointed to by symlinks, on systems that support them.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.6: </span>The <em>followlinks</em> parameter.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Be aware that setting <em>followlinks</em> to <code class="docutils literal notranslate"><span class="pre">True</span></code> can lead to infinite 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> does not keep track of the directories it visited already.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">If you pass a relative pathname, don’t change the current working directory 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 directory, and assumes that its caller doesn’t either.</p> </div> <p>This example displays the number of bytes taken by non-directory files in each directory under the starting directory, except that it doesn’t look under any CVS subdirectory:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span> <span class="kn">from</span> <span class="nn">os.path</span> <span class="k">import</span> <span class="n">join</span><span class="p">,</span> <span class="n">getsize</span> <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">'python/Lib/email'</span><span class="p">):</span> <span class="nb">print</span> <span class="n">root</span><span class="p">,</span> <span class="s2">"consumes"</span><span class="p">,</span> <span class="nb">print</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="nb">print</span> <span class="s2">"bytes in"</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">"non-directory files"</span> <span class="k">if</span> <span class="s1">'CVS'</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span> <span class="n">dirs</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">'CVS'</span><span class="p">)</span> <span class="c1"># don't visit CVS directories</span> </pre></div> </div> <p>In the next example, 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 deleting a directory before the directory is empty:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Delete everything reachable from the directory named in "top",</span> <span class="c1"># assuming there are no symbolic links.</span> <span class="c1"># CAUTION: This is dangerous! For example, if top == '/', it</span> <span class="c1"># could delete all your disk files.</span> <span class="kn">import</span> <span class="nn">os</span> <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> <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">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> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span> <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> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> </div> <div class="section" id="process-management"> <span id="os-process"></span><h2>15.1.5. Process Management<a class="headerlink" href="#process-management" title="Permalink to this headline">¶</a></h2> <p>These functions may be used to create and manage processes.</p> <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 program loaded into the process. In each case, the first of these arguments is passed to the new program as its own name rather than as an argument a user may 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> 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> <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 to be ignored.</p> <dl class="function"> <dt id="os.abort"> <code class="descclassname">os.</code><code class="descname">abort</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.abort" title="Permalink to this definition">¶</a></dt> <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 behavior is to produce a core dump; on Windows, the process immediately returns 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 Python signal handler registered for <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGABRT</span></code> with <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> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.execl"> <code class="descclassname">os.</code><code class="descname">execl</code><span class="sig-paren">(</span><em>path</em>, <em>arg0</em>, <em>arg1</em>, <em>...</em><span class="sig-paren">)</span><a class="headerlink" href="#os.execl" title="Permalink to this definition">¶</a></dt> <dt id="os.execle"> <code class="descclassname">os.</code><code class="descname">execle</code><span class="sig-paren">(</span><em>path</em>, <em>arg0</em>, <em>arg1</em>, <em>...</em>, <em>env</em><span class="sig-paren">)</span><a class="headerlink" href="#os.execle" title="Permalink to this definition">¶</a></dt> <dt id="os.execlp"> <code class="descclassname">os.</code><code class="descname">execlp</code><span class="sig-paren">(</span><em>file</em>, <em>arg0</em>, <em>arg1</em>, <em>...</em><span class="sig-paren">)</span><a class="headerlink" href="#os.execlp" title="Permalink to this definition">¶</a></dt> <dt id="os.execlpe"> <code class="descclassname">os.</code><code class="descname">execlpe</code><span class="sig-paren">(</span><em>file</em>, <em>arg0</em>, <em>arg1</em>, <em>...</em>, <em>env</em><span class="sig-paren">)</span><a class="headerlink" href="#os.execlpe" title="Permalink to this definition">¶</a></dt> <dt id="os.execv"> <code class="descclassname">os.</code><code class="descname">execv</code><span class="sig-paren">(</span><em>path</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#os.execv" title="Permalink to this definition">¶</a></dt> <dt id="os.execve"> <code class="descclassname">os.</code><code class="descname">execve</code><span class="sig-paren">(</span><em>path</em>, <em>args</em>, <em>env</em><span class="sig-paren">)</span><a class="headerlink" href="#os.execve" title="Permalink to this definition">¶</a></dt> <dt id="os.execvp"> <code class="descclassname">os.</code><code class="descname">execvp</code><span class="sig-paren">(</span><em>file</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#os.execvp" title="Permalink to this definition">¶</a></dt> <dt id="os.execvpe"> <code class="descclassname">os.</code><code class="descname">execvpe</code><span class="sig-paren">(</span><em>file</em>, <em>args</em>, <em>env</em><span class="sig-paren">)</span><a class="headerlink" href="#os.execvpe" title="Permalink to this definition">¶</a></dt> <dd><p>These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller. Errors will be reported as <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> exceptions.</p> <p>The current process is replaced immediately. Open file objects and descriptors are not flushed, so if there may be data buffered on these open files, you should flush them using <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 <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> <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 command-line arguments are passed. The “l” variants are perhaps the easiest to work with if the number of parameters is fixed when the code is written; the individual parameters simply become additional parameters to the <code class="xref py py-func docutils literal notranslate"><span class="pre">execl*()</span></code> functions. The “v” variants are good when the number of parameters is variable, with the arguments being passed in a list or tuple as the <em>args</em> parameter. In either case, the arguments to the child process should start with the name of the command being run, but this is not enforced.</p> <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>, <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 <span class="target" id="index-21"></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 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, discussed in the next paragraph), the new environment is used as the source of the <span class="target" id="index-22"></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>, <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-23"></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 appropriate absolute or relative path.</p> <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 that these all end in “e”), the <em>env</em> parameter must be a mapping which is used to define the environment variables for the new process (these are used 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>, <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 inherit the environment of the current process.</p> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os._exit"> <code class="descclassname">os.</code><code class="descname">_exit</code><span class="sig-paren">(</span><em>n</em><span class="sig-paren">)</span><a class="headerlink" href="#os._exit" title="Permalink to this definition">¶</a></dt> <dd><p>Exit the process with status <em>n</em>, without calling cleanup handlers, flushing stdio buffers, etc.</p> <p>Availability: Unix, Windows.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The standard way to exit is <code class="docutils literal notranslate"><span class="pre">sys.exit(n)</span></code>. <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> should 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> </div> </dd></dl> <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>, although they are not required. These are typically used for system programs written in Python, such as a mail server’s external command delivery program.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Some of these may not be available on all Unix platforms, since there is some variation. These constants are defined where they are defined by the underlying platform.</p> </div> <dl class="data"> <dt id="os.EX_OK"> <code class="descclassname">os.</code><code class="descname">EX_OK</code><a class="headerlink" href="#os.EX_OK" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means no error occurred.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_USAGE"> <code class="descclassname">os.</code><code class="descname">EX_USAGE</code><a class="headerlink" href="#os.EX_USAGE" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means the command was used incorrectly, such as when the wrong number of arguments are given.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_DATAERR"> <code class="descclassname">os.</code><code class="descname">EX_DATAERR</code><a class="headerlink" href="#os.EX_DATAERR" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means the input data was incorrect.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_NOINPUT"> <code class="descclassname">os.</code><code class="descname">EX_NOINPUT</code><a class="headerlink" href="#os.EX_NOINPUT" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means an input file did not exist or was not readable.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_NOUSER"> <code class="descclassname">os.</code><code class="descname">EX_NOUSER</code><a class="headerlink" href="#os.EX_NOUSER" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means a specified user did not exist.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_NOHOST"> <code class="descclassname">os.</code><code class="descname">EX_NOHOST</code><a class="headerlink" href="#os.EX_NOHOST" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means a specified host did not exist.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_UNAVAILABLE"> <code class="descclassname">os.</code><code class="descname">EX_UNAVAILABLE</code><a class="headerlink" href="#os.EX_UNAVAILABLE" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means that a required service is unavailable.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_SOFTWARE"> <code class="descclassname">os.</code><code class="descname">EX_SOFTWARE</code><a class="headerlink" href="#os.EX_SOFTWARE" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means an internal software error was detected.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_OSERR"> <code class="descclassname">os.</code><code class="descname">EX_OSERR</code><a class="headerlink" href="#os.EX_OSERR" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means an operating system error was detected, such as the inability to fork or create a pipe.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_OSFILE"> <code class="descclassname">os.</code><code class="descname">EX_OSFILE</code><a class="headerlink" href="#os.EX_OSFILE" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means some system file did not exist, could not be opened, or had some other kind of error.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_CANTCREAT"> <code class="descclassname">os.</code><code class="descname">EX_CANTCREAT</code><a class="headerlink" href="#os.EX_CANTCREAT" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means a user specified output file could not be created.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_IOERR"> <code class="descclassname">os.</code><code class="descname">EX_IOERR</code><a class="headerlink" href="#os.EX_IOERR" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means that an error occurred while doing I/O on some file.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_TEMPFAIL"> <code class="descclassname">os.</code><code class="descname">EX_TEMPFAIL</code><a class="headerlink" href="#os.EX_TEMPFAIL" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means a temporary failure occurred. This indicates something that may not really be an error, such as a network connection that couldn’t be made during a retryable operation.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_PROTOCOL"> <code class="descclassname">os.</code><code class="descname">EX_PROTOCOL</code><a class="headerlink" href="#os.EX_PROTOCOL" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means that a protocol exchange was illegal, invalid, or not understood.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_NOPERM"> <code class="descclassname">os.</code><code class="descname">EX_NOPERM</code><a class="headerlink" href="#os.EX_NOPERM" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means that there were insufficient permissions to perform the operation (but not intended for file system problems).</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_CONFIG"> <code class="descclassname">os.</code><code class="descname">EX_CONFIG</code><a class="headerlink" href="#os.EX_CONFIG" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means that some kind of configuration error occurred.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.EX_NOTFOUND"> <code class="descclassname">os.</code><code class="descname">EX_NOTFOUND</code><a class="headerlink" href="#os.EX_NOTFOUND" title="Permalink to this definition">¶</a></dt> <dd><p>Exit code that means something like “an entry was not found”.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.fork"> <code class="descclassname">os.</code><code class="descname">fork</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.fork" title="Permalink to this definition">¶</a></dt> <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 parent. If an error occurs <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised.</p> <p>Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have known issues when using fork() from a thread.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">See <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> for applications that use the SSL module with fork().</p> </div> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.forkpty"> <code class="descclassname">os.</code><code class="descname">forkpty</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.forkpty" title="Permalink to this definition">¶</a></dt> <dd><p>Fork a child process, using a new pseudo-terminal as the child’s controlling 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 new child’s process id in the parent, and <em>fd</em> is the file descriptor of the master end of the pseudo-terminal. For a more portable approach, use the <a class="reference internal" href="pty.html#module-pty" title="pty: Pseudo-Terminal Handling for Linux. (Linux)"><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#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised.</p> <p>Availability: some flavors of Unix.</p> </dd></dl> <dl class="function"> <dt id="os.kill"> <code class="descclassname">os.</code><code class="descname">kill</code><span class="sig-paren">(</span><em>pid</em>, <em>sig</em><span class="sig-paren">)</span><a class="headerlink" href="#os.kill" title="Permalink to this definition">¶</a></dt> <dd><p id="index-24">Send signal <em>sig</em> to the process <em>pid</em>. Constants for the specific signals 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> <p>Windows: The <a class="reference internal" href="signal.html#signal.CTRL_C_EVENT" title="signal.CTRL_C_EVENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">signal.CTRL_C_EVENT</span></code></a> and <a class="reference internal" href="signal.html#signal.CTRL_BREAK_EVENT" title="signal.CTRL_BREAK_EVENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">signal.CTRL_BREAK_EVENT</span></code></a> signals are special signals which can only be sent to console processes which share a common console window, e.g., some subprocesses. Any other value for <em>sig</em> will cause the process to be unconditionally killed by the TerminateProcess API, and the exit code 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 process handles to be killed.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7: </span>Windows support</p> </div> </dd></dl> <dl class="function"> <dt id="os.killpg"> <code class="descclassname">os.</code><code class="descname">killpg</code><span class="sig-paren">(</span><em>pgid</em>, <em>sig</em><span class="sig-paren">)</span><a class="headerlink" href="#os.killpg" title="Permalink to this definition">¶</a></dt> <dd><p id="index-25">Send the signal <em>sig</em> to the process group <em>pgid</em>.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.nice"> <code class="descclassname">os.</code><code class="descname">nice</code><span class="sig-paren">(</span><em>increment</em><span class="sig-paren">)</span><a class="headerlink" href="#os.nice" title="Permalink to this definition">¶</a></dt> <dd><p>Add <em>increment</em> to the process’s “niceness”. Return the new niceness.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.plock"> <code class="descclassname">os.</code><code class="descname">plock</code><span class="sig-paren">(</span><em>op</em><span class="sig-paren">)</span><a class="headerlink" href="#os.plock" title="Permalink to this definition">¶</a></dt> <dd><p>Lock program segments into memory. The value of <em>op</em> (defined in <code class="docutils literal notranslate"><span class="pre"><sys/lock.h></span></code>) determines which segments are locked.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt> <code class="descclassname">os.</code><code class="descname">popen</code><span class="sig-paren">(</span><em>...</em><span class="sig-paren">)</span></dt> <dt> <code class="descclassname">os.</code><code class="descname">popen2</code><span class="sig-paren">(</span><em>...</em><span class="sig-paren">)</span></dt> <dt> <code class="descclassname">os.</code><code class="descname">popen3</code><span class="sig-paren">(</span><em>...</em><span class="sig-paren">)</span></dt> <dt> <code class="descclassname">os.</code><code class="descname">popen4</code><span class="sig-paren">(</span><em>...</em><span class="sig-paren">)</span></dt> <dd><p>Run child processes, returning opened pipes for communications. These functions are described in section <a class="reference internal" href="#os-newstreams"><span class="std std-ref">File Object Creation</span></a>.</p> </dd></dl> <dl class="function"> <dt id="os.spawnl"> <code class="descclassname">os.</code><code class="descname">spawnl</code><span class="sig-paren">(</span><em>mode</em>, <em>path</em>, <em>...</em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnl" title="Permalink to this definition">¶</a></dt> <dt id="os.spawnle"> <code class="descclassname">os.</code><code class="descname">spawnle</code><span class="sig-paren">(</span><em>mode</em>, <em>path</em>, <em>...</em>, <em>env</em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnle" title="Permalink to this definition">¶</a></dt> <dt id="os.spawnlp"> <code class="descclassname">os.</code><code class="descname">spawnlp</code><span class="sig-paren">(</span><em>mode</em>, <em>file</em>, <em>...</em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnlp" title="Permalink to this definition">¶</a></dt> <dt id="os.spawnlpe"> <code class="descclassname">os.</code><code class="descname">spawnlpe</code><span class="sig-paren">(</span><em>mode</em>, <em>file</em>, <em>...</em>, <em>env</em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnlpe" title="Permalink to this definition">¶</a></dt> <dt id="os.spawnv"> <code class="descclassname">os.</code><code class="descname">spawnv</code><span class="sig-paren">(</span><em>mode</em>, <em>path</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnv" title="Permalink to this definition">¶</a></dt> <dt id="os.spawnve"> <code class="descclassname">os.</code><code class="descname">spawnve</code><span class="sig-paren">(</span><em>mode</em>, <em>path</em>, <em>args</em>, <em>env</em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnve" title="Permalink to this definition">¶</a></dt> <dt id="os.spawnvp"> <code class="descclassname">os.</code><code class="descname">spawnvp</code><span class="sig-paren">(</span><em>mode</em>, <em>file</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnvp" title="Permalink to this definition">¶</a></dt> <dt id="os.spawnvpe"> <code class="descclassname">os.</code><code class="descname">spawnvpe</code><span class="sig-paren">(</span><em>mode</em>, <em>file</em>, <em>args</em>, <em>env</em><span class="sig-paren">)</span><a class="headerlink" href="#os.spawnvpe" title="Permalink to this definition">¶</a></dt> <dd><p>Execute the program <em>path</em> in a new process.</p> <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 spawning new processes and retrieving their results; using that module is preferable to using these functions. Check especially 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.)</p> <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 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 exits normally, or <code class="docutils literal notranslate"><span class="pre">-signal</span></code>, where <em>signal</em> is the signal that killed the process. On Windows, the process id will actually be the process handle, so can 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> <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 command-line arguments are passed. The “l” variants are perhaps the easiest to work with if the number of parameters is fixed when the code is written; the individual parameters simply become additional parameters to the <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 parameters is variable, with the arguments being passed in a list or tuple as the <em>args</em> parameter. In either case, the arguments to the child process must start with the name of the command being run.</p> <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>, <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 <span class="target" id="index-26"></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 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, discussed in the next paragraph), the new environment is used as the source of the <span class="target" id="index-27"></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>, <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 <span class="target" id="index-28"></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 appropriate absolute or relative path.</p> <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> (note that these all end in “e”), the <em>env</em> parameter must be a mapping which is used to define the environment variables for the new process (they are used instead of the current process’ environment); the functions <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 the new process to inherit the environment of the current process. Note that keys and values in the <em>env</em> dictionary must be strings; invalid keys or values will cause the function to fail, with a return value of <code class="docutils literal notranslate"><span class="pre">127</span></code>.</p> <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 equivalent:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span> <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">'cp'</span><span class="p">,</span> <span class="s1">'cp'</span><span class="p">,</span> <span class="s1">'index.html'</span><span class="p">,</span> <span class="s1">'/dev/null'</span><span class="p">)</span> <span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'cp'</span><span class="p">,</span> <span class="s1">'index.html'</span><span class="p">,</span> <span class="s1">'/dev/null'</span><span class="p">]</span> <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">'cp'</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> </pre></div> </div> <p>Availability: Unix, Windows. <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> 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 <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 <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> <div class="versionadded"> <p><span class="versionmodified">New in version 1.6.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.P_NOWAIT"> <code class="descclassname">os.</code><code class="descname">P_NOWAIT</code><a class="headerlink" href="#os.P_NOWAIT" title="Permalink to this definition">¶</a></dt> <dt id="os.P_NOWAITO"> <code class="descclassname">os.</code><code class="descname">P_NOWAITO</code><a class="headerlink" href="#os.P_NOWAITO" title="Permalink to this definition">¶</a></dt> <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 functions. If either of these values is given, the <code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*()</span></code> functions will return as soon as the new process has been created, with the process id as the return value.</p> <p>Availability: Unix, Windows.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 1.6.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.P_WAIT"> <code class="descclassname">os.</code><code class="descname">P_WAIT</code><a class="headerlink" href="#os.P_WAIT" title="Permalink to this definition">¶</a></dt> <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 functions. If this is given as <em>mode</em>, the <code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*()</span></code> functions will not return until the new process has run to completion and will return the exit code of the process the run is successful, or <code class="docutils literal notranslate"><span class="pre">-signal</span></code> if a signal kills the process.</p> <p>Availability: Unix, Windows.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 1.6.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.P_DETACH"> <code class="descclassname">os.</code><code class="descname">P_DETACH</code><a class="headerlink" href="#os.P_DETACH" title="Permalink to this definition">¶</a></dt> <dt id="os.P_OVERLAY"> <code class="descclassname">os.</code><code class="descname">P_OVERLAY</code><a class="headerlink" href="#os.P_OVERLAY" title="Permalink to this definition">¶</a></dt> <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 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> 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 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 process will be replaced; the <code class="xref py py-func docutils literal notranslate"><span class="pre">spawn*()</span></code> function will not return.</p> <p>Availability: Windows.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 1.6.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.startfile"> <code class="descclassname">os.</code><code class="descname">startfile</code><span class="sig-paren">(</span><em>path</em><span class="optional">[</span>, <em>operation</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#os.startfile" title="Permalink to this definition">¶</a></dt> <dd><p>Start a file with its associated application.</p> <p>When <em>operation</em> is not specified or <code class="docutils literal notranslate"><span class="pre">'open'</span></code>, this acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the <strong class="program">start</strong> command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated.</p> <p>When another <em>operation</em> is given, it must be a “command verb” that specifies what should be done with the file. Common verbs documented by Microsoft are <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 <code class="docutils literal notranslate"><span class="pre">'find'</span></code> (to be used on directories).</p> <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. There is no option to wait for the application to close, and no way to retrieve the application’s exit status. The <em>path</em> parameter is relative to the current directory. If you want to use an absolute path, make sure the first character is not a slash (<code class="docutils literal notranslate"><span class="pre">'/'</span></code>); the underlying Win32 <code class="xref c c-func docutils literal notranslate"><span class="pre">ShellExecute()</span></code> function doesn’t work if it is. Use the <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 the path is properly encoded for Win32.</p> <p>Availability: Windows.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.0.</span></p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5: </span>The <em>operation</em> parameter.</p> </div> </dd></dl> <dl class="function"> <dt id="os.system"> <code class="descclassname">os.</code><code class="descname">system</code><span class="sig-paren">(</span><em>command</em><span class="sig-paren">)</span><a class="headerlink" href="#os.system" title="Permalink to this definition">¶</a></dt> <dd><p>Execute the command (a string) in a subshell. This is implemented by calling the Standard C function <code class="xref c c-func docutils literal notranslate"><span class="pre">system()</span></code>, and has the same limitations. 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 the executed command.</p> <p>On Unix, the return value is the exit status of the process encoded in the 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>. Note that POSIX does not specify the meaning of the return value of the C <code class="xref c c-func docutils literal notranslate"><span class="pre">system()</span></code> function, so the return value of the Python function is system-dependent.</p> <p>On Windows, the return value is that returned by the system shell after running <em>command</em>, given by the Windows environment variable <span class="target" id="index-29"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">COMSPEC</span></code>: on <strong class="program">command.com</strong> systems (Windows 95, 98 and ME) this is always <code class="docutils literal notranslate"><span class="pre">0</span></code>; on <strong class="program">cmd.exe</strong> systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.</p> <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 new processes and retrieving their results; using that module is preferable 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 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> <p>Availability: Unix, Windows.</p> </dd></dl> <dl class="function"> <dt id="os.times"> <code class="descclassname">os.</code><code class="descname">times</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.times" title="Permalink to this definition">¶</a></dt> <dd><p>Return a 5-tuple of floating point numbers indicating accumulated (processor or other) times, in seconds. The items are: user time, system time, children’s user time, children’s system time, and elapsed real time since a fixed point in the past, in that order. See the Unix manual page <em class="manpage">times(2)</em> or the corresponding Windows Platform API documentation. On Windows, only the first two items are filled, the others are zero.</p> <p>Availability: Unix, Windows</p> </dd></dl> <dl class="function"> <dt id="os.wait"> <code class="descclassname">os.</code><code class="descname">wait</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.wait" title="Permalink to this definition">¶</a></dt> <dd><p>Wait for completion of a child process, and return a tuple containing its pid and exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.waitpid"> <code class="descclassname">os.</code><code class="descname">waitpid</code><span class="sig-paren">(</span><em>pid</em>, <em>options</em><span class="sig-paren">)</span><a class="headerlink" href="#os.waitpid" title="Permalink to this definition">¶</a></dt> <dd><p>The details of this function differ on Unix and Windows.</p> <p>On Unix: Wait for completion of a child process given by process id <em>pid</em>, and return a tuple containing its process id and exit status indication (encoded as 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 integer <em>options</em>, which should be <code class="docutils literal notranslate"><span class="pre">0</span></code> for normal operation.</p> <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 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 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 request pertains to any child of the current process. If <em>pid</em> is less than <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 absolute value of <em>pid</em>).</p> <p>An <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised with the value of errno when the syscall returns -1.</p> <p>On Windows: Wait for completion of a process given by process handle <em>pid</em>, and return a tuple containing <em>pid</em>, and its exit status shifted left by 8 bits (shifting makes cross-platform use of the function easier). A <em>pid</em> less than or equal to <code class="docutils literal notranslate"><span class="pre">0</span></code> has no special meaning on Windows, and raises an exception. The value of integer <em>options</em> has no effect. <em>pid</em> can refer to any process whose 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> 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> </dd></dl> <dl class="function"> <dt id="os.wait3"> <code class="descclassname">os.</code><code class="descname">wait3</code><span class="sig-paren">(</span><em>options</em><span class="sig-paren">)</span><a class="headerlink" href="#os.wait3" title="Permalink to this definition">¶</a></dt> <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 3-element tuple containing the child’s process id, exit status indication, and resource usage information is returned. Refer to <a class="reference internal" href="resource.html#module-resource" title="resource: An interface to provide resource usage information on the current process. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">resource</span></code></a>.<a class="reference internal" href="resource.html#resource.getrusage" title="resource.getrusage"><code class="xref py py-func docutils literal notranslate"><span class="pre">getrusage()</span></code></a> for details on resource usage information. The option 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 <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> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.wait4"> <code class="descclassname">os.</code><code class="descname">wait4</code><span class="sig-paren">(</span><em>pid</em>, <em>options</em><span class="sig-paren">)</span><a class="headerlink" href="#os.wait4" title="Permalink to this definition">¶</a></dt> <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 process id, exit status indication, and resource usage information is returned. Refer to <a class="reference internal" href="resource.html#module-resource" title="resource: An interface to provide resource usage information on the current process. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">resource</span></code></a>.<a class="reference internal" href="resource.html#resource.getrusage" title="resource.getrusage"><code class="xref py py-func docutils literal notranslate"><span class="pre">getrusage()</span></code></a> for details on resource usage 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 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> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.WNOHANG"> <code class="descclassname">os.</code><code class="descname">WNOHANG</code><a class="headerlink" href="#os.WNOHANG" title="Permalink to this definition">¶</a></dt> <dd><p>The option 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> to return immediately if no child process status is available immediately. The function returns <code class="docutils literal notranslate"><span class="pre">(0,</span> <span class="pre">0)</span></code> in this case.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="data"> <dt id="os.WCONTINUED"> <code class="descclassname">os.</code><code class="descname">WCONTINUED</code><a class="headerlink" href="#os.WCONTINUED" title="Permalink to this definition">¶</a></dt> <dd><p>This option causes child processes to be reported if they have been continued from a job control stop since their status was last reported.</p> <p>Availability: Some Unix systems.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.WUNTRACED"> <code class="descclassname">os.</code><code class="descname">WUNTRACED</code><a class="headerlink" href="#os.WUNTRACED" title="Permalink to this definition">¶</a></dt> <dd><p>This option causes child processes to be reported if they have been stopped but their current state has not been reported since they were stopped.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <p>The following functions take a process status code as returned by <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 used to determine the disposition of a process.</p> <dl class="function"> <dt id="os.WCOREDUMP"> <code class="descclassname">os.</code><code class="descname">WCOREDUMP</code><span class="sig-paren">(</span><em>status</em><span class="sig-paren">)</span><a class="headerlink" href="#os.WCOREDUMP" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if a core dump was generated for the process, otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.WIFCONTINUED"> <code class="descclassname">os.</code><code class="descname">WIFCONTINUED</code><span class="sig-paren">(</span><em>status</em><span class="sig-paren">)</span><a class="headerlink" href="#os.WIFCONTINUED" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the process has been continued from a job control stop, otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.WIFSTOPPED"> <code class="descclassname">os.</code><code class="descname">WIFSTOPPED</code><span class="sig-paren">(</span><em>status</em><span class="sig-paren">)</span><a class="headerlink" href="#os.WIFSTOPPED" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the process has been stopped, otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.WIFSIGNALED"> <code class="descclassname">os.</code><code class="descname">WIFSIGNALED</code><span class="sig-paren">(</span><em>status</em><span class="sig-paren">)</span><a class="headerlink" href="#os.WIFSIGNALED" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the process exited due to a signal, otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.WIFEXITED"> <code class="descclassname">os.</code><code class="descname">WIFEXITED</code><span class="sig-paren">(</span><em>status</em><span class="sig-paren">)</span><a class="headerlink" href="#os.WIFEXITED" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the process exited using the <em class="manpage">exit(2)</em> system call, otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.WEXITSTATUS"> <code class="descclassname">os.</code><code class="descname">WEXITSTATUS</code><span class="sig-paren">(</span><em>status</em><span class="sig-paren">)</span><a class="headerlink" href="#os.WEXITSTATUS" title="Permalink to this definition">¶</a></dt> <dd><p>If <code class="docutils literal notranslate"><span class="pre">WIFEXITED(status)</span></code> is true, return the integer parameter to the <em class="manpage">exit(2)</em> system call. Otherwise, the return value is meaningless.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.WSTOPSIG"> <code class="descclassname">os.</code><code class="descname">WSTOPSIG</code><span class="sig-paren">(</span><em>status</em><span class="sig-paren">)</span><a class="headerlink" href="#os.WSTOPSIG" title="Permalink to this definition">¶</a></dt> <dd><p>Return the signal which caused the process to stop.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.WTERMSIG"> <code class="descclassname">os.</code><code class="descname">WTERMSIG</code><span class="sig-paren">(</span><em>status</em><span class="sig-paren">)</span><a class="headerlink" href="#os.WTERMSIG" title="Permalink to this definition">¶</a></dt> <dd><p>Return the signal which caused the process to exit.</p> <p>Availability: Unix.</p> </dd></dl> </div> <div class="section" id="miscellaneous-system-information"> <span id="os-path"></span><h2>15.1.6. Miscellaneous System Information<a class="headerlink" href="#miscellaneous-system-information" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="os.confstr"> <code class="descclassname">os.</code><code class="descname">confstr</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#os.confstr" title="Permalink to this definition">¶</a></dt> <dd><p>Return string-valued system configuration values. <em>name</em> specifies the configuration value to retrieve; it may be a string which is the name of a defined system value; these names are specified in a number of standards (POSIX, Unix 95, Unix 98, and others). Some platforms define additional names as well. The names known to the host operating system are given as the keys of the <code class="docutils literal notranslate"><span class="pre">confstr_names</span></code> dictionary. For configuration variables not included in that mapping, passing an integer for <em>name</em> is also accepted.</p> <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 returned.</p> <p>If <em>name</em> is a string and is not known, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. If a specific value for <em>name</em> is not supported by the host system, even if it is included in <code class="docutils literal notranslate"><span class="pre">confstr_names</span></code>, an <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised with <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> <p>Availability: Unix</p> </dd></dl> <dl class="data"> <dt id="os.confstr_names"> <code class="descclassname">os.</code><code class="descname">confstr_names</code><a class="headerlink" href="#os.confstr_names" title="Permalink to this definition">¶</a></dt> <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 defined for those names by the host operating system. This can be used to determine the set of names known to the system.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="function"> <dt id="os.getloadavg"> <code class="descclassname">os.</code><code class="descname">getloadavg</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#os.getloadavg" title="Permalink to this definition">¶</a></dt> <dd><p>Return the number of processes in the system run queue averaged over the last 1, 5, and 15 minutes or raises <a class="reference internal" href="exceptions.html#exceptions.OSError" title="exceptions.OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if the load average was unobtainable.</p> <p>Availability: Unix.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="os.sysconf"> <code class="descclassname">os.</code><code class="descname">sysconf</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#os.sysconf" title="Permalink to this definition">¶</a></dt> <dd><p>Return integer-valued system configuration values. If the configuration value specified by <em>name</em> isn’t defined, <code class="docutils literal notranslate"><span class="pre">-1</span></code> is returned. The comments regarding 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 provides information on the known names is given by <code class="docutils literal notranslate"><span class="pre">sysconf_names</span></code>.</p> <p>Availability: Unix.</p> </dd></dl> <dl class="data"> <dt id="os.sysconf_names"> <code class="descclassname">os.</code><code class="descname">sysconf_names</code><a class="headerlink" href="#os.sysconf_names" title="Permalink to this definition">¶</a></dt> <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 defined for those names by the host operating system. This can be used to determine the set of names known to the system.</p> <p>Availability: Unix.</p> </dd></dl> <p>The following data values are used to support path manipulation operations. These are defined for all platforms.</p> <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> <dl class="data"> <dt id="os.curdir"> <code class="descclassname">os.</code><code class="descname">curdir</code><a class="headerlink" href="#os.curdir" title="Permalink to this definition">¶</a></dt> <dd><p>The constant string used by the operating system to refer to the current directory. This is <code class="docutils literal notranslate"><span class="pre">'.'</span></code> for Windows and POSIX. 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> </dd></dl> <dl class="data"> <dt id="os.pardir"> <code class="descclassname">os.</code><code class="descname">pardir</code><a class="headerlink" href="#os.pardir" title="Permalink to this definition">¶</a></dt> <dd><p>The constant string used by the operating system to refer to the parent directory. This is <code class="docutils literal notranslate"><span class="pre">'..'</span></code> for Windows and POSIX. 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> </dd></dl> <dl class="data"> <dt id="os.sep"> <code class="descclassname">os.</code><code class="descname">sep</code><a class="headerlink" href="#os.sep" title="Permalink to this definition">¶</a></dt> <dd><p>The character used by the operating system to separate pathname components. 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 is not sufficient to be able to parse or concatenate pathnames — use <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 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> </dd></dl> <dl class="data"> <dt id="os.altsep"> <code class="descclassname">os.</code><code class="descname">altsep</code><a class="headerlink" href="#os.altsep" title="Permalink to this definition">¶</a></dt> <dd><p>An alternative character used by the operating system to separate pathname components, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if only one separator character exists. This is set to <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 <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> </dd></dl> <dl class="data"> <dt id="os.extsep"> <code class="descclassname">os.</code><code class="descname">extsep</code><a class="headerlink" href="#os.extsep" title="Permalink to this definition">¶</a></dt> <dd><p>The character which separates the base filename from the extension; for example, 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> <div class="versionadded"> <p><span class="versionmodified">New in version 2.2.</span></p> </div> </dd></dl> <dl class="data"> <dt id="os.pathsep"> <code class="descclassname">os.</code><code class="descname">pathsep</code><a class="headerlink" href="#os.pathsep" title="Permalink to this definition">¶</a></dt> <dd><p>The character conventionally used by the operating system to separate search path components (as in <span class="target" id="index-30"></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 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> </dd></dl> <dl class="data"> <dt id="os.defpath"> <code class="descclassname">os.</code><code class="descname">defpath</code><a class="headerlink" href="#os.defpath" title="Permalink to this definition">¶</a></dt> <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 <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> 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> </dd></dl> <dl class="data"> <dt id="os.linesep"> <code class="descclassname">os.</code><code class="descname">linesep</code><a class="headerlink" href="#os.linesep" title="Permalink to this definition">¶</a></dt> <dd><p>The string used to separate (or, rather, terminate) lines on the current platform. This may be a single character, such as <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> for POSIX, or multiple characters, for example, <code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code> for Windows. Do not use <em>os.linesep</em> as a line terminator when writing files opened in text mode (the default); use a single <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> instead, on all platforms.</p> </dd></dl> <dl class="data"> <dt id="os.devnull"> <code class="descclassname">os.</code><code class="descname">devnull</code><a class="headerlink" href="#os.devnull" title="Permalink to this definition">¶</a></dt> <dd><p>The file path of the null device. For example: <code class="docutils literal notranslate"><span class="pre">'/dev/null'</span></code> for 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> <div class="versionadded"> <p><span class="versionmodified">New in version 2.4.</span></p> </div> </dd></dl> </div> <div class="section" id="miscellaneous-functions"> <span id="os-miscfunc"></span><h2>15.1.7. Miscellaneous Functions<a class="headerlink" href="#miscellaneous-functions" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="os.urandom"> <code class="descclassname">os.</code><code class="descname">urandom</code><span class="sig-paren">(</span><em>n</em><span class="sig-paren">)</span><a class="headerlink" href="#os.urandom" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string of <em>n</em> random bytes suitable for cryptographic use.</p> <p>This function returns random bytes from an OS-specific randomness source. The returned data should be unpredictable enough for cryptographic applications, though its exact quality depends on the OS implementation. On a UNIX-like system this will query <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code>, and on Windows it will use <code class="docutils literal notranslate"><span class="pre">CryptGenRandom()</span></code>. If a randomness source is not found, <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> will be raised.</p> <p>For an easy-to-use interface to the random number generator provided by your 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> <div class="versionadded"> <p><span class="versionmodified">New in version 2.4.</span></p> </div> </dd></dl> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">15.1. <code class="docutils literal notranslate"><span class="pre">os</span></code> — Miscellaneous operating system interfaces</a><ul> <li><a class="reference internal" href="#process-parameters">15.1.1. Process Parameters</a></li> <li><a class="reference internal" href="#file-object-creation">15.1.2. File Object Creation</a></li> <li><a class="reference internal" href="#file-descriptor-operations">15.1.3. File Descriptor Operations</a><ul> <li><a class="reference internal" href="#open-flag-constants">15.1.3.1. <code class="docutils literal notranslate"><span class="pre">open()</span></code> flag constants</a></li> </ul> </li> <li><a class="reference internal" href="#files-and-directories">15.1.4. Files and Directories</a></li> <li><a class="reference internal" href="#process-management">15.1.5. Process Management</a></li> <li><a class="reference internal" href="#miscellaneous-system-information">15.1.6. Miscellaneous System Information</a></li> <li><a class="reference internal" href="#miscellaneous-functions">15.1.7. Miscellaneous Functions</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="allos.html" title="previous chapter">15. Generic Operating System Services</a></p> <h4>Next topic</h4> <p class="topless"><a href="io.html" title="next chapter">15.2. <code class="docutils literal notranslate"><span class="pre">io</span></code> — Core tools for working with streams</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/os.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="io.html" title="15.2. io — Core tools for working with streams" >next</a> |</li> <li class="right" > <a href="allos.html" title="15. Generic Operating System Services" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.16 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="allos.html" >15. Generic Operating System Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Mar 27, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.6. </div> </body> </html>