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()
How can I control a fan with GPIO on a Raspberry Pi 3 using Python?