Skip to content
Related Articles

Related Articles

Improve Article
Python script to shows Laptop Battery Percentage
  • Last Updated : 29 Aug, 2020

psutil is a cross-platform library for retrieving information on running processes and system utilization(CPU, memory, disks, networks, sensors) in Python. The Python script below can be run in both Windows and Linux. Install psutil in windows by :

pip install psutil

Install psutil in Linux by:

sudo apt-get install gcc python3-dev
sudo pip3 install psutil



# python script showing battery details
import psutil
# function returning time in hh:mm:ss
def convertTime(seconds):
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    return "%d:%02d:%02d" % (hours, minutes, seconds)
# returns a tuple
battery = psutil.sensors_battery()
print("Battery percentage : ", battery.percent)
print("Power plugged in : ", battery.power_plugged)
# converting seconds to hh:mm:ss
print("Battery left : ", convertTime(battery.secsleft))


Battery percentage :  57
Power plugged in :  False
Battery left :  1:58:32


psutil.sensors.battery() returns a named tuple consisting of following values. If no battery is installed or metrics can’t be determined None is returned.

  • percent: Power left in percentage.
  • secsleft: Approx seconds left before the power runs out. It is set to psutil.POWER_TIME_UNLIMITED if it is on charging. If this value can’t be determined it is set to psutil.POWER_TIME_UNKNOWN .
  • power_plugged: True if power is plugged in, False if it isn’t charging or None if it can’t be determined.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :