Search code examples
pythonoperating-systemsubprocesspopen

Need to read from long running os command with Python 2.4


Firstly, I'm stuck with Python 2.4. This is a large enterprise environment and I'm unable to update to python 2.7 which would be my preference.

I need to read the output of some dtrace scripts that spit out data in intervals similar to iostat. (ie: iostat 5 100 # every 5 seconds, 100 count)

I'm playing around with Popen and Popen.communicate but it seems to slurp all the data at once and then print out in one large string.

I need to enter into a while loop and read the output 1 line at a time.

Can someone point me into the right direction for doing this?

Much thx.


Solution

  • import subprocess
    p = subprocess.Popen("some_long_command",stdout=subprocess.PIPE)
    for line in iter(p.stdout.readline, ""):
        print line
    

    I think at least ...