Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

working with laptop battery

6 views
Skip to first unread message

Daniel Dalton

unread,
Feb 13, 2010, 7:48:31 PM2/13/10
to Python-list
Hi,

I'm constantly working in the command line and need to write a program
to give me alerts on my battery. Can someone please tell me what module
I should use to access battery information? Looking for something that
perhaps makes use of acpi so I can get estimated time left as well as a
percentage.

Thanks very much for any help,
Dan

Chris Rebert

unread,
Feb 13, 2010, 8:26:02 PM2/13/10
to Daniel Dalton, Python-list
On Sat, Feb 13, 2010 at 4:48 PM, Daniel Dalton <d.da...@iinet.net.au> wrote:
> Hi,
>
> I'm constantly working in the command line and need to write a program
> to give me alerts on my battery. Can someone please tell me what module
> I should use to access battery information? Looking for something that
> perhaps makes use of acpi so I can get estimated time left as well as a
> percentage.

It's probably gonna depend on which OS you're running. Which would be...?

Cheers,
Chris
--
http://blog.rebertia.com

Daniel Dalton

unread,
Feb 13, 2010, 8:43:24 PM2/13/10
to Chris Rebert, Python-list
On Sat, Feb 13, 2010 at 05:26:02PM -0800, Chris Rebert wrote:
> It's probably gonna depend on which OS you're running. Which would be...?

Sorry, forgot to mention this. I'm running debian linux.

Thanks,
Dan

Daniel Fetchinson

unread,
Feb 13, 2010, 9:22:11 PM2/13/10
to Python
>> It's probably gonna depend on which OS you're running. Which would be...?
>
> Sorry, forgot to mention this. I'm running debian linux.

I don't know about python modules but have a look at

/proc/acpi/battery/BAT0/info
/proc/acpi/battery/BAT0/state

You can parse the numbers you want from there.

HTH,
Daniel


--
Psss, psss, put it down! - http://www.cafepress.com/putitdown

Tim Chase

unread,
Feb 13, 2010, 9:23:28 PM2/13/10
to Daniel Dalton, Python-list
Daniel Dalton wrote:
> On Sat, Feb 13, 2010 at 05:26:02PM -0800, Chris Rebert wrote:
>> It's probably gonna depend on which OS you're running. Which would be...?
>
> Sorry, forgot to mention this. I'm running debian linux.

You should be able to read/poll the various files in

/proc/acpi/battery/BAT*/*

for whatever battery information you need. Each BAT* directory
contains information about one of the batteries in the system
(it's possible, albeit rare, to have more than one). So you
might have some script that runs every $INTERVAL that looks
something like

from glob import glob
for fname in glob('/proc/acpi/battery/BAT*/*'):
f = file(fname)
for line in f:
do_something(line)
f.close()

On my Debian laptop (Gateway Solo 1200, OEM battery circa 2001),
the contents look something like

tim@rubbish:/proc/acpi/battery/BAT0$ cat alarm
alarm: unsupported
tim@rubbish:/proc/acpi/battery/BAT0$ cat info
present: yes
design capacity: 4016 mAh
last full capacity: 4011 mAh
battery technology: rechargeable
design voltage: 9600 mV
design capacity warning: 602 mAh
design capacity low: 401 mAh
capacity granularity 1: 201 mAh
capacity granularity 2: 3409 mAh
model number: QT08
serial number: SANYOQT08
battery type: NiMH
OEM info: SANYO
tim@rubbish:/proc/acpi/battery/BAT0$ cat state
present: yes
capacity state: ok
charging state: charged
present rate: unknown
remaining capacity: 4011 mAh
present voltage: 9600 mV

-tkc


Daniel Dalton

unread,
Feb 18, 2010, 6:09:05 AM2/18/10
to Chris Colbert, Python-list
On Sat, Feb 13, 2010 at 09:19:59PM -0500, Chris Colbert wrote:
> You'll need acpi installed:
> In [6]: import subprocess

Thanks for that code, I'll try putting something together this weekend.

Dan

Daniel Dalton

unread,
Feb 18, 2010, 6:11:51 AM2/18/10
to Tim Chase, Python-list
I'm not sure I have those files, but I'll look a little harder this
weekend when I put together the script.

Thanks for your help,
Dan

Daniel Dalton

unread,
Feb 18, 2010, 6:16:07 AM2/18/10
to pytho...@python.org
On Sun, Feb 14, 2010 at 03:22:11AM +0100, Daniel Fetchinson wrote:
> /proc/acpi/battery/BAT0/info
> /proc/acpi/battery/BAT0/state

Had a quick look, but that path doesn't seem to exist, I'll look harder
on the weekend when I put the script together, because it has to be
somewhere.

Thanks,
Dan

0 new messages