Skip to content

Instantly share code, notes, and snippets.

@joneskoo
Created August 21, 2011 07:29
Show Gist options
  • Save joneskoo/1160290 to your computer and use it in GitHub Desktop.
Save joneskoo/1160290 to your computer and use it in GitHub Desktop.
Read battery information from System Profiler (OS X)
#!/usr/bin/env python3
from tempfile import NamedTemporaryFile
from subprocess import call
import plistlib
with NamedTemporaryFile() as f:
call(['system_profiler', '-xml', 'SPPowerDataType'], stdout=f)
f.seek(0)
p = plistlib.readPlist(f)
d = p[0]['_items'][0]
voltage = d['sppower_current_voltage']
amperage = d['sppower_current_amperage']
chargeinfo = d['sppower_battery_charge_info']
maxcap = chargeinfo['sppower_battery_max_capacity']
is_charging = chargeinfo['sppower_battery_is_charging']
capacity = chargeinfo['sppower_battery_current_capacity']
print("%d mV %d mA (full: %d mAh)" % (voltage, amperage, maxcap))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment