python os.system保存返回值_Python中os.system()的返回值是多少?

os.system('command')返回一个16位数字,从左开始的前8位(lsb)表示操作系统用来关闭命令的信号,接下来的8位表示命令的返回代码。00000000 00000000

exit code signal num

示例1-命令退出,代码为1os.system('command') #it returns 256

256 in 16 bits - 00000001 00000000

Exit code is 00000001 which means 1

示例2-命令退出,代码为3os.system('command') # it returns 768

768 in 16 bits - 00000011 00000000

Exit code is 00000011 which means 3

现在试试信号-

示例3-编写一个长时间睡眠的程序,在os.system()中将其用作命令,然后通过kill-15或kill-9终止它os.system('command') #it returns signal num by which it is killed

15 in bits - 00000000 00001111

Signal num is 00001111 which means 15

可以将python程序设置为command='python command.py'import sys

sys.exit(n) # here n would be exit code

对于c或c++程序,可以使用return from main()或exit(n)from any function#

注意-这适用于unixOn 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.

os.wait()

Wait for completion of a child process, and return a tuple containing its pid and exit status indication: a 16-bit number, whose

low byte is the signal number that killed the process, and whose high

byte is the exit status (if the signal number is zero); the high bit

of the low byte is set if a core file was produced.Availability: Unix

是的。

你可能感兴趣的:(python,os.system保存返回值)