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?
In Python 3, if you want to read binary data from stdin , you need to use the buffer attribute:
stdin
buffer
import sys data = sys.stdin.buffer.read()
In Python 2, sys.stdin.read() already returns a byte string; no need to use buffer .
sys.stdin.read()