battery_checker/battery_checker.py
2024-05-12 22:15:56 +09:00

32 lines
No EOL
806 B
Python

import xml.etree.ElementTree as ET
import subprocess
import os
def get(filename):
if os.path.isfile(filename):
os.remove(filename)
try:
detail = subprocess.run(["powercfg", "/batteryreport", "/output", filename, "/XML"], check=True)
return 0
except:
return 1
def parse(filename):
tree = ET.parse(filename)
root = tree.getroot()
capacity = {"design": int(root[2][0][7].text),
"current": int(root[2][0][8].text),
"cycle": int(root[2][0][9].text)}
return capacity
xmlfile = "battery.xml"
get_detail = get(xmlfile)
if get_detail == 0:
battery_about = parse(xmlfile)
print("電池の容量 : " + str(round(battery_about["current"] / battery_about["design"] * 100, 1)) + " %")
else:
print("エラー")