Python write to file flush


Python file method flush() flushes the internal buffer, like stdio's fflush. This may be a no-op on some file-like objects.

Python automatically flushes the files when closing them. But you may want to flush the data before closing any file.


Following is the syntax for flush() method −



  • NA

Return Value

This method does not return any value.


The following example shows the usage of flush() method.


# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ",

# Here it does nothing, but you can call it with read operation.

# Close opend file

When we run above program, it produces following result −

Name of the file:  foo.txt


  1. How often does Python flush to a file?
  2. How often does Python flush to stdout?

I'm unsure about (1).

As for (2), I believe Python flushes to stdout after every new line. But, if you overload stdout to be to a file, does it flush as often?

Python write to file flush

asked Jul 2, 2010 at 16:30

Tim McJiltonTim McJilton

6,3146 gold badges24 silver badges27 bronze badges


For file operations, Python uses the operating system's default buffering unless you configure it do otherwise. You can specify a buffer size, unbuffered, or line buffered.

For example, the open function takes a buffer size argument.

"The optional buffering argument specifies the file’s desired buffer size:"

  • 0 means unbuffered,
  • 1 means line buffered,
  • any other positive value means use a buffer of (approximately) that size.
  • A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files.
  • If omitted, the system default is used.


bufsize = 0
f = open('file.txt', 'w', buffering=bufsize)


7,2343 gold badges36 silver badges66 bronze badges

answered Jul 2, 2010 at 19:02

Python write to file flush

Corey GoldbergCorey Goldberg

57.1k27 gold badges123 silver badges141 bronze badges


You can also force flush the buffer to a file programmatically with the flush() method.

with open('out.log', 'w+') as f:
    f.write('output is ')
    # some work
    s = 'OK.'
    # some other work

I have found this useful when tailing an output file with tail -f.

Martin Thoma

113k148 gold badges569 silver badges873 bronze badges

answered Mar 10, 2011 at 5:13


I don't know if this applies to python as well, but I think it depends on the operating system that you are running.

On Linux for example, output to terminal flushes the buffer on a newline, whereas for output to files it only flushes when the buffer is full (by default). This is because it is more efficient to flush the buffer fewer times, and the user is less likely to notice if the output is not flushed on a newline in a file.

You might be able to auto-flush the output if that is what you need.

EDIT: I think you would auto-flush in python this way (based from here)

#0 means there is no buffer, so all output
#will be auto-flushed
fsock = open('out.log', 'w', 0)
sys.stdout = fsock
#do whatever

answered Jul 2, 2010 at 16:37


6,0344 gold badges28 silver badges41 bronze badges

You can also check the default buffer size by calling the read only DEFAULT_BUFFER_SIZE attribute from io module.

import io

answered May 20, 2016 at 15:28

N RandhawaN Randhawa

8,0533 gold badges41 silver badges47 bronze badges


Here is another approach, up to the OP to choose which one he prefers.

When including the code below in the file before any other code, messages printed with print and any errors will no longer be logged to Ableton's Log.txt but to separate files on your disk:

import sys

path = "/Users/#username#"

errorLog = open(path + "/stderr.txt", "w", 1)
errorLog.write("---Starting Error Log---\n")
sys.stderr = errorLog
stdoutLog = open(path + "/stdout.txt", "w", 1)
stdoutLog.write("---Starting Standard Out Log---\n")
sys.stdout = stdoutLog

(for Mac, change #username# to the name of your user folder. On Windows the path to your user folder will have a different format)

When you open the files in a text editor that refreshes its content when the file on disk is changed (example for Mac: TextEdit does not but TextWrangler does), you will see the logs being updated in real-time.

Credits: this code was copied mostly from the liveAPI control surface scripts by Nathan Ramella

answered Jul 29, 2016 at 9:09


1,8763 gold badges19 silver badges28 bronze badges

How do you flush a file in Python?

File flush() method in Python. The flush() method in Python file handling clears the internal buffer of the file. In Python, files are automatically flushed while closing them. However, a programmer can flush a file before closing it by using the flush() method.

What does it mean to flush a file?

file. flush forces the data to be written out at that moment. This is hand when you know that it might be a while before you have more data to write out, but you want other processes to be able to view the data you've already written.

Which method is used to clear the buffer and write contents in buffer to the file this is how programmers can forcefully write to the files as when required?

Python File flush() Method.

What does flush true do in Python?

flush=False (yes, the default) will wait for the line to complete before printing it. flush=True will force our terminal to print it.

Tải thêm tài liệu liên quan đến bài viết Python write to file flush