subprocess.check_output 和subprocess.Popen区别

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.

参考地址

https://docs.python.org/2/library/subprocess.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值