Creating io.BufferedReader from sys.stdin in Python2

How can I create a BufferedReader object from a standard file object like sys.stdin or what do you get from 'open'?

(Reference: I need the peek () method, which standard file objects do not work with. Any suggestions to solve this problem are also welcome.)

I would have expected this to work, but it is not:

>>> import sys >>> import io >>> io.BufferedReader(sys.stdin) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'file' object has no attribute 'readable' 

(This is Python 2.7)

Yes, it turned out, at least for everything that has a file descriptor.

 stream = sys.stdin, or open(...), etc. reader = io.open(stream.fileno(), mode='rb', closefd=False) 
+22
python bufferedreader
May 19 '11 at 21:39
source share
1 answer

I also searched for the same code for the same reason (using peek) some time ago. And it works:

 reader = io.open(sys.stdin.fileno()) 
+12
Nov 02
source share
— -



All Articles