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).
How can I detect, from Python, whether the computer is currently on battery power?
I'm using Python 3.1 on Windows.
If you want to do it without
win32api
, you can use the built-inctypes
module. I usually run CPython withoutwin32api
, so I kinda like these solutions.It's a tiny bit more work for
GetSystemPowerStatus()
because you have to define theSYSTEM_POWER_STATUS
structure, but not bad.On my system that prints this (basically meaning "desktop, plugged in"):
The most reliable way to retrieve this information in C is by using GetSystemPowerStatus. If no battery is present
ACLineStatus
will be set to128
. psutil exposes this information under Linux, Windows and FreeBSD, so to check if battery is present you can do thisIf a battery is present and you want to know whether the power cable is plugged in you can do this:
It is easy, all you have to do is to call Windows API function GetSystemPowerStatus from Python, probably by importingwin32api
module.EDIT:
GetSystemPowerStatus()
is not yet implemented inwin32api
as of build 219 (2014-05-04).A simple method for cross platform power status indication is the 'power' module which you can install with pip