Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8f1462e52e1797a02c97073eed0b7f92 > files > 685

python-docs-2.6.5-2.5mdv2010.2.i586.rpm

<!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>36.16. commands — Utilities for running commands &mdash; Python v2.6.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.6.5',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.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 v2.6.5 documentation" href="../index.html" />
    <link rel="up" title="36. Unix Specific Services" href="unix.html" />
    <link rel="next" title="37. Mac OS X specific services" href="mac.html" />
    <link rel="prev" title="36.15. syslog — Unix syslog library routines" href="syslog.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </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="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="mac.html" title="37. Mac OS X specific services"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="syslog.html" title="36.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="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="unix.html" accesskey="U">36. Unix Specific Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-commands">
<h1>36.16. <tt class="xref docutils literal"><span class="pre">commands</span></tt> &#8212; Utilities for running commands<a class="headerlink" href="#module-commands" title="Permalink to this headline">¶</a></h1>
<p><em>Platforms: </em>Unix</p>
<p>The <tt class="xref docutils literal"><span class="pre">commands</span></tt> module contains wrapper functions for <a title="os.popen" class="reference external" href="os.html#os.popen"><tt class="xref 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 title="Subprocess management." class="reference external" href="subprocess.html#module-subprocess"><tt class="xref 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 title="Subprocess management." class="reference external" href="subprocess.html#module-subprocess"><tt class="xref docutils literal"><span class="pre">subprocess</span></tt></a> module is
preferable to using the <tt class="xref docutils literal"><span class="pre">commands</span></tt> module.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In Python 3.x, <a title="commands.getstatus" class="reference internal" href="#commands.getstatus"><tt class="xref docutils literal"><span class="pre">getstatus()</span></tt></a> and two undocumented functions
(<tt class="xref docutils literal"><span class="pre">mk2arg()</span></tt> and <tt class="xref docutils literal"><span class="pre">mkarg()</span></tt>) have been removed.  Also,
<a title="commands.getstatusoutput" class="reference internal" href="#commands.getstatusoutput"><tt class="xref docutils literal"><span class="pre">getstatusoutput()</span></tt></a> and <a title="commands.getoutput" class="reference internal" href="#commands.getoutput"><tt class="xref docutils literal"><span class="pre">getoutput()</span></tt></a> have been moved to the
<a title="Subprocess management." class="reference external" href="subprocess.html#module-subprocess"><tt class="xref docutils literal"><span class="pre">subprocess</span></tt></a> module.</p>
</div>
<p>The <tt class="xref docutils literal"><span class="pre">commands</span></tt> 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>Execute the string <em>cmd</em> in a shell with <a title="os.popen" class="reference external" href="os.html#os.popen"><tt class="xref 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&gt;&amp;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 docutils literal"><span class="pre">wait()</span></tt>.</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>Like <a title="commands.getstatusoutput" class="reference internal" href="#commands.getstatusoutput"><tt class="xref 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&#8217;s output.</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 title="commands.getoutput" class="reference internal" href="#commands.getoutput"><tt class="xref 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 title="commands.getstatusoutput" class="reference internal" href="#commands.getstatusoutput"><tt class="xref 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">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">commands</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">commands</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s">&#39;ls /bin/ls&#39;</span><span class="p">)</span>
<span class="go">(0, &#39;/bin/ls&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">commands</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s">&#39;cat /bin/junk&#39;</span><span class="p">)</span>
<span class="go">(256, &#39;cat: /bin/junk: No such file or directory&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">commands</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s">&#39;/bin/junk&#39;</span><span class="p">)</span>
<span class="go">(256, &#39;sh: /bin/junk: not found&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">commands</span><span class="o">.</span><span class="n">getoutput</span><span class="p">(</span><span class="s">&#39;ls /bin/ls&#39;</span><span class="p">)</span>
<span class="go">&#39;/bin/ls&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">commands</span><span class="o">.</span><span class="n">getstatus</span><span class="p">(</span><span class="s">&#39;/bin/ls&#39;</span><span class="p">)</span>
<span class="go">&#39;-rwxr-xr-x  1 root        13352 Oct 14  1994 /bin/ls&#39;</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 title="Subprocess management." class="reference external" href="subprocess.html#module-subprocess"><tt class="xref 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">36.15. <tt class="docutils literal"><span class="pre">syslog</span></tt> &#8212; Unix syslog library routines</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="mac.html"
                                  title="next chapter">37. Mac OS X specific services</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <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" size="18" />
                <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="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="mac.html" title="37. Mac OS X specific services"
             >next</a> |</li>
        <li class="right" >
          <a href="syslog.html" title="36.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="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="unix.html" >36. Unix Specific Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, 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 Mar 19, 2010.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>