- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python script to shows Laptop Battery Percentage
Monitoring the battery percentage of your laptop is essential to keep track of its power level and ensure uninterrupted usage. Whether you're working on the go or relying on your laptop for extended periods, having a Python script to display the battery percentage can be incredibly useful. In this blog post, we will explore how to create a Python script that shows the laptop battery percentage.
In the following sections, we will discuss the prerequisites for running the script, explore how to retrieve the battery information, and demonstrate how to display the battery percentage in a graphical user interface (GUI).
Prerequisites
Before we begin, there are a few prerequisites you need to have in place to successfully run the Python script for displaying the laptop battery percentage. Here's what you'll need −
Python − Make sure you have Python installed on your system. You can download the latest version of Python from the official Python website (https://www.python.org/) and follow the installation instructions specific to your operating system.
Operating System Compatibility − Keep in mind that the method for accessing battery information may vary depending on the operating system you're using. The script provided in this article is compatible with Windows, macOS, and Linux-based systems, but some adjustments may be necessary for specific OS versions or distributions.
Required Python Libraries − We will be using the psutil library to retrieve the battery information. Ensure that you have psutil installed on your system. You can install it using pip, the Python package installer, by running the following command in your terminal or command prompt:
pip install psutil
Once you have Python installed and the necessary prerequisites in place, you're ready to proceed with creating the Python script.
Creating the Python Script
To display the laptop battery percentage using Python, we'll use the psutil library, which provides an interface to retrieve system information, including battery-related data. Here's how you can create the Python script −
Import the necessary modules −
import psutil
Retrieve the battery information −
battery = psutil.sensors_battery()
Check if the battery is present −
if battery is None: print("No battery found.") exit()
Get the battery percentage −
percentage = battery.percent
Display the battery percentage −
print(f"Battery Percentage: {percentage}%") Run the script: if __name__ == "__main__": main()
The complete Python script to display the laptop battery percentage should look like this −
import psutil def main(): battery = psutil.sensors_battery() if battery is None: print("No battery found.") exit() percentage = battery.percent print(f"Battery Percentage: {percentage}%") if __name__ == "__main__": main()
Save the script with a meaningful name, such as "battery_percentage.py". Now, when you run this script, it will display the current battery percentage of your laptop.
Note − Keep in mind that the script may provide different results based on your laptop's battery status. Make sure your laptop is not connected to a power source to get an accurate reading.
In the next section, we'll discuss how to run the script and explore additional customization options.
Running the Script and Customization
To run the Python script that displays the laptop battery percentage, follow these steps −
Open a text editor and create a new file.
Copy the Python script provided in the previous section and paste it into the file.
Save the file with a meaningful name, such as "battery_percentage.py".
Now, you can run the script using one of the following methods −
Method 1: Command Line
Open a terminal or command prompt, navigate to the directory where the script is saved, and run the following command −
python battery_percentage.py
Method 2: Integrated Development Environment (IDE)
If you're using an IDE like PyCharm, Visual Studio Code, or IDLE, you can open the script file and execute it from within the IDE. Look for the "Run" or "Execute" option in the IDE's menu or toolbar.
Upon running the script, you should see the laptop's battery percentage displayed in the console or output window.
Customization Options
You can customize the script to enhance its functionality and make it more informative. Here are a few ideas −
Add a timestamp − Display the current date and time alongside the battery percentage to track the changes over time.
Set a threshold − Define a minimum acceptable battery percentage and show a warning message if the battery falls below that threshold.
Create a graphical user interface (GUI) − Instead of displaying the battery percentage in the console, build a GUI using libraries like Tkinter or PyQt to show the information in a window.
Conclusion
In this tutorial, we learned how to create a Python script to display the laptop battery percentage. We used the psutil library to retrieve system information and accessed the battery percentage using the psutil.sensors_battery() function. By incorporating this functionality into a script, we can conveniently monitor the battery status of our laptops.
We started by importing the necessary modules and defining a function to get the battery percentage. We then implemented a loop to continuously display the battery percentage in the console. Additionally, we discussed how to run the script and provided customization options to expand its features.