當前位置:首頁 » 編程語言 » 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

熱點內容
c語言中怎麼賦值 發布:2025-09-19 01:17:43 瀏覽:956
公網伺服器如何共享ip 發布:2025-09-19 01:03:43 瀏覽:237
存儲器已幾乎滿 發布:2025-09-19 00:36:28 瀏覽:884
安卓系統在哪裡輸入網址 發布:2025-09-19 00:35:46 瀏覽:173
armlinuxgccgcc 發布:2025-09-19 00:35:37 瀏覽:425
wincachephp 發布:2025-09-19 00:30:28 瀏覽:863
如何給文件夾設置圖標 發布:2025-09-19 00:06:09 瀏覽:750
火車頭密碼指紋鎖多少錢 發布:2025-09-18 23:16:55 瀏覽:139
雪佛蘭最高配置長什麼樣 發布:2025-09-18 23:16:54 瀏覽:173
網路通話源碼 發布:2025-09-18 23:07:23 瀏覽:92