{"id":26936,"date":"2022-04-18T01:14:48","date_gmt":"2022-04-17T19:44:48","guid":{"rendered":"https:\/\/python-programs.com\/?p=26936"},"modified":"2022-04-18T01:14:48","modified_gmt":"2022-04-17T19:44:48","slug":"how-to-find-battery-percentage-and-charging-status-in-windows-and-linux-using-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-find-battery-percentage-and-charging-status-in-windows-and-linux-using-python\/","title":{"rendered":"How to Find Battery Percentage and Charging Status in Windows and Linux Using Python"},"content":{"rendered":"
In this article, we will use Python to calculate the Battery percentage(%).<\/p>\n
The psutil library is used in this program to obtain Battery information.<\/p>\n
psutil library in python:<\/strong><\/p>\n The python system and process utilities are abbreviated as psutil.<\/p>\n In Python, the psutil Library is used to obtain information on active(ongoing) processes and system utilization information in, such as CPU, memory, and so on.<\/p>\n Because the psutil Library is not built-in, we must install it before we can use it.<\/p>\n Use the below command to install the psutil library<\/p>\n Installation<\/strong><\/p>\n sensors_battery() function:<\/strong><\/p>\n To obtain information about the Battery, we will utilize the sensors battery() method present in the psutil Library.<\/p>\n Syntax:<\/strong><\/p>\n The sensors_battery() method returns battery status information as a named tuple. The following information is included in the battery status:<\/p>\n percent:-<\/strong> It represents the percentage of battery life remaining(battery left).<\/p>\n secsleft:-<\/strong> This is the battery’s capacity in seconds.<\/p>\n power_plugged:-<\/strong> This variable indicates the status of the battery’s charging. If the battery is charging, it is True; otherwise, it is False.<\/p>\n If the battery status cannot be found, the value of power plugged is set to None.<\/p>\n If the battery cannot be found, the psutil.sensors_battery() method gives None.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Execution Image:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In this article, we will use Python to calculate the Battery percentage(%). The psutil library is used in this program to obtain Battery information. psutil library in python: The python system and process utilities are abbreviated as psutil. In Python, the psutil Library is used to obtain information on active(ongoing) processes and system utilization information …<\/p>\npip install psutil<\/pre>\n
psutil.sensors_battery()<\/pre>\n
Find Battery Percentage and Charging Status in Windows and Linux in Python<\/h2>\n
\n
# Import psutil module using the import keyword \r\nimport psutil\r\n# Get the named tuple with battery information using the sensors_battery() \r\n# function of the psutil module.\r\nbatteryinformation = psutil.sensors_battery()\r\n# Print the battery percentage by applying percent attribute on the above battery information \r\nprint(\"The Battery percentage of the sytem = \",batteryinformation.percent)\r\n# Using batteryinformation.power_plugged, we can determine the charging status.\r\n# Check if the batteryinformation.power_plugged is equal to True using the if conditional statement\r\nif batteryinformation.power_plugged == True :\r\n # If it is true, then print \"The battery of the system is Charging!!!\"\r\n print(\"The battery of the system is Charging!!!\")\r\n# Else check if the batteryinformation.power_plugged is equal to False using the elif conditional statement\r\nelif batteryinformation.power_plugged == False:\r\n # If the condition is true, then print \"The battery of the system is NOT Charging\/Discharging\"\r\n print(\"The battery of the system is NOT Charging\/Discharging\")<\/pre>\n
The Battery percentage of the sytem = 55\r\nThe battery of the system is Charging!!!<\/pre>\n