Search code examples
pythonpython-3.xbattery

How to get battery percentage with python?


How to get battery percentage with python?
Maybe some win32api functions may help.


Solution

  • Try this:

    import psutil
    battery = psutil.sensors_battery()
    plugged = battery.power_plugged
    percent = str(battery.percent)
    plugged = "Plugged In" if plugged else "Not Plugged In"
    print(percent+'% | '+plugged)
    

    Library link is here. And check out this