How to get battery percentage with python? [duplic

2019-02-26 01:39发布

问题:

This question already has an answer here:

  • Getting Battery Capacity Windows with Python 2 answers

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

回答1:

Try this:

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

Library link is here. And check out this