Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: File IO - Disable incremental flush

Tags:

python

file-io

Kind of the opposite of this question.

Is there a way to tell Python "Do not write to disk until I tell you to." (by closing or flushing the file)? I'm writing to a file on the network, and would rather write the entire file at once.

In the meantime, I'm writing to a StringIO buffer, and then writing that to the disk at the end.

like image 455
Jason Coon Avatar asked Mar 02 '26 04:03

Jason Coon


1 Answers

No, a glance at the python manual does not indicate an option to set the buffer size to infinity.

Your current solution is basically the same concept.

You could use Alex's idea, but I would hazard against it for the following reasons:

  1. The buffer size on open is limited to 2^31-1 or 2 gigs. Any larger will result in "OverflowError: long int too large to convert to int"
  2. It doesn't seem to work:

    a = open("blah.txt", "w", 2 ** 31 - 1)
    for i in xrange(10000): 
        a.write("a")
    

Open up the file without closing python, and you will see the text

like image 109
Unknown Avatar answered Mar 03 '26 17:03

Unknown