psutil (Python system and process utilities) module is a Python package that retrieves information on ongoing processes and system usage (CPU, memory, storage, network, and sensors). It is mostly used for system monitoring, profiling, restricting process resources, and process management.
The module can be installed via
pip as follows:
pip install psutil
sensors_battery method is used to return the battery status information with the following attributes:
percent: The percentage of battery/power left in the system.
secsleft: A rough approximation of how long the battery will last. This value is in seconds.
power_plugged: This is a boolean value indicating whether the power cable is plugged or not.
Trueindicates power cabled is plugged.
Falseindicates power cabled is not plugged.
It returns a tuple.
The method has no parameters.
import psutil battery_status = psutil.sensors_battery() print(battery_status) print("Percentage of battery: %s %" % (battery_status.percent,)) print("Approx time remaining: %s seconds" % (battery_status.secsleft,)) print("Is power cable connected: %s" % (battery_status.power_plugged,))
sensors_battery()method and store it in
power_pluggedto the console.
View all Courses