0

I would like to forward the output from one script to the another.

I try to do it like that:

./producer.py > ./consumer.py

Where procuder:

#!/usr/bin/env python3

import sys
import time

i=0
while True:
    print(i)
    i+=1
    time.sleep(1)

And consumer:

#!/usr/bin/env python3

import sys

f= open("test.txt","w+")

while True:
    line = sys.stdin.read()
    print(line)
    f.write(line)

I expect that producer will generate: 1 2 3 4 5 and this pipe will be saved in file test.txt by another script. Thank you for help.

0

3 Answers 3

1

Use a pipe | instead of > to forward output (stdout) from one process to another (as stdin). Also, beware that the data over the pipe is buffered, which may seem like nothing is happening when there is not a lot of data and the stream is never closed.

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

Comments

0

Consider using a queue with two threads instead. Here is a good basic guide. Here is another guide.

Comments

0

Try using subprocess and then save the data from output pipe to file. You do not need to execute the command outside.

from subprocess import PIPE,Popen

process = Popen(['python','test.py'],stderr=PIPE,stdout=PIPE)

output = process.stdout.readlines()

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.