How often does python flush to a file?

后端 未结 5 1874
礼貌的吻别
礼貌的吻别 2020-11-22 04:33
  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 flush

5条回答
  •  攒了一身酷
    2020-11-22 05:35

    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)
    

提交回复
热议问题