我在Raspberry Pi 3 Model B (OS NOOBS)上有一个Python脚本,该脚本在运行时每分钟将CPU的温度记录到一个.csv文件中。
我想知道的是如何从Python脚本打开终端并在终端中输出温度,而不是将其记录到.csv文件中?
这是我到目前为止编写的代码:
from gpiozero import CPUTemperature
from time import sleep, strftime, time
cpu = CPUTemperature()
def output_temp(temp):
with open("cpu_temp.csv", "a") as log:
log.write("{0}, {1}\n".format(strftime("%Y-%m-%d %H:&M:%S"),str(temp)))
while True:
temp = cpu.temperature
output_temp(temp)
sleep(60)
我正在计划使用我要的东西,因此当温度超过“ x”度时,终端将打开,在此处打印温度,直到温度再次降至“ x”标记以下。但是,我会认为剩下的部分是我自己,因为我是一个仍在学习的新手。但是我被困在想要打开终端并在那里打印变量的部分。
我正在使用软件“ Python 3(IDLE)”来编写和运行此代码。完成后,我将对其进行集成,以便在启动时自动运行。
任何帮助将不胜感激!顺祝商祺
我无法在树莓派上对其进行测试,但是OS NOOBS应该lxterminal
可用,并且以下代码应该可以工作。如果lxterminal
您的系统上没有,请安装它或尝试用下面的代码将其替换为xterm
或gnome-terminal
等。在Ubuntu 16。
import os
import time
import subprocess
# create custom pipe file
PIPE_PATH = "/tmp/my_pipe"
if os.path.exists(PIPE_PATH):
os.remove(PIPE_PATH)
os.mkfifo(PIPE_PATH)
# open terminal that reads from your pipe file
a = subprocess.Popen(['lxterminal', '-e', 'tail --follow {0}'.format(PIPE_PATH)])
# write to file and it will be displayed in the terminal window
message = "some message to terminal\n"
with open(PIPE_PATH, "w") as p:
p.write(message)
time.sleep(5)
# close the terminal
a.terminate()
您可以根据需要调整写入文件并休眠。:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句