如何捕获 subprocess.call 的输出
How to capture output of subprocess.call
我已经制作了一个脚本来告诉我 Raspberry Pi 3 的温度,但是脚本有问题。结果输出是机器人说 "Your RPI3 temp is currently 0"。我的代码有什么问题?
@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)")
编辑:我知道想要运行任何命令。
当前脚本
@bot.command(pass_context=真)
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
您应该使用 subprocess.check_output
从您的命令中获取响应。
来自文档:
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.
使用call
给出返回码:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None,
shell=False)
Run the command described by args. Wait for command to
complete, then return the returncode attribute.
return_code
将具有进程的 return 代码。当一个进程成功存在(没有错误)时,它 return 是 0
的代码。如果它出错,它 return 是 1
的代码(或非零的东西)。如果您想要程序的输出(它打印到 stdout
),这是获取它的一种方法:
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))
我已经制作了一个脚本来告诉我 Raspberry Pi 3 的温度,但是脚本有问题。结果输出是机器人说 "Your RPI3 temp is currently 0"。我的代码有什么问题?
@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)")
编辑:我知道想要运行任何命令。 当前脚本
@bot.command(pass_context=真) 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
您应该使用 subprocess.check_output
从您的命令中获取响应。
来自文档:
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.
使用call
给出返回码:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
Run the command described by args. Wait for command to complete, then return the returncode attribute.
return_code
将具有进程的 return 代码。当一个进程成功存在(没有错误)时,它 return 是 0
的代码。如果它出错,它 return 是 1
的代码(或非零的东西)。如果您想要程序的输出(它打印到 stdout
),这是获取它的一种方法:
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))