0
0

Write text to file with disabling buffer in Python3

Robin Dong 发表于 2018年12月13日 14:27 | Hits: 409
Tag: develope | python

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 ?

原文链接: http://donghao.org/2018/12/13/write-text-to-file-with-disabling-buffer-in-python3/

0     0

评价列表(0)