检查 CalledProcessError 的输出 [英] check output from CalledProcessError

查看:15
本文介绍了检查 CalledProcessError 的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 子进程模块中的 subprocess.check_output 来执行 ping 命令.这是我的做法:

I am using subprocess.check_output from pythons subprocess module to execute a ping command. Here is how I am doing it:

output = subprocess.check_output(["ping","-c 2 -W 2","1.1.1.1")

它正在引发一个 CalledProcessError 并说输出是函数的参数之一.任何人都可以帮助我如何阅读该输出.我想将输出读入一个字符串并解析它.例如,如果 ping 返回

It is raising a CalledProcessError and says the output is one of the arguments of the function. Can anyone help me how to read that output. I would like to read the output into a string and parse it. So say for example if the ping returns

100% 丢包

我需要捕捉它.如果有其他更好的方法..请提出建议.谢谢.

I need to capture that. If there is any other better way..please suggest. Thanks.

推荐答案

在参数列表中,每个条目都必须单独存在.使用

In the list of arguments, each entry must be on its own. Using

output = subprocess.check_output(["ping", "-c","2", "-W","2", "1.1.1.1"])

应该可以解决您的问题.

should fix your problem.

这篇关于检查 CalledProcessError 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆