Accessing cpu temperature in python

Python provides several ways to access the CPU temperature. In this article, we will explore three different methods to accomplish this task. We will discuss each method in detail and compare their advantages and disadvantages.

Method 1: Using the psutil Library

The psutil library is a cross-platform library for retrieving information on running processes and system utilization. It provides a simple and straightforward way to access the CPU temperature in Python.

import psutil

def get_cpu_temperature():
    temperature = psutil.sensors_temperatures()['coretemp'][0].current
    return temperature

temperature = get_cpu_temperature()
print(f"CPU Temperature: {temperature}°C")

This method uses the `sensors_temperatures()` function from the psutil library to retrieve the CPU temperature. It returns a dictionary containing temperature information for different sensors. In this case, we are accessing the ‘coretemp’ sensor and retrieving the current temperature.

Method 2: Reading the Temperature from sysfs

On Linux systems, the CPU temperature can be accessed by reading the temperature files in the sysfs filesystem. This method requires accessing the appropriate file and parsing the temperature value.

def get_cpu_temperature():
    with open('/sys/class/thermal/thermal_zone0/temp', 'r') as file:
        temperature = int(file.read()) / 1000
    return temperature

temperature = get_cpu_temperature()
print(f"CPU Temperature: {temperature}°C")

This method directly reads the temperature value from the ‘/sys/class/thermal/thermal_zone0/temp’ file and converts it to Celsius by dividing it by 1000.

Method 3: Using the platform Module

The platform module in Python provides a way to access system-specific information, including the CPU temperature. This method uses the `platform.system()` function to determine the operating system and then retrieves the temperature accordingly.

import platform

def get_cpu_temperature():
    system = platform.system()
    if system == 'Linux':
        with open('/sys/class/thermal/thermal_zone0/temp', 'r') as file:
            temperature = int(file.read()) / 1000
    elif system == 'Windows':
        # Code to retrieve temperature on Windows
        pass
    elif system == 'Darwin':
        # Code to retrieve temperature on macOS
        pass
    else:
        temperature = None
    return temperature

temperature = get_cpu_temperature()
if temperature is not None:
    print(f"CPU Temperature: {temperature}°C")
else:
    print("Unable to retrieve CPU temperature.")

This method first determines the operating system using `platform.system()` and then retrieves the temperature accordingly. However, it requires additional code to handle temperature retrieval on different operating systems.

After comparing the three methods, it is evident that Method 1 using the psutil library is the most convenient and platform-independent solution. It provides a simple API to access the CPU temperature without worrying about system-specific details. Therefore, Method 1 is the recommended option for accessing the CPU temperature in Python.

Rate this post

3 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents