<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>8.14 commands --
Utilities for running commands</TITLE>
<META NAME="description" CONTENT="8.14 commands --
Utilities for running commands">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="previous" HREF="module-popen2.html">
<LINK REL="up" HREF="unix.html">
<LINK REL="next" HREF="module-pdb.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-pdb.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="unix.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="popen3-objects.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-pdb.html">9. The Python Debugger</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="unix.html">8. Unix Specific Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="popen3-objects.html">8.13.1 Popen3 Objects</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION00101400000000000000000">
8.14 <tt class=module>commands</tt> --
         Utilities for running commands</A>
</H1>

<P>
  
<p class=availability>Availability: <span
 class=platform>Unix</span>.</p>

<P>
The <tt class=module>commands</tt> module contains wrapper functions for
<tt class=function>os.popen()</tt> which take a system command as a string and
return any output generated by the command and, optionally, the exit
status.

<P>
The <tt class=module>commands</tt> module defines the following functions:

<P>
<dl><dt><b><a name="l2h-1544"><tt class=function>getstatusoutput</tt></a></b> (<var>cmd</var>)
<dd>
Execute the string <var>cmd</var> in a shell with <tt class=function>os.popen()</tt> and
return a 2-tuple <tt>(<var>status</var>, <var>output</var>)</tt>.  <var>cmd</var> is
actually run as <tt>{ <var>cmd</var> ; } 2&gt;&amp;1</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=cfunction>wait()</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-1545"><tt class=function>getoutput</tt></a></b> (<var>cmd</var>)
<dd>
Like <tt class=function>getstatusoutput()</tt>, except the exit status is ignored
and the return value is a string containing the command's output.  
</dl>

<P>
<dl><dt><b><a name="l2h-1546"><tt class=function>getstatus</tt></a></b> (<var>file</var>)
<dd>
Return the output of "<tt class=samp>ls -ld <var>file</var></tt>" as a string.  This
function uses the <tt class=function>getoutput()</tt> function, and properly
escapes backslashes and dollar signs in the argument.
</dl>

<P>
Example:

<P>
<dl><dd><pre class=verbatim>
&gt;&gt;&gt; import commands
&gt;&gt;&gt; commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
&gt;&gt;&gt; commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
&gt;&gt;&gt; commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
&gt;&gt;&gt; commands.getoutput('ls /bin/ls')
'/bin/ls'
&gt;&gt;&gt; commands.getstatus('/bin/ls')
'-rwxr-xr-x  1 root        13352 Oct 14  1994 /bin/ls'
</pre></dl>

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-pdb.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="unix.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="popen3-objects.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-pdb.html">9. The Python Debugger</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="unix.html">8. Unix Specific Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="popen3-objects.html">8.13.1 Popen3 Objects</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
