简体   繁体   English

如何在变量中存储os.system()的输出

[英]How to store output of os.system() in a variable

I wrote a small code: 我写了一个小代码:

import os
os.system('users')
os.system('w')

This prints 这打印

ubuntu
 09:27:25 up 9 days, 21:23,  1 user,  load average: 0.00, 0.00, 0.00
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

But when i try : 但当我尝试:

import os
from pyslack import SlackClient

user_name = os.system('users')
login_details = os.system('w')

print user_name
print login_details

It has the following output: 它有以下输出:

ubuntu
 09:28:32 up 9 days, 21:24,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ubuntu   pts/0    42.99.164.66     09:06    0.00s  0.11s  0.00s w
0
0

Now i am not sure why i am not able to store the result in the varible , ie why is it printing 0 ? 现在我不知道为什么我无法将结果存储在变量中,即为什么打印0? And what should be the correct way to get rid of it? 什么应该是摆脱它的正确方法?

The value returned by the os.system is identical to the return value of the command you launched. os.system返回的值与您启动的命令的返回值相同。 Since most calls, like 'users' are written in C, they return 0 when the code is executed successfully ( they have a return 0; at the end of the main() ). 由于大多数调用(如“用户”)都是用C语言编写的,因此当代码执行成功时它们返回0(它们return 0;main()的末尾)。

If you want to save their output, you can redirect their output path (by default stdout ) to a text file, then read the text file. 如果要保存输出,可以将其输出路径(默认情况下为stdout )重定向到文本文件,然后读取文本文件。

user_name = os.system('users > users.txt')
login_details = os.system('w > w.txt')

with open("users.txt", "r") as f:
    for line in f:
        print line
with open("w.txt", "r") as f:
    for line in f:
        print line

os.system("rm users.txt")
os.system("rm w.txt")

I bow to the subprocess.check_output solution 我向subprocess.check_output解决方案低头

From the os.system(command) . os.system(命令)

os.system just execute the command (a string) in a subshell. os.system只是在子shell中执行命令(字符串)。

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

Which means the above data is the output written to stdout by calling the Standard C function system() not the return value . 这意味着上面的数据是通过调用标准C函数system()而不是返回值写入stdout的输出。

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). 在Unix上, 返回值是以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. 请注意,POSIX未指定C system()函数的返回值的含义,因此Python函数的返回值取决于系统。

On Windows, the return value 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; 在Windows上, 返回值是系统shell在运行命令后返回的值 ,由Windows环境变量COMSPEC给出:在command.com系统上(Windows 95,98和ME),它始终为0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; 在cmd.exe系统(Windows NT,2000和XP)上,这是命令运行的退出状态; on systems using a non-native shell, consult your shell documentation. 在使用非本机shell的系统上,请参阅shell文档。

So if the exit status is success, user_name and login_details will get a zero. 因此,如果退出状态为success,则user_namelogin_details将为零。

As a matter of fact, you can try this:; 事实上,你可以试试这个:

import subprocess
user = subprocess.check_output(['users'])
details = subprocess.check_output(['w'])

print(user)
print(details)

The os.system return the exit code of the command. os.system返回命令的退出代码。

To capture the output of the command, you can use subprocess.check_output 要捕获命令的输出,可以使用subprocess.check_output

output = subprocess.check_output('users', shell=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM