Q:

When are files updated?

I have a set of code which runs in an infinite loop, cancelled through terminal with control C. In this code I use the code json.dumps(dictionary,outfile)
I have noticed that this does not actually put the data into the file until I have used control C to terminate the process. Why does the file not update until after the program has terminated?

A:

Anthony Rossi is basically right, you need to flush the data using outfile.flush(). But why is that so?
json.dump expects a "a .write()-supporting file-like object", see here. Somewhere in your code, you have used open to get your outfile. If we have a look at the documentation for open, we can read the following:

buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size in bytes of a fixed-size chunk buffer. When no buffering argument is given, the default buffering policy works as follows:

Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device’s “block size” and falling back on io.DEFAULT_BUFFER_SIZE. On many systems, the buffer will typically be 4096 or 8192 bytes long.

“Interactive” text files (files for which isatty() returns True) use line buffering. Other text files use the policy described above for binary files.

I guess you haven't specified the buffering parameter and your data is smaller than 4 / 8 kb. Therefore, the write is buffered and not directly written to the file.
When you kill your program using Ctrl+C, the outfile is closed and flushes the data to your file.
To fix this, simply put outfile.flush() after your json.dump() as Anthony Rossi suggested.

