Share icon

Below an example of a Python script that checks the battery status on a Windows system:

import psutil

battery = psutil.sensors_battery()
plugged = battery.power_plugged
percent = battery.percent

if plugged:
    status = "Plugged In"
    status = "Unplugged"

print(f"Battery Status: {status}")
print(f"Battery Percentage: {percent}%")

This script uses the psutil library, which provides system-related information, including battery status. You can install it using pip by running pip install psutil.

The psutil.sensors_battery() function retrieves the battery information, and battery.power_plugged indicates whether the system is plugged in or not. The battery.percent attribute gives the current battery percentage.

The script then checks the plugged variable to determine the battery status (plugged in or unplugged) and prints the status and battery percentage.

You can run this script on a Windows system with Python installed to get the battery status information.

Add new comment

Restricted HTML