blob: fe6f9293454c922ad0a64cffcf1f8a47bf192230 [file] [log] [blame]
Fred Drake4fd12921997-06-12 16:05:46 +00001\section{Standard module \sectcode{commands}} % If implemented in Python
Guido van Rossum1c78cf31997-07-18 21:07:34 +00002\label{module-commands}
Fred Drake4fd12921997-06-12 16:05:46 +00003\stmodindex{commands}
4
5The \code{commands} module contains wrapper functions for \code{os.popen()}
6which take a system command as a string and return any output generated by
7the command, and optionally, the exit status.
8
9The \code{commands} module is only usable on systems which support
10\code{popen()} (currently \UNIX{}).
11
12The \code{commands} module defines the following functions:
13
14\renewcommand{\indexsubitem}{(in module commands)}
15\begin{funcdesc}{getstatusoutput}{cmd}
16Execute the string \var{cmd} in a shell with \code{os.popen()} and return
17a 2-tuple (status, output). \var{cmd} is actually run as
Fred Drake8f81ef11997-06-12 16:14:07 +000018\samp{\{\ cmd \} ; 2$>$\$1}, so that the returned output will contain output
Fred Drake4fd12921997-06-12 16:05:46 +000019or error messages. A trailing newline is stripped from the output.
20The exit status for the command can be interpreted according to the
21rules for the \C{} function \code{wait()}.
22\end{funcdesc}
23
24\begin{funcdesc}{getoutput}{cmd}
25Like \code{getstatusoutput()}, except the exit status is ignored and
26the return value is a string containing the command's output.
27\end{funcdesc}
28
29\begin{funcdesc}{getstatus}{file}
30Return the output of \samp{ls -ld \var{file}} as a string. This
31function uses the \code{getoutput()} function, and properly escapes
32backslashes and dollar signs in the argument.
33\end{funcdesc}
34
35Example:
36
37\begin{verbatim}
38>>> import commands
39>>> commands.getstatusoutput('ls /bin/ls')
40(0, '/bin/ls')
41>>> commands.getstatusoutput('cat /bin/junk')
42(256, 'cat: /bin/junk: No such file or directory')
43>>> commands.getstatusoutput('/bin/junk')
44(256, 'sh: /bin/junk: not found')
45>>> commands.getoutput('ls /bin/ls')
46'/bin/ls'
47>>> commands.getstatus('/bin/ls')
48'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
49\end{verbatim}