你很亲密这两行是有问题的:
temp = os.popen('vcgencmd measure_temp').readline()
if temp > 65:
在这里,temp是一个字符串.您需要先将temp转换为整数,然后再尝试将其与整数进行比较.假设您正在读取的行只是对应于某个温度的十进制字符串,则只需调用int()即可,如下所示:
temp = os.popen('vcgencmd measure_temp').readline()
temp = int(temp)
更新由于您发布了实际上要解析的输出,因此我们可以使用正则表达式将输出与re module匹配.我们还将其放在函数中:
def measure_temp():
raw = os.popen('vcgencmd measure_temp').readline()
m = re.match("temp=(\d+\.?\d*)'C", raw)
if not m:
raise ValueError("Unexpected temperature string: " + raw)
return float(m.group(1))
temp = measure_temp()
请注意,我在字符串中的实际温度小数点附近使用了一个捕获组,并使用m.group(1)对其进行了访问.
让我们现在放在一起.另外,当您的代码未达到预期的效果时,包含一些“调试打印”非常有帮助,例如:
def measure_temp():
raw = os.popen('vcgencmd measure_temp').readli