Python 2.7 : Write to file instantly

I realized that when I write into a file using python it wait until the end of my Python file to execute it:

outputFile = open("./outputFile.txt","a")
outputFile.write("First")
print "Now you have 10sec to see that outputFile.txt is still the same as before"
time.sleep(10)
outputFile.write("Second")
print "Now if you look at outputFile.txt you will see 'First' and 'Second'"

How am I suppose to make python write instantly to the output file?


Solution 1:

You can use flush() or you can set the file object to be unbuffered.

Details on using that parameter for open() here.

So you would change your open call to -

outputFile = open("./outputFile.txt", "a", 0)

Solution 2:

Force it with the flush() function, add

outputFile.flush()

at the end of your code.

Solution 3:

As @RyPeck said you can use flush() or set the file object to be unbuffered. But note the following (from https://docs.python.org/2/library/stdtypes.html?highlight=file%20flush#file.flush):

Flush the internal buffer, like stdio‘s fflush().

Note flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior.

And a quote from man 3 fflush:

Note that fflush() only flushes the user-space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2).

Solution 4:

Just to combine all of the above answers into a set of useful utility functions because a key requirement of the OP (and myself!) is "because I don't want to write outputFile.flush() each time":

import os
import tempfile
import time


def write_now(filep, msg):
    """Write msg to the file given by filep, forcing the msg to be written to the filesystem immediately (now).

    Without this, if you write to files, and then execute programs
    that should read them, the files will not show up in the program
    on disk.
    """
    filep.write(msg)
    filep.flush()
    # The above call to flush is not enough to write it to disk *now*;
    # according to https://stackoverflow.com/a/41506739/257924 we must
    # also call fsync:
    os.fsync(filep)


def print_now(filep, msg):
    """Call write_now with msg plus a newline."""
    write_now(filep, msg + '\n')


# Example use with the with..as statement:
with tempfile.NamedTemporaryFile(prefix='some_prefix_here.', suffix='.log', dir='.', delete=False) as logf:
    print_now(logf, "this is a test1")
    time.sleep(20)
    print_now(logf, "this is a test2")