Write text to file with disabling buffer in Python3

Robin Dong 2018-12-13 14:27

In Python2 era, we could use these code to write the file without buffer:

file = open('my.txt', 'w', 0)
file.write('hello')

But in Python3 we can only write binary file by disabling buffer:

file = open('my.txt', 'wb', buffering = 0)
file.write('hello'.encode('utf-8'))

The only way to write text file without buffering is:

file = open('my.txt', 'w')
file.write('hello')
file.flush()

Adding ‘flush()’ everywhere is a terrible experience for a programmer who need to migrate his code from Python2 to Python3. I really want to know: what’s in Python3’s developers mind ?

[返回] [原文链接]