So I am trying to get live update of the battery power status, that is if the laptop is plugged in and charging or if it is running on battery power. I am using psutil library in python to retrieve the required data but the data I am getting back is limited to one time retrieval that is it is not updating while the program run.
import psutil
import keyboard
battery = psutil.sensors_battery()
print("Starting Program...")
while True:
status = battery.power_plugged
print(status)
if keyboard.is_pressed("q"):
break
elif(status == True):
print("Charging...")
elif(status == False):
print("Discharging...")
print("Closing Program...")
Here I am either getting only "true" and "Charging..." or "false" and "Discharging..." irrespective of the change in status. So how can I get around this, any suggestion would be helpful.
You need to get updated status in the while
as follows:
import psutil
import keyboard
import time
print("Starting Program...")
while True:
battery = psutil.sensors_battery()
status = battery.power_plugged
print(status)
if keyboard.is_pressed("q"):
break
elif(status == True):
print("Charging...")
elif(status == False):
print("Discharging...")
time.sleep(1)
print("Closing Program...")