Skip to content

Instantly share code, notes, and snippets.

@SyNeto
Created November 17, 2021 18:49
Show Gist options
  • Save SyNeto/2d78e391775bf7c1c882ab0196152337 to your computer and use it in GitHub Desktop.
Save SyNeto/2d78e391775bf7c1c882ab0196152337 to your computer and use it in GitHub Desktop.
Obtains current CPU temperature an prints it.
#!/usr/bin/env python3
import os
def main() -> None:
"""
For Raspbian OS:
Obtains current CPU temperature an prints it.
"""
print(f'CPU temp: {get_cpu_temp():.2f} *C')
def get_cpu_temp() -> float:
"""
Obtains the current CPU temperature:
:returns: Current value of the CPU temperature if successful.
:rtype: float
"""
thermal_path = '/sys/class/thermal/thermal_zone0/temp'
if os.path.isfile(thermal_path):
with open(thermal_path) as f:
line = f.readline().strip()
if line.isdigit():
return float(line) / 1000
return 0.0
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment