from threading import Thread
import time
class MessageThread(Thread):
def run(self):
while True:
time.sleep(60)
ReadTemp()
def ReadTemp():
file = open("/sys/class/thermal/thermal_zone0/temp")
# 读取结果,并转换为浮点数
s = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 获取格式化的当前时间
temp = float(file.read()) / 1000
log_file = open('/home/pi/Desktop/temperature/temp_log',"a+")
log_file.write(s + ' ' + str(temp) + '\n')
# 关闭文件
log_file.close()
file.close()
if __name__ == "__main__":
readTemp = MessageThread()
readTemp.start()