Setting output of a program to pipe in Python 2.6.6
Question: I was wondering how I would redirect the output of a program I would run with
subprocess.Popen to the program I am using. For example, if I'm executing a
script from Python ImageProcessing.py in.jpeg out.jpeg, I would like to
instead do some extra processing in the higher level script; meaning that I
would like the python code to replace out.jpeg with a pipe or some other sort
of communication. (Also, yes I know it would be better to import in most
cases, in this case I rather not).

Thank you in advance!


Answer: You can use the `stdout=subprocess.PIPE` argument to `Popen`. The stdout of
the subprocess is a normal file. Example:

    
    
    import subprocess
    
    p = subprocess.Popen(["/bin/cat", "hello.txt"], stdout=subprocess.PIPE)
    
    for line in p.stdout:
        print("PIPE OUT [%s]" % line)
    

