How to capture output of subprocess.call(如何捕获 subprocess.call 的输出)
问题描述
我制作了一个脚本,告诉我 Raspberry Pi 3 的温度,但脚本有问题.结果输出是机器人说您的 RPI3 温度当前为 0".我的代码有什么问题?
I have made a script that tells me the temperature of my Raspberry Pi 3, but there is a problem with the script. The result output is the bot saying "Your RPI3 temp is currently 0". What is wrong with my code?
@bot.command(pass_context=True)
async def vcgencmdmeasure_temp(ctx):
if ctx.message.author.id == "412372079242117123":
await bot.say("OK....")
return_code = subprocess.call("vcgencmd measure_temp", shell=True)
await bot.say("KK done")
await bot.say("Your RPI3 temp is currently: {}".format(return_code))
else:
await bot.say("Error user lacks perms(only bot owner can run this)")
我知道想要运行任何命令.当前脚本
i know want to run any command. Current script
@bot.command(pass_context=True)async def rpicmd(ctx, *args):
@bot.command(pass_context=True) async def rpicmd(ctx, *args):
if ctx.message.author.id == "412372079242117123":
mesg = ''.join(args)
mesg = str(mesg)
command_output = subprocess.check_output(mesg, shell=True, universal_newlines=True)
await bot.say(command_output)
else:
await bot.say("No noob")
我得到了错误:
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an
exception: CalledProcessError: Command 'vcgencmdmeasure_temp' returned
non-zero exit status 12
推荐答案
return_code 会有进程的返回码.当一个进程成功存在(没有错误)时,它返回一个代码0.如果出错,则返回 1 代码(或非零值).如果你想要程序的输出(它打印到 stdout),这是获得它的一种方法:
return_code will have the return code of the process. When a process exists successfully (without error), it returns a code of 0. If it errors, it returns a code of 1 (or something non-zero). If you want the output of the program (that it prints to stdout), this is one way to get it:
p = subprocess.run("vcgencmd measure_temp", shell=True,stdout=subprocess.PIPE)
result = p.stdout.decode()
await bot.say("Your RPI3 temp is currently: {}".format(result))
这篇关于如何捕获 subprocess.call 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何捕获 subprocess.call 的输出
基础教程推荐
- 包装空间模型 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
