get system cpu temperature in fahrenheit in python

You can use the psutil and os modules in Python to get the CPU temperature of your system in Fahrenheit. Here's a sample code that does that:

main.py
import psutil
import os

def get_cpu_temperature():
    # get the temperature in Celsius
    temperature = psutil.sensors_temperatures()["coretemp"][0].current
    # convert the temperature to Fahrenheit
    temperature = temperature * 9/5 + 32
    # round off the temperature to two decimal places
    temperature = round(temperature, 2)
    return temperature

if __name__ == '__main__':
    temperature = get_cpu_temperature()
    print(f"CPU temperature: {temperature}°F")
477 chars
16 lines

The sensors_temperatures() function of psutil module returns the temperature of various system components including CPU as a namedtuple. We can use "coretemp" as the key to get the temperature tuple of the CPU. The current attribute of the namedtuple object contains the current temperature in Celsius which we convert to Fahrenheit by applying the formula temperature * 9/5 + 32. Finally, we round off the temperature to two decimal places using the round() method.

gistlibby LogSnag