max
max

Reputation: 52293

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

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

Upvotes: 26

Views: 12218

Answers (2)

Jindra Helcl
Jindra Helcl

Reputation: 3736

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)

Upvotes: 9

phzx_munki
phzx_munki

Reputation: 1055

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

Upvotes: 21

Related Questions