I'm starting off with my RPI alarm clock, now I managed to make a button execute a shell-script that terminates my alarm. I'm looking of buying a toggling-switch now the script I'd really like is.
if pin = 1
then = "write to status.txt : awake
if pin = 0
then = "write to status.txt : sleeping
I can add the rules to start/stop my alarm scripts myself but on this one I really need some help.
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")
or
append_to_file("status.txt", ("awake\n" if pin else "sleeping\n"))
with open('status.txt', 'w') as status:
if pin == 1:
status.write('awake')
elif pin == 0:
status.write('sleeping')
Although if pin
could ever potentially be anything else you may want to avoid opening the file unnecessarily.
if pin in [0, 1]:
with open( …
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.