Python 2.7 : Write to file instantly

后端 未结 4 1498
粉色の甜心
粉色の甜心 2020-11-27 05:03

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\")
ou         


        
相关标签:
4条回答
  • 2020-11-27 05:20

    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).

    0 讨论(0)
  • 2020-11-27 05:24

    Force it with the flush() function, add

    outputFile.flush()
    

    at the end of your code.

    0 讨论(0)
  • 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")
    
    0 讨论(0)
  • 2020-11-27 05:44

    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)
    
    0 讨论(0)
提交回复
热议问题