Created
November 17, 2021 18:49
-
-
Save SyNeto/2d78e391775bf7c1c882ab0196152337 to your computer and use it in GitHub Desktop.
Obtains current CPU temperature an prints it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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