x = os.system(.. .)的返回值[重复]

时间:2022-05-30 22:43:04

This question already has an answer here:

这个问题已经有了答案:

When I type os.system("whoami") in Python, as root, it returns root, but when I try to assign it to a variable x = os.system("whoami") it set's the value of x to 0. Why ? (:

当我在Python中键入os.system(“whoami”)时,作为根,它返回root,但是当我尝试将它赋值给一个变量x = os.system(“whoami”)时,它将x的值设为0。为什么?(:

2 个解决方案

#1


23  

os.system() returns the (encoded) process exit value. 0 means success:

system()返回(编码)进程退出值。0意味着成功:

On Unix, the return value is the exit status 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.

在Unix上,返回值是以wait()指定的格式编码的进程的退出状态。注意,POSIX没有指定C system()函数的返回值的含义,因此Python函数的返回值是与系统相关的。

The output you see is written to stdout, so your console or terminal, and not returned to the Python caller.

您看到的输出被写入stdout,因此您的控制台或终端,而没有返回到Python调用者。

If you wanted to capture stdout, use subprocess.check_output() instead:

如果您想捕获stdout,可以使用subprocess.check_output()代替:

x = subprocess.check_output(['whoami'])

#2


3  

os.system('command') returns a 16 bit number, which first 8 bits from left(lsb) talks about signal used by os to close the command, Next 8 bits talks about return code of command.

system('command')返回一个16位数字,左前8位(lsb)表示os用来关闭命令的信号,下8位表示命令的返回代码。

Refer my answer for more detail in What is the return value of os.system() in Python?

请参阅我的答案,了解Python中的os.system()的返回值是什么?

#1


23  

os.system() returns the (encoded) process exit value. 0 means success:

system()返回(编码)进程退出值。0意味着成功:

On Unix, the return value is the exit status 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.

在Unix上,返回值是以wait()指定的格式编码的进程的退出状态。注意,POSIX没有指定C system()函数的返回值的含义,因此Python函数的返回值是与系统相关的。

The output you see is written to stdout, so your console or terminal, and not returned to the Python caller.

您看到的输出被写入stdout,因此您的控制台或终端,而没有返回到Python调用者。

If you wanted to capture stdout, use subprocess.check_output() instead:

如果您想捕获stdout,可以使用subprocess.check_output()代替:

x = subprocess.check_output(['whoami'])

#2


3  

os.system('command') returns a 16 bit number, which first 8 bits from left(lsb) talks about signal used by os to close the command, Next 8 bits talks about return code of command.

system('command')返回一个16位数字,左前8位(lsb)表示os用来关闭命令的信号,下8位表示命令的返回代码。

Refer my answer for more detail in What is the return value of os.system() in Python?

请参阅我的答案,了解Python中的os.system()的返回值是什么?