Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47593 > unrolled thread

Re: Popen and reading stdout in windows

Started byDave Angel <davea@davea.name>
First post2013-06-10 15:46 -0400
Last post2013-06-10 15:46 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Popen and reading stdout in windows Dave Angel <davea@davea.name> - 2013-06-10 15:46 -0400

#47593 — Re: Popen and reading stdout in windows

FromDave Angel <davea@davea.name>
Date2013-06-10 15:46 -0400
SubjectRe: Popen and reading stdout in windows
Message-ID<mailman.2982.1370893598.3114.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web