Skip to content

Instantly share code, notes, and snippets.

@jsyeo
Created July 20, 2012 07:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsyeo/3149284 to your computer and use it in GitHub Desktop.
Save jsyeo/3149284 to your computer and use it in GitHub Desktop.
Python Script To Print Current Laptop Battery Power
#!/usr/bin/python
def main():
power_now = open("/sys/class/power_supply/BAT0/energy_now", "r").readline()
power_full = open("/sys/class/power_supply/BAT0/energy_full", "r").readline()
print float(power_now)/float(power_full) * 100 , "%"
if __name__ == "__main__":
main()
@jose-almir
Copy link

Hi, on my system the energy_now file is called charge_now, I see you wrote this 7 years ago. Thanks for sharing.

@SeanBeagle
Copy link

Check and see if you have this file to get the percentage directly: /sys/class/power_supply/BAT0/capacity

@imvickykumar999
Copy link

i got this error...


FileNotFoundError Traceback (most recent call last)
in
----> 1 power_now = open("/sys/class/power_supply/BAT0/energy_now", "r").readline()
2 power_full = open("/sys/class/power_supply/BAT0/energy_full", "r").readline()
3 print (float(power_now)/float(power_full) * 100 , "%")

FileNotFoundError: [Errno 2] No such file or directory: '/sys/class/power_supply/BAT0/energy_now'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment