You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python获取联想ThinkPad T570双电池的单独信息?

获取联想ThinkPad T570双电池的单独电量信息(Python实现)

psutil的sensors_battery()确实只会返回合并后的整体电池数据,要获取双电池的单独信息,得根据操作系统分别处理:

Windows 平台

可以通过wmi库直接调用Windows的WMI接口,读取每块电池的独立数据:

  1. 先安装依赖库:
pip install wmi
  1. 示例代码:
import wmi

c = wmi.WMI()
# 遍历所有电池实例
for battery in c.Win32_Battery():
    print(f"电池标识: {battery.DeviceID}")
    print(f"当前电量百分比: {battery.EstimatedChargePercentage}%")
    print(f"电池状态: {battery.Status}")
    print(f"设计容量: {battery.DesignCapacity} mWh")
    print(f"预估剩余续航: {battery.EstimatedRunTime} 分钟(未充电时)")
    print("-" * 30)

这段代码会输出每块电池的独立电量、状态、容量等信息,ThinkPad T570的两块电池会分别对应不同的DeviceID。

Linux 平台

Linux系统下的电池信息存储在/sys/class/power_supply/目录中,每块电池对应一个以BAT开头的子目录(比如BAT0、BAT1):

示例代码:

import os

BATTERY_PATH = "/sys/class/power_supply/"

# 遍历所有电池目录
for dir_name in os.listdir(BATTERY_PATH):
    if dir_name.startswith("BAT"):
        battery_dir = os.path.join(BATTERY_PATH, dir_name)
        # 读取电量百分比
        with open(os.path.join(battery_dir, "capacity"), "r") as f:
            capacity = f.read().strip()
        # 读取电池状态(Charging/Discharging/Full等)
        with open(os.path.join(battery_dir, "status"), "r") as f:
            status = f.read().strip()
        # 读取设计容量
        with open(os.path.join(battery_dir, "energy_full_design"), "r") as f:
            design_capacity = f.read().strip()
        # 读取当前剩余容量
        with open(os.path.join(battery_dir, "energy_now"), "r") as f:
            current_capacity = f.read().strip()
        
        print(f"电池标识: {dir_name}")
        print(f"当前电量百分比: {capacity}%")
        print(f"电池状态: {status}")
        print(f"设计容量: {design_capacity} µWh")
        print(f"当前剩余容量: {current_capacity} µWh")
        print("-" * 30)

这段代码会直接读取系统文件获取每块电池的独立数据,无需额外安装依赖。

内容的提问来源于stack exchange,提问作者Liam Alexander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 02:25:03