How often does python flush to a file?

后端 未结 5 1864
礼貌的吻别
礼貌的吻别 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:29

    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.'
        f.write(s)
        f.write('\n')
        f.flush()
        # some other work
        f.write('done\n')
        f.flush()
    

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

提交回复
热议问题