与返回 stdout 内容的 subprocess.check_call 等效的好方法是什么?

发布于 2024-09-03 09:39:03 字数 192 浏览 1 评论 0原文

我想要一个与 subprocess.check_call 接口相匹配的好方法 - 即,它在失败时抛出 CalledProcessError ,是同步的,&c - 但相反返回命令的返回代码(如果它确实这样做)返回程序的输出,要么仅是 stdout,要么是 (stdout, stderr) 的元组。

有人有这样做的方法吗?

I'd like a good method that matches the interface of subprocess.check_call -- ie, it throws CalledProcessError when it fails, is synchronous, &c -- but instead of returning the return code of the command (if it even does that) returns the program's output, either only stdout, or a tuple of (stdout, stderr).

Does somebody have a method that does this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵强ㄟ 2024-09-10 09:39:03

Python 2.7+

from subprocess import check_output as qx

Python < 2.7

来自 subprocess.py

import subprocess
def check_output(*popenargs, **kwargs):
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        raise subprocess.CalledProcessError(retcode, cmd, output=output)
    return output

class CalledProcessError(Exception):
    def __init__(self, returncode, cmd, output=None):
        self.returncode = returncode
        self.cmd = cmd
        self.output = output
    def __str__(self):
        return "Command '%s' returned non-zero exit status %d" % (
            self.cmd, self.returncode)
# overwrite CalledProcessError due to `output` keyword might be not available
subprocess.CalledProcessError = CalledProcessError

另请参阅 将系统命令输出捕获为字符串,作为可能的另一个示例 check_output() 实现。

Python 2.7+

from subprocess import check_output as qx

Python < 2.7

From subprocess.py:

import subprocess
def check_output(*popenargs, **kwargs):
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        raise subprocess.CalledProcessError(retcode, cmd, output=output)
    return output

class CalledProcessError(Exception):
    def __init__(self, returncode, cmd, output=None):
        self.returncode = returncode
        self.cmd = cmd
        self.output = output
    def __str__(self):
        return "Command '%s' returned non-zero exit status %d" % (
            self.cmd, self.returncode)
# overwrite CalledProcessError due to `output` keyword might be not available
subprocess.CalledProcessError = CalledProcessError

See also Capturing system command output as a string for another example of possible check_output() implementation.

明天过后 2024-09-10 09:39:03

我无法在评论中获取格式,所以这是对 JF Sebastian 的回答

我发现这非常有帮助,所以我想我会添加到这个答案中。我希望能够在代码中无缝工作而无需检查版本。这就是我所做的...

我将上面的代码放入一个名为“subprocess_compat.py”的文件中。然后在我使用子进程的代码中我做了。

import sys
if sys.version_info < (2, 7):
    import subprocess_compat
    subprocess.check_output = subprocess_compat.check_output

现在,在代码中的任何位置,我都可以使用我想要的参数调用“subprocess.check_output”,并且无论我使用哪个版本的 python,它都可以工作。

I can not get formatting in a comment, so this is in response to J.F. Sebastian's answer

I found this very helpful so I figured I would add to this answer. I wanted to be able to work seamlessly in the code without checking the version. This is what I did...

I put the code above into a file called 'subprocess_compat.py'. Then in the code where I use subprocess I did.

import sys
if sys.version_info < (2, 7):
    import subprocess_compat
    subprocess.check_output = subprocess_compat.check_output

Now anywhere in the code I can just call 'subprocess.check_output' with the params I want and it will work regardless of which version of python I am using.

东京女 2024-09-10 09:39:03

在我读了两遍之后,我意识到它已经有十年的历史了,并且大多数答案适用于现已弃用的 python2.7 而不是 python3。

现在我们正在 - 或者应该 - 在 python3 上,似乎 python >= 3.7 的最佳选择是使用多个评论中提到的以下内容:

result = subprocess.run(..., check=True, capture_output=True)

为了节省您搜索更多详细信息,我推荐我找到的答案SethMMorton中提供了精彩的细节回答“如何抑制或捕获 subprocess.run() 的输出?” 正如那里所述,您可以直接访问 stdout、stderr:

print(result.stdout)
print(result.stderr)

如果您需要支持 Python 3.6:

但是,您可以通过将 stdoutstderr 设置为 PIPE 来轻松“模拟”这一点:

从子进程导入 PIPE

subprocess.run(["ls", "-l", "/dev/null"], stdout=PIPE, stderr=PIPE)

此信息来自
威廉·范·昂塞姆相关问题的回答

我倾向于直接访问 https://docs.python.org/3/library/ subprocess.html 来刷新我对一般子流程事物的记忆。 (不过,这些示例通常对我来说更容易快速访问。)

After I read this twice, I realized it's ten years old and most answers apply to the now deprecated python2.7 rather than python3.

Now that we are - or should be - on python3, it seems that the best option for python >= 3.7 is to use the following as is mentioned in multiple comments:

result = subprocess.run(..., check=True, capture_output=True)

To save you searching for more details, I recommend an answer I found with wonderful detail by SethMMorton in an answer to "How to suppress or capture the output of subprocess.run()?" As described there, you can access stdout, stderr directly as:

print(result.stdout)
print(result.stderr)

If you need to support Python 3.6:

You can however easily "emulate" this by setting both stdout and stderr to PIPE:

from subprocess import PIPE

subprocess.run(["ls", "-l", "/dev/null"], stdout=PIPE, stderr=PIPE)

This info is from
Willem Van Onsem's answer to a related question.

I tend to go straight to https://docs.python.org/3/library/subprocess.html to refresh my memory on general subprocess things. (The SO examples are often easier for me to access quickly though.)

情深如许 2024-09-10 09:39:03

该函数以字符串列表的形式返回终端输出。

import subprocess,sys
def output(cmd):
    cmd_l = cmd.split()
    output = subprocess.Popen(cmd_l, stdout=subprocess.PIPE).communicate()[0]
    output = output.decode("utf-8")
    return (output)

This function returns terminal output in the form of list of string.

import subprocess,sys
def output(cmd):
    cmd_l = cmd.split()
    output = subprocess.Popen(cmd_l, stdout=subprocess.PIPE).communicate()[0]
    output = output.decode("utf-8")
    return (output)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文