Python脚本打开并写入终端
我的Raspberry Pi 3 Model B(OS NOOBS)上有一个Python脚本,在运行时,每分钟将CPU的温度记录到一个.csv文件中 我想知道的是,如何从Python脚本打开终端并在终端中输出温度,而不是将其记录到.csv文件中 这是我到目前为止编写的代码:Python脚本打开并写入终端,python,linux,terminal,output,raspberry-pi3,Python,Linux,Terminal,Output,Raspberry Pi3,我的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_t
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”标记以下。不过,我会自己计算剩下的部分,因为我是一个仍在学习的新手。但是我被困在我想要打开终端并在那里打印变量的地方
我正在使用软件“Python3(IDLE)”来编写和运行这段代码。当它完成后,我将集成它,以便它在启动时自动运行
任何帮助都将不胜感激!
真诚地
Jocke我不能在raspberry上测试它,但是OS NOOBS应该有可用的
lxterminal
,下面的代码应该可以工作。如果您的系统上没有lxterm
,请安装它,或者尝试在下面的代码中用Ubuntu 16上测试的xterm
或gnome-terminal
等替换它
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()
您可以根据需要调整写入文件和睡眠。:) 看看这是否有用非常感谢!这个解决方案是可以理解的,而且有效,我非常感谢您的帮助!:)