[Tutor] input/output redirection

Javier Ruere javier at ruere.com.ar
Thu Sep 8 17:58:44 CEST 2005


Lane, Frank L wrote:
> Hi List,
>  
> I wanted to take the stdout of a process and redirect it to stdin of a
> python script, then after playing with the input in the script I want to
> send it back to stdout (all of this to be done while the original
> process runs happily along).  I can't seem to figure out the correct
> syntax if this is possible.
>  
> e.g.
>  
> C:\> runner.exe | script.py
> C:\> runner.exe > script.py  # this overwrites your script!:-)
>  
> e.g.
>  
> #!/usr/bin/env python
> import re
>  
> while 1:
>     packet = sys.stdin.read()

  In the previous line, all input is read which is not ideal. Depending on the input a single line or part of it should be read so that the script can start working before runner.exe finishes.
  Now, once you have a workable chunck of the input, you can happily process it and write it out.

>     if field in packet:
>         # change it and put it back on the command window and get the
> next bunch of stuff
>         sys.stdout.write()

  In the previous line, write is called with no arguments. It should recieve the string to output.

> I hope my question/intention is clear.

  The given code is not a working example. It would help if the code was more complete.

Javier



More information about the Tutor mailing list