Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why text I/O must be buffered in python 3?

Tags:

Python 2 supported unbuffered text I/O.

The same approach doesn't work in python 3. Why was unbuffered text I/O disabled?

> import sys > sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) builtins.ValueError: can't have unbuffered text I/O 

The binary still works fine:

> sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) # works fine 
like image 539
max Avatar asked Nov 21 '14 18:11

max


People also ask

Why does Python buffer data before writing?

So, the purpose of buffering is to speed up your program.

What is output buffering Python?

Summary: Python output buffering is the process of storing the output of your code in buffer memory. Once the buffer is full, the output gets displayed on the standard output screen.


2 Answers

This is an open bug, issue # 17404 (last update 2013-03-13): http://bugs.python.org/issue17404

like image 82
phzx_munki Avatar answered Sep 19 '22 13:09

phzx_munki


For text files, if you want to use the buffering line-by-line, use open(..., buffering=1)

From python documentation:

1 to select line buffering (only usable in text mode)

like image 33
Jindra Helcl Avatar answered Sep 21 '22 13:09

Jindra Helcl