⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module-commands.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>8.18 commands -- Utilities for running commands</title>
<META NAME="description" CONTENT="8.18 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" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="previous" href="module-syslog.html" tppabs="http://www.python.org/doc/current/lib/module-syslog.html">
<LINK REL="up" href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html">
<LINK REL="next" href="module-pdb.html" tppabs="http://www.python.org/doc/current/lib/module-pdb.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-syslog.html" tppabs="http://www.python.org/doc/current/lib/module-syslog.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="module-pdb.html" tppabs="http://www.python.org/doc/current/lib/module-pdb.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-syslog.html" tppabs="http://www.python.org/doc/current/lib/module-syslog.html">8.17 syslog  </A>
<b class="navlabel">Up:</b> <a class="sectref" href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html">8. Unix Specific Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-pdb.html" tppabs="http://www.python.org/doc/current/lib/module-pdb.html">9. The Python Debugger</A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION00101800000000000000000">
8.18 <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-1933'><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 <code>(<var>status</var>, <var>output</var>)</code>.  <var>cmd</var> is
actually run as <code>{ <var>cmd</var> ; } 2&gt;&amp;1</code>, 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-1934'><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-1935'><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>

<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-syslog.html" tppabs="http://www.python.org/doc/current/lib/module-syslog.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="module-pdb.html" tppabs="http://www.python.org/doc/current/lib/module-pdb.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-syslog.html" tppabs="http://www.python.org/doc/current/lib/module-syslog.html">8.17 syslog  </A>
<b class="navlabel">Up:</b> <a class="sectref" href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html">8. Unix Specific Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-pdb.html" tppabs="http://www.python.org/doc/current/lib/module-pdb.html">9. The Python Debugger</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -