user2302718 user2302718 - 2 months ago 5x
Python Question

Python, write text in TXT file

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.

with open('status.txt', 'w') as status:
  if pin == 1:
  elif pin == 0:

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