check_output 和popen大体上是一样的,除了,check_output无法使用stdout ,因为stdout 在内部使用了.
参考原文
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
Run command with arguments and return its output as a byte string.
If the return code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and any output in the output attribute.
The arguments shown above are merely the most common ones, described below in Frequently Used Arguments (hence the slightly odd notation in the abbreviated signature). The full function signature is largely the same as that of the Popen constructor, except that stdout is not permitted as it is used internally. All other supplied arguments are passed directly through to the Popen constructor.
Examples:
subprocess.check_output([“echo”, “Hello World!”])
‘Hello World!\n’subprocess.check_output(“exit 1”, shell=True)
Traceback (most recent call last):
…
subprocess.CalledProcessError: Command ‘exit 1’ returned non-zero exit status 1
To also capture standard error in the result, use stderr=subprocess.STDOUT:subprocess.check_output(
… “ls non_existent_file; exit 0”,
… stderr=subprocess.STDOUT,
… shell=True)
‘ls: non_existent_file: No such file or directory\n’
New in version 2.7.Warning :Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.
Note :Do not use stderr=PIPE with this function as that can deadlock based on the child process error volume. Use Popen with the communicate() method when you need a stderr pipe.
参考地址