1

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?

Maxijazz
  • 175
  • 1
  • 11

1 Answers1

2

This happens because writing to a file is an expensive operation, so python actually writes to a buffer. When the buffer fills up (it's slightly more complicated than that), or when the file handle is closed, the buffer is flushed to the filesystem.

In your case, this is what that looks like:

with open("saved_games.txt", "a") as text_file:
    text_file.write("a") # ----> text_file buffer = a
    text_file.write("b") # ----> text_file buffer = ab
    write()              # -------------> Open a new file handle, write c, close it

    # At this point, file contents: c
    text_file.write("d") # ----> text_file buffer = abd

# Exit the with block, write buffer contents to file
# File contents: cabd

You can manually flush() the file before calling the other function to force it to write ab to the file before it writes c.

def write():
    with open("saved_games.txt", "a") as text_file2:
        text_file2.write("c")


with open("saved_games.txt", "a") as text_file1:
    text_file1.write("a") # ---> text_file1 buffer = a
    text_file1.write("b") # ---> text_file1 buffer = ab
    text_file1.flush()    # ---> text_file1 buffer cleared
    # File contents: ab
    write()  # ---> Open, write c, close file. File contents: abc
    text_file1.write("d") # ---> text_file1 buffer = d

# Exit with block. Write buffer to file
# File contents: abcd

The output is now:

abcd
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 1
    Thanks for the answer, and for explaining it to me! I'll be sure to accept it once StackOverflow lets me. – Maxijazz Jun 03 '21 at 15:03