In Python, how can I detect whether the computer is on battery power?

python get battery status
python process monitoring
python get running processes
python ps
psutil
python system monitoring
python pciutils
modulenotfounderror: no module named psutil

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-in ctypes module. I usually run CPython without win32api, so I kinda like these solutions.

It's a tiny bit more work for GetSystemPowerStatus() because you have to define the SYSTEM_POWER_STATUS structure, but not bad.

# 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

Python – how can I detect whether the computer is on battery power , Python – how can I detect whether the computer is on battery power or AC? Write the result to a text file every 5 seconds. import ctypes from  If no battery is present ACLineStatus will be set to 128. psutil exposes this information under Linux, Windows and FreeBSD, so to check if battery is present you can do this >>> 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:

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. psutil exposes this information under Linux, Windows and FreeBSD, so to check if battery is present you can do this

>>> 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
>>> 

Computer Vision Systems: 12th International Conference, ICVS 2019, , The water detection system can be battery-powered or connected to the power as a Python script which periodically receives camera images, forwards them to the according to the following computation scheme: vn = { 1 0 ,if ,else owater,n >  Posted by admin on October 10, 2016 Python Python – how can I detect whether the computer is on battery power or AC? Write the result to a text file every 5 seconds

It is easy, all you have to do is to call Windows API function GetSystemPowerStatus from Python, probably by importing win32api module.

EDIT: GetSystemPowerStatus() is not yet implemented in win32api as of build 219 (2014-05-04).

Using Windows PowerShell to Determine if a Laptop Is on Battery , Hmm, I wonder if I could write a Windows PowerShell script to pick the that indicates if the computer is running on battery, or if it is on power. We found some information that suggests the value is reported back as milliwatt-hours, but we don’t know that for sure; for that matter, we don’t even know what a millwatt-hour is. However, we do know this: on our test computer the Windows Power Meter would report an estimated battery life like 2 hours and 54 minutes (that is, 174 minutes).

A simple method for cross platform power status indication is the 'power' module which you can install with pip

    import power
    ans = power.PowerManagement().get_providing_power_source_type()
    if not ans:
        print "plugged into wall socket"
    else:
        print "on battery"

Computer Science with Python, Special Purpose Computer is the one that is designed to perform only one specific task. Detecting patient's heart function and temperature. Their power source is a battery—a completely selfcontained yet can be recharged from an  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

You can install 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

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) 

Computer and Cyber Security: Principles, Algorithm, Applications, , Dex2jar is used to convert Dalvik Executable (.dex/.odex) format to jar files by It supports Python scripts and plugins. Many security solutions have been proposed by the researchers to detect and Smartphones are the resource- constrained devices that consist of a limited battery life and low computational power. First you should know there is no standard way to determine if a computer is ON or not, and this is not language/platform dependent. A computer can fake any state you try to check. But the usual way to check if a computer is ON is sending a Ping (ICMP Echo Request). This answer shows how to send a Ping using Python.

Raspberry Pi Home Automation with Arduino, Our final task will be to test the system again to confirm that we have installed it correctly. The battery pack or power adapter should be hooked up to power your Arduino. Ensure From a separate computer, run the following HTSQL query: http://<iphttp://<ip of You now have a working, installed parcel detection system. Batteries power our favorite electronic devices, but they're not meant to last forever. The good news is that Windows 10 laptops have a Battery Report feature that breaks down whether your battery

power · PyPI, Library that allows you get current power source type (AC, Battery or UPS), warning level (none, <22%, <10min) and remaining minutes. You can also observe  Make sure to re-plug the charger, and see if a charging notification will pop-up. If not, you can try to use other charger and test your own charger to other laptop. Refer to your laptop's user manual to check for battery troubleshooting. You can also contact the laptop manufacturer for further assistance. Kindly post back if you have further

16th Nordic-Baltic Conference on Biomedical Engineering: 16. NBC & , artefact detection algorithm, and right-leg drive (RLD) input in low profile quad Several ADAS chips can be combined as a master and slaves if more ECG 2) Raspberry PI is a credit-card-size single-board computer with an ARM processor. direct or indirect, to power-line voltage (e.g., in battery-driven holter devices). Python – how can I detect whether the computer is on battery power or AC? Upload a file with jquery (ajax javascript) and PHP Python – Tkinter panel control execute bash

Comments
  • Or you could use a bool, that F1 toggles. When on call with 30, else 0. pygame.org/docs/ref/time.html#Clock.tick
  • There is a bug in wintypes (bugs.python.org/issue16376) when BYTE is defined as c_byte, not c_ubyte. Therefore values in SYSTEM_POWER_STATUS won't match those defined in documentation. Use c_ubyte instead of BYTE.
  • @swati if you install ActiveState Python you will not need to install pywin32 anymore, it is included.
  • Alas ActiveState python is some kind of proprietary software, and not opensource á la vanilla Python.
  • It is a commercial product, still open source and you do not have to pay (free). Usually I would prefer to use Python.org version but due to the missing modules, that are not so easy to compile on Windows due to the big number of compilers it makes a lot of sense to use ActivePython. I recommend this from my experience.
  • Generally, ActiveState distributions are just batteries included distos of the language in question, and you don't need to pay for them. They have software you can pay for (Komodo), but I believe all the language packages they offer are free to download.
  • @Sorin, it'd be nice if you could provide a working example of the win32api version.
  • This doesn't seem to be working anymore on MacOS as of March 2020. Maybe since Catalina?