[Tutor] Capturing stdout/stderror from linked c++ program

Douglas Eck doug@idsia.ch
Mon Mar 10 15:42:05 2003


Hi Alan,

Thanks for the response.  This is a good trick to know about, but it 
didn't work.  It does capture the output from "print" in the python gui 
but it doesn't capture
the output from "cout" in the linked c++ file.  The output  from "cout" 
still goes to the terminal that spawned the python gui. And it is not 
copied into the file.

Cheers,
Doug

>In that case can't you just redirect stdout/stderr to a file
>using the usual 
>
>sys.stdout = foo
>
>technique? foo could even be dynamically allocvated or passed 
>in as an argument in sys.argv....
>
>  
>
>>from dse import neuralnet     %my neural net python interface
>>    
>>
>import sys
>oldout = sys.stdout   # so we can restore later...
>sys.stdout = file(sys.argv[1]) # or whatever
>
>  
>
>>neuralnet.init("params.txt")
>>neuralnet.run()          #%here is where the stdout stuff happens.
>>    
>>
>
># Hopefully now to a new file?
>
>  
>
>>a=neuralnet.getActs()
>>    
>>
>
>sys.stdout = oldout
>
>
>
>Does that work?
>
>Alan g.
>Author of the Learn to Program website
>http://www.freenetpages.co.uk/hp/alan.gauld/
>
>  
>