I am programming a connect 4 genetic algorithm, and I want to save certain games to a text file. When I tried doing this I was having severe formatting issues and I managed to recreate the error in the following code:
def write():
with open("saved_games.txt", "a") as text_file:
text_file.write("c")
with open("saved_games.txt", "a") as text_file:
text_file.write("a")
text_file.write("b")
write()
text_file.write("d")
the textfile looks like this after the program is run:
cabd
. I expected it to say abcd
. It is clear that for some reason the function which is defined but not called before a and b are written is somehow activating and writing to the text file before it is called.
I don't know if this would help solve the problem, but the file must be in append mode because I have to be able to add multiple games to it without overwriting other stuff as with write mode.
How can I fix this issue, and make the program write abcd to the file?