Reading stdin as binary

I have code that opens and reads a file from a binary file.

with open (file, mode="rb") as myfile: message_string=myfile.read() myfile.close 

Now I need to do the same thing as reading from stdin. But I can not figure out how to read binary files.

The error says only byte strings.
Any suggestions?

+5
source share
1 answer

In Python 3, if you want to read binary data from stdin , you need to use the buffer attribute:

 import sys data = sys.stdin.buffer.read() 

In Python 2, sys.stdin.read() already returns a byte string; no need to use buffer .

+8
source

All Articles