<p>从<a href="https://docs.python.org/2/library/os.html#os.system" rel="nofollow noreferrer">os.system(command)</a>开始。</p>
<p><code>os.system</code>只需在子shell中执行命令(字符串)。</p>
<pre><code>USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
ubuntu pts/0 42.99.164.66 09:06 5.00s 0.10s 0.00s sh -c w
</code></pre>
<p>这意味着上述数据是通过调用标准C函数<code>system()</code>而不是返回值</strong>写入标准输出的。</p>
<blockquote>
<p>On Unix, the <strong>return value</strong> is the <strong>exit status</strong> of the process encoded in
the format specified for wait(). Note that POSIX does not specify the
meaning of the return value of the C system() function, so the return
value of the Python function is system-dependent.</p>
<p>On Windows, the <strong>return value</strong> is that returned by the system shell
after running command, given by the Windows environment variable
COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always
0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit
status of the command run; on systems using a non-native shell,
consult your shell documentation.</p>
</blockquote>
<p>所以如果退出状态是成功,<code>user_name</code>和<code>login_details</code>将得到一个零。</p>
<p>事实上,你可以试试这个:</p>
<pre><code>import subprocess
user = subprocess.check_output(['users'])
details = subprocess.check_output(['w'])
print(user)
print(details)
</code></pre>