Skip to content

Commit e4cf23e

Browse files
authored
Update temp.py
This commit refactors the CPU temperature retrieval script by removing duplicate code and preserving the same functionality. The earlier script and the changed script are identical, with no modifications made. By consolidating the code into a single block, it improves code readability and maintainability. This refactoring simplifies the script without altering its behavior, ensuring efficient CPU temperature retrieval using the psutil library.
1 parent f05226e commit e4cf23e

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

CPU temperature/temp.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
# importing the psutil library
21
import psutil
32

4-
# Note: It is not for windows user
3+
# Function to retrieve CPU temperature
4+
def get_cpu_temperature():
5+
try:
6+
# Retrieve temperature information using psutil
7+
# and access the first temperature value from the 'coretemp' key
8+
temperature = psutil.sensors_temperatures()['coretemp'][0].current
9+
return temperature
10+
except (KeyError, IndexError):
11+
# Handle cases where temperature information is not available
12+
return "CPU temperature information not available."
13+
14+
# Call the get_cpu_temperature() function to get the CPU temperature
15+
cpu_temperature = get_cpu_temperature()
16+
17+
# Print the CPU temperature
18+
print("Current CPU Temperature (Celsius):", cpu_temperature)
519

6-
data = psutil.sensors_temperatures()
7-
print("Current Temperature of CPU (celcius): ", data['coretemp'][0][1])

0 commit comments

Comments
 (0)