Reading binary data from stdin

Is it possible to read stdin as binary data in Python 2.6? If so, how?

I can see in the Python 3.1 documentation that this is fairly simple, but the possibilities for this do not seem to exist in 2.6.

If the methods described in 3.1 are not available, is there a way to close stdin and reopen in binary mode?

Update

Just to be clear, I use the β€œtype” in the MS-DOS shell to pass the contents of the binary into my python code. As I understand it, this should be the equivalent of the "cat" Unix command. But when I check this, I always get one byte less than the expected file size.

Update # 2

First of all, thanks for all the answers. I am slowly working on a real, useful solution. In the end, I'm still trying to create a standalone JAR file that runs my Python code, automatically passing through all the command line arguments that are not corrupted.

The reason I go along the Java / JAR / Jython route is because one of my main external libraries is only available as a Java JAR. But, unfortunately, I started my work as Python. It might have been easier to convert my code to Java some time ago, but since this material was supposed to be compatible, I decided that I would try to go through it and prove that it can be done.

In case someone wondered, this is also related to the question that I asked a few days ago.

Packaging and Deploying Jython from Eclipse

This question was answered by question .

So, I will try to update my original question with some notes about what I have understood so far.

+23
python
May 17 '10 at 16:34
source share
6 answers

Use the -u command line switch to force Python 2 to treat stdin, stdout, and stderr as binary unbuffered streams.

 C:> type mydoc.txt | python.exe -u myscript.py 
+12
May 17 '10 at 19:06
source share

From the docs (see here ):

Standard streams are in text mode by default. To write or read a binary data file to them, use a binary buffer. For example, to write a byte to stdout, use sys.stdout.buffer.write(b'abc') .

But, as in the accepted answer, calling python with -u is another parameter that causes stdin, stdout and stderr to be completely unbuffered. See the python (1) man page for more details.

For more information on buffering text, see the io documentation and use sys.stdin.detach() to disable buffering from Python.

+21
Jan 31 '11 at 10:26
source share

Here is the final version for Linux / Windows Python 2/3 compatible code to read data from stdin without corruption:

 import sys PY3K = sys.version_info >= (3, 0) if PY3K: source = sys.stdin.buffer else: # Python 2 on Windows opens sys.stdin in text mode, and # binary data that read from it becomes corrupted on \r\n if sys.platform == "win32": # set sys.stdin to binary mode import os, msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) source = sys.stdin b = source.read() 
+13
Aug 14 '16 at 5:05
source share

If you still need ... This is a simple test that I used to read a binary file containing the character 0x1A between

 import os, sys, msvcrt msvcrt.setmode (sys.stdin.fileno(), os.O_BINARY) s = sys.stdin.read() print len (s) 

My test file details:

 0x23, 0x1A, 0x45 

Without setting stdin to binary mode, this test prints 1 as soon as it treats 0x1A as EOF. Of course, it only works with windows, because it depends on the msvcrt module.

+9
Nov 12 '10 at 1:31
source share

You can perform unbuffered reads with:

os.read(0, bytes_to_read)

where 0 is the file descriptor for standard input

0
Apr 15 '19 at 17:36
source share
 import sys data = sys.stdin.read(10) # Read 10 bytes from stdin 

If you need to interpret binary data, use the struct module.

-2
May 17 '10 at 16:57
source share



All Articles