Popen and reading stdout in windows
Dave Angel
davea at davea.name
Mon Jun 10 15:46:24 EDT 2013
On 06/10/2013 02:37 PM, Joseph L. Casale wrote:
> I have a use where writing an interim file is not convenient and I was hoping to
> iterate through maybe 100k lines of output by a process as its generated or
> roughly anyways.
>
> Seems to be a common question on ST, and more easily solved in Linux.
> Anyone currently doing this with Python 2.7 in windows and can share some
> guidance?
>
You leave out an awful amount of detail. I have no idea what ST is, so
I'll have to guess your real problem.
You've got a process (myprog.exe) which generates a medium amount of
output to stdout, and you want to process that data in a python program
as it's being output, but without first writing it to a file.
If by process you meant grep, the answer would be as simple as
myprocess | grep parm1 parm2
But you want to write something (not called grep) in Python.
myprocess | python myfilter.py
The question is how to write myfilter.py
Answer is to use stdin as you would a file. it's already open for you,
and it'll get the data as it's being generated (plus or minus some
buffering). So you can simply do something like:
import sys
for index, line in enumerate(sys.stdin):
print index, line
This trivial "filter" adds a line number in front of every line. But
you could do anything there. And you might not need enumerate if you
don't care about the line number.
--
DaveA
More information about the Python-list
mailing list