質問

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