I have a program that whenever I press a button it writes a value to a text file. The next time the button is pressed it finds the string that was written last and calls the variable to do the opposite. This continuously is running until I shut it off. My issue is it doesn’t stop printing “Off”, which is the last string in my text file. When the program runs, Fine() prints the last string of the text file then it goes into the while loop. It doesn’t seem to recognize my if statements and just prints Fine() again without change. I don’t know why it keeps printing “Off” it could be a formating thing that I don’t know of, but I’ve looked and couldn’t find anything similar to it.
import time import os # this is the system that detects the last line. In order to seek the end of file, we have to open the file in binary mode. We seek the last 2 characters # since the file is likely to end with a newline. Then, keep reading a single byte until a newline character is found. And, finally, we use readline() to read the # final line, and decode() to convert from bytes to string. # Checker is a fall back system so if there is nothing in the file then it writes on(or the terms i am using at that time.) so that when I press the button again it starts the cycle. def Checker(): file = open("Store.txt").readlines() #print(file[0:2]) if file[0:2] != ['Onn', 'Off']: print("nothing is here...Restarting file with added Off") with open("Store.txt","w") as df: df.write("OnnOff") df.close() def Fine(): with open("Store.txt", "rb") as f: f.seek(-2, os.SEEK_END) while f.read(1) != b'n': f.seek(-2, os.SEEK_CUR) print(f.readline().decode()) def w_on(): print("The light is on") with open("Store.txt", "w") as on: on.write("Onn","On") on.close() def w_off(): print("this is off") with open ("Store.txt", "w") as off: off.write("offno","OFFF") off.close() #This checks the status of the text file and corrects it if the text inside is wrong. Checker() # this prints the current last line in the Store.txt file. Fine() #============================================================================================ #Here is the start of my Issue of it printing "Off" # 1==1 replaces the button.pressed() function while True: time.sleep(1) if 1==1: if Fine()==("On"): # the print function replaces the function for the servo moving. right here the off funtion would happen. print("The light is turning off") w_off() elif Fine()== ("Off"): # the print fucntion replaces the function for the serveo moving. right below, the on funtion would happen. print("The light is turning on") w_on() time.sleep(2)
It is this line:
So on your first execution, you have the file written with
Off with checker with
Off as your last statement in the file.
Now every time you call
Fine() in the while loop, the above statement prints the last line which is
Fine() call is returning
None (because your function doesn’t return anything), therefore your
elif don’t really execute except calling
Fine() to check its value. So nothing gets written to your file and you keep getting the last line which is still