0

I've been successful in being able to write lines to a txt file. My issue is, after running the program the first time, my information, when running it a second time, is deleted from those text files. Naturally, this makes my if statement in end_program() not work. Any ideas on how to permanently save information to a txt file in Python? Here is my code:

def start_program():
    state = ""
    income = ""
    I = ["" + ""]


    file = open("activation.txt", "w+")
    file.write("1")
    file.close()
    with open("information.txt", "w+") as file_1:
        I = [chk_state_1() + "\n" + chk_income()]
        file_1.writelines(I)

def end_program():
    with open("activation.txt","r+") as activate:
        if not activate.readline(0) == "activated":
            activate.write("activated")
            start_program()
        else:
            print("hello")

end_program()

Another odd issue, is when the program is ran it will put an "l" in front of activated in the activation.txt

Please help!!

  • By checking whether it exists? – Willem Van Onsem Nov 17 '17 at 14:37
  • use the append (`a`) mode of the `open()` method. – scharette Nov 17 '17 at 14:37
  • Use `a` for the append mode instead of `w+`. – Vasilis G. Nov 17 '17 at 14:37
  • Just tried append, this does not fix the problem of my "if-then-else" statement not identifying the string "activated" in activation.txt, thusly the program does not move on to displaying "help." Append did keep the information there, however, so 1/2 problems fixed. –  Nov 17 '17 at 14:42
  • Jean-Francois.... where is the duplicate question? I can't find anything on the Internet that answers this question????? –  Nov 17 '17 at 14:45
  • Jean-Francois, please remove the duplicate marking –  Nov 17 '17 at 14:48
  • THIS IS NOT A DUPLICATE QUESTION. WHY ARE YOU HINDERING MY PROGRESS???!?!?! –  Nov 17 '17 at 14:50

0 Answers0