psutil
module?The 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
methodThe 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. True
indicates power cabled is plugged. False
indicates power cabled is not plugged.psutil.sensors_battery()
It returns a tuple.
The method has no parameters.
import psutilbattery_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,))
psutil
module.sensors_battery()
method and store it in battery_status
.percent
, secsleft
, and power_plugged
to the console.