欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python讀取Windows和Linux的CPU、GPU、硬盤(pán)等部件溫度的讀取方法

 更新時(shí)間:2025年02月28日 09:15:03   作者:好好學(xué)習(xí)z  
本文詳細(xì)介紹了如何使用Python在Windows和Linux系統(tǒng)上通過(guò)OpenHardwareMonitor和psutil庫(kù)讀取CPU、GPU等部件的溫度,包括Windows下的兩種方法以及Linux下的簡(jiǎn)單實(shí)現(xiàn),感興趣的小伙伴跟著小編一起來(lái)看看吧

這篇博客介紹了如何使用Python讀取計(jì)算機(jī)的CPU、GPU、硬盤(pán)等部件的溫度,讀取后的信息可以根據(jù)需要進(jìn)行日志記錄、溫度監(jiān)控等。

根據(jù)不同的平臺(tái),本文分別介紹Windows下和Linux下的部件溫度讀取方法。

Windows下讀取方法

這種方法僅適用于Windows10,Windows11運(yùn)行會(huì)報(bào)錯(cuò)(我原本是在Windows10下寫(xiě)的代碼,能正常讀取溫度,但是電腦更新到Windows11后運(yùn)行直接就報(bào)錯(cuò)了),至于其他更低版本的Windows系統(tǒng)則沒(méi)有測(cè)試過(guò)。

Windows下讀取計(jì)算機(jī)部件的溫度需要用到一款叫做 Open Hardware Monitor 的軟件(下載地址),下載后得到的是一個(gè)壓縮包,解壓后的結(jié)果如下:

  • 這里我們主要用到兩個(gè)文件(紅色框標(biāo)記),也是利用這個(gè)軟件的兩種方法。
  1. 使用 OpenHardwareMonitor.exe
  • 這種方法要求始終在后臺(tái)保持 OpenHardwareMonitor.exe 處于運(yùn)行狀態(tài)。
  • 程序運(yùn)行需要管理員權(quán)限??梢栽趩?dòng)IDE時(shí)便選擇以管理員權(quán)限運(yùn)行。
    以下為代碼:
import ctypes
import datetime
import sys
import time

import wmi
import psutil
import subprocess


def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except Exception:
        return False


def check_process(path, proc_name):
    for proc in psutil.process_iter():
        if proc.name() == proc_name:
            break
    else:
        subprocess.Popen(path + proc_name)
        time.sleep(5)


if is_admin():
    path = 'openhardwaremonitor-v0.9.6/OpenHardwareMonitor/'
    proc_name = "OpenHardwareMonitor.exe"
    check_process(path, proc_name)
    w = wmi.WMI(namespace='root/OpenHardwareMonitor')

    while True:
        print('{:=^50}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
        temperature_info = w.Sensor()
        temperature_info = sorted(temperature_info, key=lambda x: str(x.Identifier))
        for sensor in temperature_info:
            if sensor.SensorType == 'Temperature':
                identifier = str(sensor.Identifier).split('/')
                print(
                    '{0:^20}的當(dāng)前溫度為:{1:>5}℃'.format('-'.join([identifier[1], identifier[2], identifier[-1]]),
                                                   sensor.Value))
        time.sleep(1)
else:
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)

輸出示例:

  1. 使用 OpenHardwareMonitorLib.dll
  • 這種方法類似于將 OpenHardwareMonitorLib.dll 作為第三方庫(kù)引入到python中。
  • 同樣的,運(yùn)行代碼需要管理員權(quán)限,否則啥都讀不出來(lái)。可以在啟動(dòng)IDE時(shí)便選擇以管理員權(quán)限運(yùn)行。
    以下為代碼:
import clr
import time
import datetime

clr.AddReference('D:/OpenHardwareMonitor/OpenHardwareMonitorLib.dll')  # 填寫(xiě)絕對(duì)路徑

import OpenHardwareMonitor as ohm
from OpenHardwareMonitor.Hardware import Computer, HardwareType, SensorType

computer = Computer()

computer.CPUEnabled = True
computer.GPUEnabled = True
computer.HDDEnabled = True
computer.RAMEnabled = True
computer.MainboardEnabled = True
computer.FanControllerEnabled = True

hardwareType = ohm.Hardware.HardwareType
sensorType = ohm.Hardware.SensorType

computer.Open()

while True:
    print('{:=^50}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
    for hardware in computer.Hardware:
        hardware.Update()
        for sensor in hardware.Sensors:
            if sensor.SensorType == sensorType.Temperature:
                si_ls = str(sensor.Identifier).split('/')
                ssname = f'{si_ls[1]}#{si_ls[-1]}'
                print(ssname, sensor.Value)
    time.sleep(1)

輸出示例:

Linux下讀取方法

  • Linux下讀取部件溫度的方法比較簡(jiǎn)單,直接使用 psutil 包即可。代碼如下:
import psutil


def main():
    if not hasattr(psutil, "sensors_temperatures"):
        sys.exit("platform not supported")
    temps = psutil.sensors_temperatures()
    if not temps:
        sys.exit("can't read any temperature")
    for name, entries in temps.items():
        print(name)
        for entry in entries:
            print("    %-20s %s °C (high = %s °C, critical = %s °C)" % (
                entry.label or name, entry.current, entry.high,
                entry.critical))
        print()
main()

輸出示例:

以上就是Python讀取Windows和Linux的CPU、GPU、硬盤(pán)等部件溫度的讀取方法的詳細(xì)內(nèi)容,更多關(guān)于Python讀取Windows和Linux部件溫度的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python內(nèi)置函數(shù)OCT詳解

    Python內(nèi)置函數(shù)OCT詳解

    本文給大家介紹的是python中的內(nèi)置函數(shù)oct(),其主要作用是將十進(jìn)制數(shù)轉(zhuǎn)換成八進(jìn)制,再變成字符。有需要的小伙伴可以參考下
    2016-11-11
  • Python命令行參數(shù)解析之a(chǎn)rgparse模塊詳解

    Python命令行參數(shù)解析之a(chǎn)rgparse模塊詳解

    這篇文章主要介紹了Python命令行參數(shù)解析之a(chǎn)rgparse模塊詳解,argparse?是?Python?的一個(gè)標(biāo)準(zhǔn)庫(kù),用于命令行參數(shù)的解析,這意味著我們無(wú)需在代碼中手動(dòng)為變量賦值,而是可以直接在命令行中向程序傳遞相應(yīng)的參數(shù),再由變量去讀取這些參數(shù),需要的朋友可以參考下
    2023-08-08
  • Python中的星號(hào)*還能這么用你知道嗎

    Python中的星號(hào)*還能這么用你知道嗎

    這篇文章主要為大家詳細(xì)介紹了Python中的星號(hào)*用法的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-06-06
  • Django跨域請(qǐng)求CSRF的方法示例

    Django跨域請(qǐng)求CSRF的方法示例

    這篇文章主要介紹了Django跨域請(qǐng)求CSRF的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • Python做簡(jiǎn)單的字符串匹配詳解

    Python做簡(jiǎn)單的字符串匹配詳解

    這篇文章主要介紹了Python做簡(jiǎn)單的字符串匹配詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Python實(shí)現(xiàn)控制臺(tái)中的進(jìn)度條功能代碼

    Python實(shí)現(xiàn)控制臺(tái)中的進(jìn)度條功能代碼

    下面小編就為大家分享一篇Python實(shí)現(xiàn)控制臺(tái)中的進(jìn)度條功能代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • PyCharm配置第三方鏡像源的解決方法

    PyCharm配置第三方鏡像源的解決方法

    在pycharm中配置第三方鏡像后,秩序搜索需要的第三方庫(kù),就可以使用第三方鏡像下載,速度不是一般的快,這篇文章主要介紹了PyCharm配置第三方鏡像源,需要的朋友可以參考下
    2024-01-01
  • Python之——生成動(dòng)態(tài)路由軌跡圖的實(shí)例

    Python之——生成動(dòng)態(tài)路由軌跡圖的實(shí)例

    今天小編就為大家分享一篇Python之——生成動(dòng)態(tài)路由軌跡圖的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Python?"手繪風(fēng)格"數(shù)據(jù)可視化方法實(shí)例匯總

    Python?"手繪風(fēng)格"數(shù)據(jù)可視化方法實(shí)例匯總

    這篇文章主要給大家介紹了關(guān)于Python?"手繪風(fēng)格"數(shù)據(jù)可視化方法實(shí)現(xiàn)的相關(guān)資料,本文分別給大家?guī)?lái)了Python-matplotlib手繪風(fēng)格圖表繪制、Python-cutecharts手繪風(fēng)格圖表繪制以及Python-py-roughviz手繪風(fēng)格圖表繪制,需要的朋友可以參考下
    2022-02-02
  • selenium學(xué)習(xí)教程之定位以及切換frame(iframe)

    selenium學(xué)習(xí)教程之定位以及切換frame(iframe)

    這篇文章主要給大家介紹了關(guān)于selenium學(xué)習(xí)教程之定位以及切換frame(iframe)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評(píng)論