python在Windows下获取CPU温度的例子
时间: 2023-06-04 13:06:25 浏览: 695
以下是获取CPU温度的python示例代码:
```python
import wmi
c = wmi.WMI(namespace='root\OpenHardwareMonitor')
temperature_infos = c.Sensor()
for sensor in temperature_infos:
if sensor.SensorType==u'Temperature' and sensor.Name==u'CPU Core':
print(sensor.Name + ': ' + str(sensor.Value) + 'C')
```
这个脚本需要安装 Open Hardware Monitor,可以通过 pip 安装:
```
pip install wmi
```
相关问题
python 在Windows上的python中获取CPU温度
获取CPU温度需要借助第三方库,常用的有psutil和wmi,以下是使用psutil的示例代码:
```python
import psutil
def get_cpu_temp():
temp = psutil.sensors_temperatures()['coretemp'][0].current # 以coretemp为例,如果不是coretemp需要修改
return temp
if __name__ == '__main__':
temp = get_cpu_temp()
print(f'当前CPU温度为{temp}℃')
```
请确保已安装psutil库,可以使用pip进行安装:
```
pip install psutil
```
注意:不同的CPU可能有不同的传感器名称,如果上述代码无法正常工作,请根据实际情况修改代码。
在Windows上的python中获取AMD CPU温度
获取AMD CPU温度需要借助第三方库,常用的有py-cpuinfo和wmi,以下是使用py-cpuinfo的示例代码:
```python
import cpuinfo
def get_cpu_temp():
cpu_info = cpuinfo.get_cpu_info()
if cpu_info['vendor_id_raw'].lower() == 'amd':
for sensor in cpu_info['sensors']:
if sensor['label'] == 'Tdie':
return sensor['current_value']
return None
if __name__ == '__main__':
temp = get_cpu_temp()
if temp is not None:
print(f'当前CPU温度为{temp}℃')
else:
print('无法获取CPU温度')
```
请确保已安装py-cpuinfo库,可以使用pip进行安装:
```
pip install py-cpuinfo
```
注意:不同的CPU可能有不同的传感器名称,如果上述代码无法正常工作,请根据实际情况修改代码。