![]() |
卖萌的佛珠 · Kotlin中的Null操作:处理可空性和非空性· 2 天前 · |
![]() |
英姿勃勃的椰子 · 费斯图斯·埃泽利_百度百科· 4 月前 · |
![]() |
行走的长颈鹿 · [Absolute or Relative ...· 5 月前 · |
![]() |
狂野的柳树 · 神话故事120篇原著· 11 月前 · |
![]() |
完美的稀饭 · 超模的秘密大结局如何 - 抖音· 1 年前 · |
![]() |
踏实的馒头 · 在Node.js中将SVG图像转换为PNG, ...· 1 年前 · |
linux系统 linux服务器 python 编程语言 |
https://cloud.tencent.cn/developer/information/Python%203%E4%B8%AD%E7%9A%84CPU%E6%B8%A9%E5%BA%A6(Linux%E5%92%8CWindows)-article |
![]() |
风度翩翩的排球
5 月前 |
Python 3中获取CPU温度的方法在Linux和Windows系统上略有不同。
在Linux系统上,可以使用psutil库来获取CPU温度。psutil是一个跨平台的系统信息库,可以用于获取各种系统信息,包括CPU温度。以下是获取CPU温度的示例代码:
import psutil
def get_cpu_temperature_linux():
sensors_data = psutil.sensors_temperatures()
if 'coretemp' in sensors_data:
core_temps = sensors_data['coretemp']
cpu_temps = [temp.current for temp in core_temps]
return cpu_temps
else:
return None
cpu_temperatures = get_cpu_temperature_linux()
if cpu_temperatures:
for i, temp in enumerate(cpu_temperatures):
print(f"Core {i+1} temperature: {temp}°C")
else:
print("Failed to retrieve CPU temperature.")
在Windows系统上,可以使用wmi库来获取CPU温度。wmi是一个用于访问Windows管理接口的Python扩展库,可以用于获取各种系统信息,包括CPU温度。以下是获取CPU温度的示例代码:
import wmi
def get_cpu_temperature_windows():
w = wmi.WMI(namespace="root\\OpenHardwareMonitor")
temperature_sensors = w.Sensor()
cpu_temps = []
for sensor in temperature_sensors:
if sensor.SensorType == 'Temperature' and 'cpu' in sensor.Name.lower():
cpu_temps.append(sensor.Value)
return cpu_temps
cpu_temperatures = get_cpu_temperature_windows()
if cpu_temperatures:
for i, temp in enumerate(cpu_temperatures):
print(f"Core {i+1} temperature: {temp}°C")
else:
print("Failed to retrieve CPU temperature.")
这些代码示例中,我们使用了psutil库和wmi库来获取CPU温度。在Linux系统上,我们通过psutil库的
sensors_temperatures()
方法获取传感器数据,然后从中提取CPU温度。在Windows系统上,我们使用wmi库连接到OpenHardwareMonitor的命名空间,并通过遍历传感器数据来获取CPU温度。
这些方法可以帮助开发人员在Python 3中获取CPU温度,并根据需要进行进一步处理和应用。
![]() |
卖萌的佛珠 · Kotlin中的Null操作:处理可空性和非空性 2 天前 |
![]() |
英姿勃勃的椰子 · 费斯图斯·埃泽利_百度百科 4 月前 |
![]() |
行走的长颈鹿 · [Absolute or Relative path] `Default location for new notes` should be ignored when creating files b 5 月前 |
![]() |
狂野的柳树 · 神话故事120篇原著 11 月前 |
![]() |
完美的稀饭 · 超模的秘密大结局如何 - 抖音 1 年前 |