0

I was looking in google lot of example, but none work, I print to a file that passes through an outlet pipe ms-dos, but this throws me an error as if my file could not read sys.stdin, I put the code:

import sys
line = sys.stdin
for l in line.read():
   print l

and ms-dos I write the following:

ping 127.0.0.1 | pipetest.py

console above shows me that I have mistake in the line of "for" and shows this:

IOError: [Errno 9] Bad file descriptor

I use python2.7, and windows.

3 Answers 3

1

Instead of

ping 127.0.0.1 | pipetest.py

try

ping 127.0.0.1 | python pipetest.py

Also consider the other suggestion, you probably don't need .read()

Sign up to request clarification or add additional context in comments.

3 Comments

Python piping on Windows: Why does this not work? indicates that it might fix EOFError but OP has different IOError(EBADF) error.
@J.F. Sebastian Windows 7 + Python 2.7.3 gives me exactly the same IOError: [Errno 9] Bad file descriptor error what the OP had. The problem you linked really results in an EOFError.
yes, I was incorrect. Both EOFError and IOError(errno 9) errors are possible (the link is from the accepted answer to the question I've linked) so it might provide a solution for this case too).
1

This works:

import sys
lines = sys.stdin
for l in lines:
   print l

You might run into buffering issues though, because of how Python iterates on files. If you want to read each line right away, you should use readline() instead:

import sys
lines = sys.stdin
for l in iter(lines.readline, ''):
    print l

2 Comments

it doesn't fix "Bad file descriptor" error. btw, you could use for line in iter(sys.stdin.readline, ''): print line, instead of the while loop.
Edited, thanks. I'm not getting "bad file descriptor" here on Windows 7, not sure what would do that.
0

code correct: ping 127.0.0.1 | python pipetest.py

thank Andris

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.