python 在Windows上的python中获取CPU温度
时间: 2024-02-18 08:03:04 浏览: 18
获取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可能有不同的传感器名称,如果上述代码无法正常工作,请根据实际情况修改代码。
python windows cpu指令循环,在Windows上的python中获取CPU温度
获取CPU温度需要使用第三方库来获取,可以使用 PyWin32 和 OpenHardwareMonitor 库。
首先,你需要安装 PyWin32 库。可以通过以下命令来安装:
```
pip install pywin32
```
然后,你需要下载 OpenHardwareMonitor 应用程序,并安装在你的电脑上。接下来,你需要使用以下代码来获取 CPU 温度:
```python
import win32com.client
wmi = win32com.client.GetObject('winmgmts:/root/wmi')
temperature_info = wmi.MSAcpi_ThermalZoneTemperature()[0]
temperature = temperature_info.CurrentTemperature / 10.0 - 273.15
print(f"CPU 温度为 {temperature} ℃")
```
这个脚本使用 WMI(Windows Management Instrumentation)来获取 CPU 温度。它首先创建一个 WMI 对象,然后使用 `winmgmts` 命名空间来连接到 WMI 服务。然后,它使用 `MSAcpi_ThermalZoneTemperature()` 方法来获取 CPU 温度信息。最后,它计算出 CPU 温度并打印出来。
需要注意的是,这个脚本只在 Windows 上可用,并且需要管理员权限才能正常运行。