Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read STDIN as bytes rather than a string

1,528 views
Skip to first unread message

Jason Friedman

unread,
Jun 18, 2012, 7:13:40 PM6/18/12
to python-list
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'>

Benjamin Kaplan

unread,
Jun 18, 2012, 7:18:55 PM6/18/12
to pytho...@python.org
> --

You want to read from sys.stdin.buffer

Christian Heimes

unread,
Jun 18, 2012, 7:35:31 PM6/18/12
to pytho...@python.org
Am 19.06.2012 01:13, schrieb Jason Friedman:
> I tried this:

sys.stdin wraps a buffered reader which itself wraps a raw file reader.

>>> sys.stdin
<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
>>> sys.stdin.buffer
<_io.BufferedReader name='<stdin>'>
>>> sys.stdin.buffer.raw
<_io.FileIO name='<stdin>' mode='rb'>

You should read from sys.stdin.buffer unless you really need the bare
metal.

Christian

Jason Friedman

unread,
Jun 18, 2012, 7:49:37 PM6/18/12
to pytho...@python.org
> sys.stdin wraps a buffered reader which itself wraps a raw file reader.
>
>>>> sys.stdin
> <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
>>>> sys.stdin.buffer
> <_io.BufferedReader name='<stdin>'>
>>>> sys.stdin.buffer.raw
> <_io.FileIO name='<stdin>' mode='rb'>
>
> You should read from sys.stdin.buffer unless you really need the bare
> metal.
>

Thank you, and just to close the loop:

$ cat ~/my-input.py
#!/opt/python/bin/python3
import sys
print(sys.stdin.buffer.read())

$ echo hello | ~/my-input.py
b'hello\n'

Jason Friedman

unread,
Jun 18, 2012, 7:53:29 PM6/18/12
to pytho...@python.org
Which leads me to another question ... how can I debug these things?

$ echo 'hello' | python3 -m pdb ~/my-input.py
> /home/jason/my-input.py(2)<module>()
-> import sys
(Pdb) *** NameError: name 'hello' is not defined
0 new messages