How to get battery percentage with python?
Maybe some win32api
functions may help.
Asked
Active
Viewed 2.1k times
17

Mad Physicist
- 107,652
- 25
- 181
- 264

Nouman
- 6,947
- 7
- 32
- 60
1 Answers
36
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)

whackamadoodle3000
- 6,684
- 4
- 27
- 44
-
Why are you parsing the string when the object has those attributes ? You just need `battery.percent` and `battery.power_plugged`. – Mad Physicist Aug 11 '17 at 13:33
-
Good point. I will change it soon – whackamadoodle3000 Aug 11 '17 at 15:00
-
1`psutil.sensors_battery()` returns null sometimes? is there an alternative? – Lidor shimoni Dec 30 '20 at 11:46