gpt4 book ai didi

Python,在TXT文件中写入文本

转载 作者:太空宇宙 更新时间:2023-11-04 09:05:04 28 4
gpt4 key购买 nike

我从我的 RPI 闹钟开始,现在我设法让一个按钮执行一个 shell 脚本来终止我的闹钟。我想买一个拨动开关现在我真正喜欢的剧本是。

if pin = 1
then = "write to status.txt : awake
if pin = 0
then = "write to status.txt : sleeping

我可以自己添加规则来启动/停止我的警报脚本,但在这方面我真的需要一些帮助。

最佳答案

def append_to_file(fname, s):
with open(fname, "a") as outf:
outf.write(s)

if pin:
append_to_file("status.txt", "awake\n")
else:
append_to_file("status.txt", "sleeping\n")

append_to_file("status.txt", ("awake\n" if pin else "sleeping\n"))

关于Python,在TXT文件中写入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21661019/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com