在 Python 中获取 CPU 温度的方法取决于你的操作系统。
在 Linux 操作系统中,你可以使用
/sys/class/thermal/thermal_zone0/temp
文件来获取 CPU 的温度。这个文件中存储着 CPU 的温度,单位是摄氏度。你可以使用 Python 的
open()
函数打开这个文件,并使用
read()
方法读取其中的内容。例如:
with open("/sys/class/thermal/thermal_zone0/temp") as f:
temp = f.read()
print(float(temp) / 1000)
在 Windows 操作系统中,你可以使用 wmi
模块来获取 CPU 的温度。首先,你需要安装 wmi
模块,可以使用 pip install wmi
命令来安装。然后,你可以使用以下代码来获取 CPU 的温度:
import wmi
c = wmi.WMI()
for sensor in c.Win32_TemperatureProbe():
print(sensor.InstanceName)
print(sensor.CurrentReading)
注意,这种方法获取的 CPU 温度可能是摄氏度或华氏度,具体取决于你的系统。
希望这些信息对你有帮助。