문제

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.

도움이 되었습니까?

해결책 2

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( …

다른 팁

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"))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top