0

I have a script that keeps on dumping some values to the screen. Lets call this script 1

I need the values that gets dumped to the screen to be processed by another python script.Lets call this script2. I Script1 cant be stopped

I currently use python 2.2 so it would be great if someone could provide an example without using subprocess

script 1 looks something like this

import sys
import os
i = 0
while 1:
    print i
    sys.stdout.write(str(i)+"\n")
        i = i + 1

I need to write script2 such that every output generated by script1 is processed by script2

-- edit --

I need it to run on a legacy implementation ie no power shell just the command prompt available in Windows XP

1
  • 2
    What operating system are you using? Commented Apr 5, 2011 at 14:41

2 Answers 2

7

I'm not going to say anything about code design here, because I really don't know what you are working on.

But for script2.py:

import sys
for line in sys.stdin:
    print "From stdin:", line

And then run (at least on unix/linux, if you are on windows, google "piping in powershell"):

python script1.py | python script2.py

Or did you want to do it within one python script? You could re-pipe sys.stdout, by simply replacing it with a class that has all the necessary methods (write, etc.).

Sign up to request clarification or add additional context in comments.

Comments

2

I guess you want to process line by line:

# script2.py
import sys

for line in sys.stdin:
     print line # do something here with line

[edit] Finally, you simply pipe them:

python script1.py | python script2.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.