Run command with arguments and return its output.
运行带参数的命令并返回其输出。
If the exit code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and output in the output attribute.
如果退出码非零,则引发CalledProcessError。
CalledProcessError对象将在returncode属性中具有返回代码,并在output属性中具有输出。
The arguments are the same as for the Popen constructor. Example: 参数与Popen构造函数的参数相同。例子:
import subprocess
a = subprocess.check_output(["ls", "-l", "/dev/null"])
print(a)
b = subprocess.check_output(["ls", "/dev/null"])
print(b)
b'crw-rw-rw- 1 root root 1, 3 1\xe6\x9c\x88 29 17:19 /dev/null\n'
b'/dev/null\n'
打印当前工作目录:
p = subprocess.check_output("pwd")
print(p)