laptop's power state on ubuntu
How can I find out the power state(whether it's plugged to a power source, or not) of a laptop running on Ubuntu? I need to use that 开发者_运维百科info. to trigger a python script, so I want to know if there is a way to do this using Python?
On a sufficiently modern Ubuntu you might want to use DBus (via its Python binding) to query the DeviceKit daemon.
Update: actually, on modern Ubuntu installations DeviceKit-power is replace by UPower (but that's got its own, very similar DBus interface).
Ubuntu has support for the acpi protocol. By using the -b
flag, you can get battery info. One way you may want to use this is:
import os
os.system("acpi -b > batteryinfo.txt")
#code to parse batteryinfo.txt
Having to parse the batteryinfo.txt file may be a bit of a pain. In addition you will have to set up permissions because normally acpi
is only run as root.
精彩评论