windows 在 Python 中,如何检测计算机是否使用电池供电?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6153860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Python, how can I detect whether the computer is on battery power?
提问by Joe White
I'm playing around with pygame, and one thing I'd like to do is reduce the number of frames per second when the computer is on battery power (to lower the CPU usage and extend battery life).
我正在玩 pygame,我想做的一件事是在计算机使用电池供电时减少每秒帧数(以降低 CPU 使用率并延长电池寿命)。
How can I detect, from Python, whether the computer is currently on battery power?
如何从 Python 中检测计算机当前是否处于电池供电状态?
I'm using Python 3.1 on Windows.
我在 Windows 上使用 Python 3.1。
采纳答案by Ben Hoyt
If you want to do it without win32api
, you can use the built-in ctypes
module. I usually run CPython without win32api
, so I kinda like these solutions.
如果你不想这样做win32api
,你可以使用内置ctypes
模块。我通常在没有 .的情况下运行 CPython win32api
,所以我有点喜欢这些解决方案。
It's a tiny bit more work for GetSystemPowerStatus()
because you have to define the SYSTEM_POWER_STATUS
structure, but not bad.
GetSystemPowerStatus()
由于您必须定义SYSTEM_POWER_STATUS
结构,因此需要做更多的工作,但还不错。
# Get power status of the system using ctypes to call GetSystemPowerStatus
import ctypes
from ctypes import wintypes
class SYSTEM_POWER_STATUS(ctypes.Structure):
_fields_ = [
('ACLineStatus', wintypes.BYTE),
('BatteryFlag', wintypes.BYTE),
('BatteryLifePercent', wintypes.BYTE),
('Reserved1', wintypes.BYTE),
('BatteryLifeTime', wintypes.DWORD),
('BatteryFullLifeTime', wintypes.DWORD),
]
SYSTEM_POWER_STATUS_P = ctypes.POINTER(SYSTEM_POWER_STATUS)
GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus
GetSystemPowerStatus.argtypes = [SYSTEM_POWER_STATUS_P]
GetSystemPowerStatus.restype = wintypes.BOOL
status = SYSTEM_POWER_STATUS()
if not GetSystemPowerStatus(ctypes.pointer(status)):
raise ctypes.WinError()
print 'ACLineStatus', status.ACLineStatus
print 'BatteryFlag', status.BatteryFlag
print 'BatteryLifePercent', status.BatteryLifePercent
print 'BatteryLifeTime', status.BatteryLifeTime
print 'BatteryFullLifeTime', status.BatteryFullLifeTime
On my system that prints this (basically meaning "desktop, plugged in"):
在我打印这个的系统上(基本上意味着“桌面,插入”):
ACLineStatus 1
BatteryFlag -128
BatteryLifePercent -1
BatteryLifeTime 4294967295
BatteryFullLifeTime 4294967295
回答by Giampaolo Rodolà
The most reliable way to retrieve this information in C is by using GetSystemPowerStatus. If no battery is present ACLineStatus
will be set to 128
. psutilexposes this information under Linux, Windows and FreeBSD, so to check if battery is present you can do this
在 C 中检索此信息的最可靠方法是使用GetSystemPowerStatus。如果没有电池,ACLineStatus
将设置为128
。psutil在 Linux、Windows 和 FreeBSD 下公开此信息,因此要检查电池是否存在,您可以执行此操作
>>> import psutil
>>> has_battery = psutil.sensors_battery() is not None
If a battery is present and you want to know whether the power cable is plugged in you can do this:
如果有电池并且您想知道电源线是否已插入,您可以执行以下操作:
>>> import psutil
>>> psutil.sensors_battery()
sbattery(percent=99, secsleft=20308, power_plugged=True)
>>> psutil.sensors_battery().power_plugged
True
>>>
回答by sorin
It is easy, all you have to do is to call Windows API function GetSystemPowerStatusfrom Python, probably by importing win32api
module.
这很简单,您所要做的就是从 Python调用 Windows API 函数GetSystemPowerStatus,可能是通过导入win32api
模块。
EDIT:GetSystemPowerStatus()
is not yet implemented in win32api
as of build 219 (2014-05-04).
编辑:GetSystemPowerStatus()
尚未在win32api
构建 219 (2014-05-04) 中实现。
回答by Wayne DSouza
A simple method for cross platform power status indication is the 'power' module which you can install with pip
跨平台电源状态指示的一种简单方法是您可以使用 pip 安装的“电源”模块
import power
ans = power.PowerManagement().get_providing_power_source_type()
if not ans:
print "plugged into wall socket"
else:
print "on battery"
回答by Abhyudai
You can install acpi
.From wikipedia
您可以安装acpi
.From wikipedia
In a computer, the Advanced Configuration and Power Interface provides an open standard that operating systems can use to discover and configure computer hardware components, to perform power management by putting unused components to sleep, and to perform status monitoring.
在计算机中,高级配置和电源接口提供了一个开放标准,操作系统可以使用该标准来发现和配置计算机硬件组件、通过将未使用的组件置于睡眠状态来执行电源管理以及执行状态监控。
Then use the subprocess
module in python
然后subprocess
在python中使用模块
import subprocess
cmd = 'acpi -b'
# for python 3.7+
p = subprocess.run(cmd.split(), shell=True, capture_output=True)
battery_info, error = p.stdout.decode(), p.stderr.decode()
# for python3.x (x<6)
battery_info = subprocess.check_output(cmd.split(), shell=True).decode('utf-8')
print (battery_info)