Redirect output

abcd codecraig at
Tue Mar 28 20:45:06 CEST 2006

I have a program which is written in C and interfaced with python via
Swig.  However, the function I call prints stuff out to the console.  I
would like to capture what it is printing out.  I tried:

import MyCProg, sys

f = open("tmp.txt", "wb")
sys.stdout = f
sys.stdout = sys.__stdout__

print open("tmp.txt", "rb").read()

However, it just prints the data to the screen.  I tried redirecting
stderr as well.

other than modifying the C code and re-generating the interface via
Swig, any ideas?  just asking before i have to do that.

More information about the Python-list mailing list