{"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

pip install psutil<\/pre>\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

 psutil.sensors_battery()<\/pre>\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

Find Battery Percentage and Charging Status in Windows and Linux in Python<\/h2>\n

Approach:<\/strong><\/p>\n