當前位置:首頁 » 編程語言 » python電腦溫度

python電腦溫度

發布時間: 2022-11-30 02:44:28

1. 用python獲取電腦cpu溫度代碼

from __future__ import division
import os
from collections import namedtuple

_nt_cpu_temp = namedtuple('cputemp', 'name temp max critical')

def get_cpu_temp(fahrenheit=False):
"""Return temperatures expressed in Celsius for each physical CPU
installed on the system as a list of namedtuples as in:

>>> get_cpu_temp()
[cputemp(name='atk0110', temp=32.0, max=60.0, critical=95.0)]
"""
# http://www.mjmwired.net/kernel/Documentation/hwmon/sysfs-interface
cat = lambda file: open(file, 'r').read().strip()
base = '/sys/class/hwmon/'
ls = sorted(os.listdir(base))
assert ls, "%r is empty" % base
ret = []
for hwmon in ls:
hwmon = os.path.join(base, hwmon)
label = cat(os.path.join(hwmon, 'temp1_label'))
assert 'cpu temp' in label.lower(), label
name = cat(os.path.join(hwmon, 'name'))
temp = int(cat(os.path.join(hwmon, 'temp1_input'))) / 1000
max_ = int(cat(os.path.join(hwmon, 'temp1_max'))) / 1000
crit = int(cat(os.path.join(hwmon, 'temp1_crit'))) / 1000
digits = (temp, max_, crit)
if fahrenheit:
digits = [(x * 1.8) + 32 for x in digits]
ret.append(_nt_cpu_temp(name, *digits))
return ret

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:581
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:875
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:569
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:755
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:672
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:999
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:242
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:102
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:794
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:700