GIF89a;
Direktori : /usr/share/doc/python-docs-2.7.5/html/library/ |
Current File : //usr/share/doc/python-docs-2.7.5/html/library/commands.html |
<!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="Content-Type" content="text/html; charset=utf-8" /> <title>35.16. commands — Utilities for running commands — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </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.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="35. Unix Specific Services" href="unix.html" /> <link rel="next" title="36. Mac OS X specific services" href="mac.html" /> <link rel="prev" title="35.15. syslog — Unix syslog library routines" href="syslog.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <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="mac.html" title="36. Mac OS X specific services" accesskey="N">next</a> |</li> <li class="right" > <a href="syslog.html" title="35.15. syslog — Unix syslog library routines" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="unix.html" accesskey="U">35. Unix Specific Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-commands"> <span id="commands-utilities-for-running-commands"></span><h1>35.16. <a class="reference internal" href="#module-commands" title="commands: Utility functions for running external commands. (deprecated) (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">commands</span></tt></a> — Utilities for running commands<a class="headerlink" href="#module-commands" title="Permalink to this headline">¶</a></h1> <p class="deprecated"> <span class="versionmodified">Deprecated since version 2.6: </span>The <a class="reference internal" href="#module-commands" title="commands: Utility functions for running external commands. (deprecated) (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">commands</span></tt></a> module has been removed in Python 3. Use the <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module instead.</p> <p>The <a class="reference internal" href="#module-commands" title="commands: Utility functions for running external commands. (deprecated) (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">commands</span></tt></a> module contains wrapper functions for <a class="reference internal" href="os.html#os.popen" title="os.popen"><tt class="xref py py-func docutils literal"><span class="pre">os.popen()</span></tt></a> which take a system command as a string and return any output generated by the command and, optionally, the exit status.</p> <p>The <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module provides more powerful facilities for spawning new processes and retrieving their results. Using the <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module is preferable to using the <a class="reference internal" href="#module-commands" title="commands: Utility functions for running external commands. (deprecated) (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">commands</span></tt></a> module.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">In Python 3.x, <a class="reference internal" href="#commands.getstatus" title="commands.getstatus"><tt class="xref py py-func docutils literal"><span class="pre">getstatus()</span></tt></a> and two undocumented functions (<tt class="xref py py-func docutils literal"><span class="pre">mk2arg()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">mkarg()</span></tt>) have been removed. Also, <a class="reference internal" href="#commands.getstatusoutput" title="commands.getstatusoutput"><tt class="xref py py-func docutils literal"><span class="pre">getstatusoutput()</span></tt></a> and <a class="reference internal" href="#commands.getoutput" title="commands.getoutput"><tt class="xref py py-func docutils literal"><span class="pre">getoutput()</span></tt></a> have been moved to the <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module.</p> </div> <p>The <a class="reference internal" href="#module-commands" title="commands: Utility functions for running external commands. (deprecated) (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">commands</span></tt></a> module defines the following functions:</p> <dl class="function"> <dt id="commands.getstatusoutput"> <tt class="descclassname">commands.</tt><tt class="descname">getstatusoutput</tt><big>(</big><em>cmd</em><big>)</big><a class="headerlink" href="#commands.getstatusoutput" title="Permalink to this definition">¶</a></dt> <dd><p>Execute the string <em>cmd</em> in a shell with <a class="reference internal" href="os.html#os.popen" title="os.popen"><tt class="xref py py-func docutils literal"><span class="pre">os.popen()</span></tt></a> and return a 2-tuple <tt class="docutils literal"><span class="pre">(status,</span> <span class="pre">output)</span></tt>. <em>cmd</em> is actually run as <tt class="docutils literal"><span class="pre">{</span> <span class="pre">cmd</span> <span class="pre">;</span> <span class="pre">}</span> <span class="pre">2>&1</span></tt>, so that the returned output will contain output or error messages. A trailing newline is stripped from the output. The exit status for the command can be interpreted according to the rules for the C function <tt class="xref c c-func docutils literal"><span class="pre">wait()</span></tt>.</p> </dd></dl> <dl class="function"> <dt id="commands.getoutput"> <tt class="descclassname">commands.</tt><tt class="descname">getoutput</tt><big>(</big><em>cmd</em><big>)</big><a class="headerlink" href="#commands.getoutput" title="Permalink to this definition">¶</a></dt> <dd><p>Like <a class="reference internal" href="#commands.getstatusoutput" title="commands.getstatusoutput"><tt class="xref py py-func docutils literal"><span class="pre">getstatusoutput()</span></tt></a>, except the exit status is ignored and the return value is a string containing the command’s output.</p> </dd></dl> <dl class="function"> <dt id="commands.getstatus"> <tt class="descclassname">commands.</tt><tt class="descname">getstatus</tt><big>(</big><em>file</em><big>)</big><a class="headerlink" href="#commands.getstatus" title="Permalink to this definition">¶</a></dt> <dd><p>Return the output of <tt class="docutils literal"><span class="pre">ls</span> <span class="pre">-ld</span> <span class="pre">file</span></tt> as a string. This function uses the <a class="reference internal" href="#commands.getoutput" title="commands.getoutput"><tt class="xref py py-func docutils literal"><span class="pre">getoutput()</span></tt></a> function, and properly escapes backslashes and dollar signs in the argument.</p> <p class="deprecated"> <span class="versionmodified">Deprecated since version 2.6: </span>This function is nonobvious and useless. The name is also misleading in the presence of <a class="reference internal" href="#commands.getstatusoutput" title="commands.getstatusoutput"><tt class="xref py py-func docutils literal"><span class="pre">getstatusoutput()</span></tt></a>.</p> </dd></dl> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">commands</span> <span class="gp">>>> </span><span class="n">commands</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s">'ls /bin/ls'</span><span class="p">)</span> <span class="go">(0, '/bin/ls')</span> <span class="gp">>>> </span><span class="n">commands</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s">'cat /bin/junk'</span><span class="p">)</span> <span class="go">(256, 'cat: /bin/junk: No such file or directory')</span> <span class="gp">>>> </span><span class="n">commands</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s">'/bin/junk'</span><span class="p">)</span> <span class="go">(256, 'sh: /bin/junk: not found')</span> <span class="gp">>>> </span><span class="n">commands</span><span class="o">.</span><span class="n">getoutput</span><span class="p">(</span><span class="s">'ls /bin/ls'</span><span class="p">)</span> <span class="go">'/bin/ls'</span> <span class="gp">>>> </span><span class="n">commands</span><span class="o">.</span><span class="n">getstatus</span><span class="p">(</span><span class="s">'/bin/ls'</span><span class="p">)</span> <span class="go">'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'</span> </pre></div> </div> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt>Module <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a></dt> <dd>Module for spawning and managing subprocesses.</dd> </dl> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="syslog.html" title="previous chapter">35.15. <tt class="docutils literal"><span class="pre">syslog</span></tt> — Unix syslog library routines</a></p> <h4>Next topic</h4> <p class="topless"><a href="mac.html" title="next chapter">36. Mac OS X specific services</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/commands.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <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> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <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="mac.html" title="36. Mac OS X specific services" >next</a> |</li> <li class="right" > <a href="syslog.html" title="35.15. syslog — Unix syslog library routines" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="unix.html" >35. Unix Specific 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="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>