I want to pipe
output of my file using popen
, how can I do that?
test.py:
while True:
print"hello"
a.py :
import os
os.popen('python test.py')
I want to pipe the output using os.popen
.
how can i do the same?
This will print just the first line of output:
a.py:
import os
pipe = os.popen('python test.py')
a = pipe.readline()
print a
...and this will print all of them
import os
pipe = os.popen('python test.py')
while True:
a = pipe.readline()
print a
(I changed test.py to this, to make it easier to see what's going on:
#!/usr/bin/python
x = 0
while True:
x = x + 1
print "hello",x
)
First of all, os.popen() is deprecated, use the subprocess module instead.
You can use it like this:
from subprocess import Popen, PIPE
output = Popen(['command-to-run', 'some-argument'], stdout=PIPE)
print output.stdout.read()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With