Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="module-syslog.html" /> | |
13 | <link rel="parent" href="unix.html" /> | |
14 | <link rel="next" href="module-pdb.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>8.17 commands -- Utilities for running commands</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="8.16 syslog " | |
24 | href="module-syslog.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="8. Unix Specific Services" | |
27 | href="unix.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="9. The Python Debugger" | |
30 | href="module-pdb.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="module-syslog.html">8.16 syslog </A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="unix.html">8. Unix Specific Services</A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-pdb.html">9. The Python Debugger</A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H1><A NAME="SECTION00101700000000000000000"> | |
55 | 8.17 <tt class="module">commands</tt> -- | |
56 | Utilities for running commands</A> | |
57 | </H1> | |
58 | ||
59 | <P> | |
60 | <A NAME="module-commands"></A> | |
61 | <p class="availability">Availability: <span | |
62 | class="platform">Unix</span>.</p> | |
63 | ||
64 | <P> | |
65 | The <tt class="module">commands</tt> module contains wrapper functions for | |
66 | <tt class="function">os.popen()</tt> which take a system command as a string and | |
67 | return any output generated by the command and, optionally, the exit | |
68 | status. | |
69 | ||
70 | <P> | |
71 | The <tt class="module">commands</tt> module defines the following functions: | |
72 | ||
73 | <P> | |
74 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
75 | <td><nobr><b><tt id='l2h-3087' xml:id='l2h-3087' class="function">getstatusoutput</tt></b>(</nobr></td> | |
76 | <td><var>cmd</var>)</td></tr></table></dt> | |
77 | <dd> | |
78 | Execute the string <var>cmd</var> in a shell with <tt class="function">os.popen()</tt> and | |
79 | return a 2-tuple <code>(<var>status</var>, <var>output</var>)</code>. <var>cmd</var> is | |
80 | actually run as <code>{ <var>cmd</var> ; } 2>&1</code>, so that the returned | |
81 | output will contain output or error messages. A trailing newline is | |
82 | stripped from the output. The exit status for the command can be | |
83 | interpreted according to the rules for the C function | |
84 | <tt class="cfunction">wait()</tt>. | |
85 | </dl> | |
86 | ||
87 | <P> | |
88 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
89 | <td><nobr><b><tt id='l2h-3088' xml:id='l2h-3088' class="function">getoutput</tt></b>(</nobr></td> | |
90 | <td><var>cmd</var>)</td></tr></table></dt> | |
91 | <dd> | |
92 | Like <tt class="function">getstatusoutput()</tt>, except the exit status is ignored | |
93 | and the return value is a string containing the command's output. | |
94 | </dl> | |
95 | ||
96 | <P> | |
97 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
98 | <td><nobr><b><tt id='l2h-3089' xml:id='l2h-3089' class="function">getstatus</tt></b>(</nobr></td> | |
99 | <td><var>file</var>)</td></tr></table></dt> | |
100 | <dd> | |
101 | Return the output of "<tt class="samp">ls -ld <var>file</var></tt>" as a string. This | |
102 | function uses the <tt class="function">getoutput()</tt> function, and properly | |
103 | escapes backslashes and dollar signs in the argument. | |
104 | </dl> | |
105 | ||
106 | <P> | |
107 | Example: | |
108 | ||
109 | <P> | |
110 | <div class="verbatim"><pre> | |
111 | >>> import commands | |
112 | >>> commands.getstatusoutput('ls /bin/ls') | |
113 | (0, '/bin/ls') | |
114 | >>> commands.getstatusoutput('cat /bin/junk') | |
115 | (256, 'cat: /bin/junk: No such file or directory') | |
116 | >>> commands.getstatusoutput('/bin/junk') | |
117 | (256, 'sh: /bin/junk: not found') | |
118 | >>> commands.getoutput('ls /bin/ls') | |
119 | '/bin/ls' | |
120 | >>> commands.getstatus('/bin/ls') | |
121 | '-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls' | |
122 | </pre></div> | |
123 | ||
124 | <P> | |
125 | ||
126 | <DIV CLASS="navigation"> | |
127 | <div class='online-navigation'> | |
128 | <p></p><hr /> | |
129 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
130 | <tr> | |
131 | <td class='online-navigation'><a rel="prev" title="8.16 syslog " | |
132 | href="module-syslog.html"><img src='../icons/previous.png' | |
133 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
134 | <td class='online-navigation'><a rel="parent" title="8. Unix Specific Services" | |
135 | href="unix.html"><img src='../icons/up.png' | |
136 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
137 | <td class='online-navigation'><a rel="next" title="9. The Python Debugger" | |
138 | href="module-pdb.html"><img src='../icons/next.png' | |
139 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
140 | <td align="center" width="100%">Python Library Reference</td> | |
141 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
142 | href="contents.html"><img src='../icons/contents.png' | |
143 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
144 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
145 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
146 | <td class='online-navigation'><a rel="index" title="Index" | |
147 | href="genindex.html"><img src='../icons/index.png' | |
148 | border='0' height='32' alt='Index' width='32' /></A></td> | |
149 | </tr></table> | |
150 | <div class='online-navigation'> | |
151 | <b class="navlabel">Previous:</b> | |
152 | <a class="sectref" rel="prev" href="module-syslog.html">8.16 syslog </A> | |
153 | <b class="navlabel">Up:</b> | |
154 | <a class="sectref" rel="parent" href="unix.html">8. Unix Specific Services</A> | |
155 | <b class="navlabel">Next:</b> | |
156 | <a class="sectref" rel="next" href="module-pdb.html">9. The Python Debugger</A> | |
157 | </div> | |
158 | </div> | |
159 | <hr /> | |
160 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
161 | </DIV> | |
162 | <!--End of Navigation Panel--> | |
163 | <ADDRESS> | |
164 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
165 | </ADDRESS> | |
166 | </BODY> | |
167 | </HTML> |