Search code examples
pythonfileflush

How often does python flush to a file?


  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?


Solution

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

    http://docs.python.org/library/functions.html#open

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

    code:

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