Project

General

Profile

Code snippets

Getting CPU temperature in Python

Example 1, reading from vcgencmd output (probably very ineffective way):

import os

cpu_t_str = os.popen('vcgencmd measure_temp').readline()
cpu_t = float(cpu_t_str.replace("temp=","").replace("'C\n",""))

print ('{:0.2f}'.format(cpu_t))

Example 2, reading directly from /sys filesystem (probably the most effective way):

cat /sys/class/thermal/thermal_zone0/temp

This gives CPU temperature in mili-Celciuses.