Accessing CPU temperature in python

前端 未结 7 1519
暗喜
暗喜 2020-11-29 04:07

I need an example code for accessing CPU temperature in python.

I\'m running windows 7, BTW.

7条回答
  •  囚心锁ツ
    2020-11-29 04:48

    You can use pywin32 to access the native Windows API. I believe it should be possible to query the Windows API for the CPU temperature if the manufacturer for your mainboard driver registers a WMI Data Provider through their driver. Assuming this is the case you could download the pywin32 extensions and the Python WMI module mentioned in the answer by ars, and then proceed as follows:

    import wmi
    w = wmi.WMI()
    print w.Win32_TemperatureProbe()[0].CurrentReading
    

    Looking at the IronPython script in the ars' answer there seems to be another way to do it too, using a different WMI object. Using the same API and approach you could try receiving the temperature value with

    w = wmi.WMI(namespace="root\wmi")
    temperature_info = w.MSAcpi_ThermalZoneTemperature()[0]
    print temperature_info.CurrentTemperature
    

    which apparently should return the temperature value in tenths of Kelvin, thus to receive the degree in Celsius I guess you just divide this value by 10 and subtract ~273.

提交回复
热议问题