0
votes

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

3 Answers

1
votes

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()

1
votes

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
0
votes

code correct: ping 127.0.0.1 | python pipetest.py

thank Andris