I want to manipulate the output of a console trace tool in a python script. So I want to read each line that the tool writes to stdout and maybe do something with that line and print it to the screen.
I have this script:
import sys
for l in sys.stdin:
print l
and I start the trace tool like this:
qspy -c COM7 | my_script.py
the problem is when I hit enter the first time: nothing happens. When I hit enter again, I see all the output that was created by the tool but it also quits (the trace-tool quits on any character). What am I doing wrong?
i'm on windows.
EDIT:
I've tried all the suggestions, with little success:
first of all i created this little script to produce some output on stdout:
import time
while 1:
time.sleep(1)
print "test"
this does not work at all (no output):
import sys
for line in sys.stdin:
print line,
sys.stdout.flush()
this one also doesnt do anything:
import fileinput, sys
for line in fileinput.input():
print line,
sys.stdout.flush()
this one works, but only with the test-script, not with qspy:
import sys
for line in iter(sys.stdin.readline, ''):
print line,
sys.stdout.flush()
i did call the scripts like this:
test.py | python -u my_script.py
and did also:
set PYTHONUNBUFFERED=1