How to Find Battery Percentage and Charging Status in Windows and Linux Using Python

In this article, we will use Python to calculate the Battery percentage(%).

The psutil library is used in this program to obtain Battery information.

psutil library in python:

The python system and process utilities are abbreviated as psutil.

In Python, the psutil Library is used to obtain information on active(ongoing) processes and system utilization information in, such as CPU, memory, and so on.

Because the psutil Library is not built-in, we must install it before we can use it.

Use the below command to install the psutil library

Installation

pip install psutil

sensors_battery() function:

To obtain information about the Battery, we will utilize the sensors battery() method present in the psutil Library.

Syntax:

 psutil.sensors_battery()

The sensors_battery() method returns battery status information as a named tuple. The following information is included in the battery status:

percent:- It represents the percentage of battery life remaining(battery left).

secsleft:- This is the battery’s capacity in seconds.

power_plugged:- This variable indicates the status of the battery’s charging. If the battery is charging, it is True; otherwise, it is False.

If the battery status cannot be found, the value of power plugged is set to None.

If the battery cannot be found, the psutil.sensors_battery() method gives None.

Find Battery Percentage and Charging Status in Windows and Linux in Python

Approach:

  • Import psutil module using the import keyword
  • Get the named tuple with battery information using the sensors_battery() function of the psutil module.
  • Print the battery percentage by applying the percent attribute on the above battery information
  • Using batteryinformation.power_plugged, we can determine the charging status.
  • Check if the batteryinformation.power_plugged is equal to True using the if conditional statement
  • If it is true, then print “The battery of the system is Charging!!!”
  • Else check if the batteryinformation.power_plugged is equal to False using the elif conditional statement
  • If the condition is true, then print “The battery of the system is NOT Charging/Discharging”
  • The Exit of the Program.

Below is the implementation:

# Import psutil module using the import keyword 
import psutil
# Get the named tuple with battery information using the sensors_battery() 
# function of the psutil module.
batteryinformation  = psutil.sensors_battery()
# Print the battery percentage by applying percent attribute on the above battery information 
print("The Battery percentage of the sytem  = ",batteryinformation.percent)
# Using batteryinformation.power_plugged, we can determine the charging status.
# Check if the batteryinformation.power_plugged is equal to True using the if conditional statement
if batteryinformation.power_plugged == True :
    # If it is true, then print "The battery of the system is Charging!!!"
    print("The battery of the system is Charging!!!")
# Else check if the batteryinformation.power_plugged is equal to False using the elif conditional statement
elif batteryinformation.power_plugged == False:
    # If the condition is true, then print "The battery of the system is NOT Charging/Discharging"
    print("The battery of the system is NOT Charging/Discharging")

Output:

The Battery percentage of the sytem = 55
The battery of the system is Charging!!!

Execution Image:

showing battery percentage and charging status