問題描述
我制作了一個腳本,告訴我 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 的輸出的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!