Read STDIN as bytes rather than a string

Benjamin Kaplan benjamin.kaplan at case.edu
Mon Jun 18 19:18:55 EDT 2012


On Mon, Jun 18, 2012 at 4:13 PM, Jason Friedman <jason at powerpull.net> wrote:
> I tried this:
>
> Python 3.2.2 (default, Feb 24 2012, 20:07:04)
> [GCC 4.6.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import sys
>>>> import io
>>>> fh = io.open(sys.stdin)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: invalid file: <_io.TextIOWrapper name='<stdin>' mode='r'
> encoding='UTF-8'>
>>>> fh = io.open(sys.stdin, "rb")
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: invalid file: <_io.TextIOWrapper name='<stdin>' mode='r'
> encoding='UTF-8'>
> --

You want to read from sys.stdin.buffer



More information about the Python-list mailing list