gpt4 book ai didi

python - 子进程.CalledProcessError : what *is* the error?

转载 作者:行者123 更新时间:2023-11-28 20:03:44 25 4
gpt4 key购买 nike

import subprocess

cmd = "grep -r * | grep jquery"
print cmd

subprocess.check_output(cmd, shell=True)

subprocess.CalledProcessError: 命令 'grep -r * | grep jquery' 返回非零退出状态 1

我可以在我的 shell 中毫无问题地执行该命令。如何查看 python 中的实际错误?

这个片段是一个更大的脚本的一部分,它接受多个参数并链接 grep 命令 + 添加一些排除项(我不需要 grep 日志文件或缩小的 JavaScript。因此是迂腐的语法。

Python 2.7.10

最佳答案

Quoting docs :

If the return code was non-zero it raises a CalledProcessError. TheCalledProcessError object will have the return code in the returncodeattribute and any output in the output attribute.

import subprocess

cmd = "grep -r * | grep jquery"
print cmd

try:
subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError as e:
print e.returncode
print e.output

错误消息被打印到 stderr,你需要关键字参数 stderr=subprocess.STDOUT

除了返回码和 stdout/stderr 之外,没有其他“出了什么问题”的来源。

关于python - 子进程.CalledProcessError : what *is* the error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39563802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com