Windows 7/11下无需管理员权限的Python CPU温度获取脚本需求
无需管理员权限的Windows CPU温度获取脚本
下面提供两个无需管理员权限即可运行的Python脚本,适配Windows 7和Windows 11系统:
方案一:使用psutil库(推荐)
psutil是跨平台的系统监控库,在Windows上获取温度无需额外权限,实现简单:
首先安装依赖:
pip install psutil
脚本代码:
import psutil def get_cpu_temperature(): temps = psutil.sensors_temperatures() if 'coretemp' in temps: # 适配Intel CPU core_temps = [temp.current for temp in temps['coretemp']] avg_temp = sum(core_temps) / len(core_temps) return f"CPU平均温度: {int(avg_temp)}°C" elif 'k10temp' in temps: # 适配AMD CPU return f"CPU温度: {int(temps['k10temp'][0].current)}°C" elif 'thermal_zone' in temps: # 通用ACPI温度传感器 return f"CPU温度: {int(temps['thermal_zone'][0].current)}°C" else: return "无法检测到CPU温度传感器" if __name__ == "__main__": print(get_cpu_temperature()) input("Press any key to exit...")
方案二:使用WMI访问无需权限的命名空间
如果不想依赖psutil,可以使用wmi库访问root\cimv2下的性能计数器类,多数系统无需管理员权限:
首先安装依赖:
pip install wmi
脚本代码:
import wmi def main(): try: # 访问root\cimv2命名空间,无需管理员权限 c = wmi.WMI(namespace='root\\cimv2') # 查询热区性能数据 thermal_data = c.Win32_PerfFormattedData_Counters_ThermalZoneInformation() if thermal_data: # 转换为摄氏度(原始值为开尔文) temp = int(thermal_data[0].Temperature - 273.15) print(f"CPU温度: {temp}°C") else: print("无法获取温度数据") except Exception as e: print(f"获取温度失败: {str(e)}") input("Press any key to exit...") if __name__ == "__main__": main()
注意事项
- 部分老旧硬件或定制化系统可能无法通过上述方法获取温度,取决于硬件是否向系统暴露温度数据
psutil的传感器支持依赖系统驱动,若无法检测到温度,建议更新硬件驱动
内容的提问来源于stack exchange,提问作者Chris P
相关产品推荐
相关产品推荐

