4.Write a python program to get information about power-supply.
import psutil
def get_power_info():
# Get power status
power_status = psutil.sensors_battery()
if power_status is None:
print("Power status: Running on ac power .")
else:
percent = power_status.percent
power_plugged = power_status.power_plugged
seconds_left = power_status.secsleft
if power_plugged:
power_plugged_str = "Plugged in"
else:
power_plugged_str = "Not plugged in"
print("Battery percentage:", percent)
print("Power status:", power_plugged_str)
if seconds_left is not None:
print("Time left (seconds):", seconds_left)
else:
print("Time left: Unknown")
if __name__ == "__main__":
get_power_info()
Output 4:
Power status: Running on ac power .
( on laptop)
Battery percentage : 90%
Power status : Plugged in