Python 电池 AI - 如何仅在电池连接或断开时记录
Python Battery AI - How to log only if battery is connected or disconnected
我正在使人工智能电池监视器看起来像 iOS13 并且我需要仅在用户连接或断开充电器插头时记录电池 percentage/hour/plugged。
我尝试做类似的事情:
if str(plugged) == "True":
log_file.write(current_info + "\r\n")
elif str(plugged) == "False"
log_file.write(current_info + "\r\n")
但脚本不会停止循环 "True"
这是我的代码的主要功能
log_file = open("activity_log.txt", "w")
while True:
battery = psutil.sensors_battery()
# Check if charger is plugged in or not
plugged = battery.power_plugged
# Check for current battery percentage
percent = str(battery.percent)
# Check for the current system time
sys_time = datetime.datetime.now()
current_info = percent + " " + str(sys_time) + " " + str(plugged)
if str(plugged) == "True":
log_file.write(current_info + "\r\n")
log_file.close()
github 上的项目,如果您想测试或实施它:https://github.com/peterspbr/battery-ai
如果我没理解错你想在变量 plugged
为 True 时退出循环?需要考虑的是 Python 是一种字符串类型语言,这意味着它与 "True" 和 True.
不同
log_file = open("activity_log.txt", "w")
plugged = False
while not plugged:
battery = psutil.sensors_battery()
# Check if charger is plugged in or not
plugged = battery.power_plugged
# Check for current battery percentage
percent = str(battery.percent)
# Check for the current system time
sys_time = datetime.datetime.now()
current_info = percent + " " + str(sys_time) + " " + str(plugged)
if str(plugged) == "True":
log_file.write(current_info + "\r\n")
log_file.close()
PD:我假设变量 batery.power_plug
是 bool 类型。
我可能已经明白你想做什么了:你想在电池插头改变状态时记录信息。您遇到问题是因为您没有采取任何措施来跟踪电池是否已插入。试试这个:
was_plugged = battery.power_plugged
while True:
...
if battery.power_plugged != was_plugged:
log_file.write(current_info + "\r\n")
was_plugged = battery.power_plugged
请学习更多关于 Python 基本类型的教程。很难遵循检查值的间接方法:将布尔值转换为文本,然后检查结果字符串:
if str(plugged) == "True":
您只需要直接布尔测试:
if plugged:
我正在使人工智能电池监视器看起来像 iOS13 并且我需要仅在用户连接或断开充电器插头时记录电池 percentage/hour/plugged。
我尝试做类似的事情:
if str(plugged) == "True":
log_file.write(current_info + "\r\n")
elif str(plugged) == "False"
log_file.write(current_info + "\r\n")
但脚本不会停止循环 "True"
这是我的代码的主要功能
log_file = open("activity_log.txt", "w")
while True:
battery = psutil.sensors_battery()
# Check if charger is plugged in or not
plugged = battery.power_plugged
# Check for current battery percentage
percent = str(battery.percent)
# Check for the current system time
sys_time = datetime.datetime.now()
current_info = percent + " " + str(sys_time) + " " + str(plugged)
if str(plugged) == "True":
log_file.write(current_info + "\r\n")
log_file.close()
github 上的项目,如果您想测试或实施它:https://github.com/peterspbr/battery-ai
如果我没理解错你想在变量 plugged
为 True 时退出循环?需要考虑的是 Python 是一种字符串类型语言,这意味着它与 "True" 和 True.
log_file = open("activity_log.txt", "w")
plugged = False
while not plugged:
battery = psutil.sensors_battery()
# Check if charger is plugged in or not
plugged = battery.power_plugged
# Check for current battery percentage
percent = str(battery.percent)
# Check for the current system time
sys_time = datetime.datetime.now()
current_info = percent + " " + str(sys_time) + " " + str(plugged)
if str(plugged) == "True":
log_file.write(current_info + "\r\n")
log_file.close()
PD:我假设变量 batery.power_plug
是 bool 类型。
我可能已经明白你想做什么了:你想在电池插头改变状态时记录信息。您遇到问题是因为您没有采取任何措施来跟踪电池是否已插入。试试这个:
was_plugged = battery.power_plugged
while True:
...
if battery.power_plugged != was_plugged:
log_file.write(current_info + "\r\n")
was_plugged = battery.power_plugged
请学习更多关于 Python 基本类型的教程。很难遵循检查值的间接方法:将布尔值转换为文本,然后检查结果字符串:
if str(plugged) == "True":
您只需要直接布尔测试:
if plugged: