[SciPy-user] optimize.fmin_tnc - how to catch its output?

Marek Wojciechowski mwojc@p.lodz...
Thu Jul 24 09:52:27 CDT 2008


Robert Kern wrote:

> On Mon, Jul 21, 2008 at 17:56, Stéfan van der Walt <stefan@sun.ac.za>
> wrote:
>> Hi Marek
>>
>> 2008/7/21 Marek Wojciechowski <mwojc@p.lodz.pl>:
>>> Hallo!
>>> I tried to catch fmin_tnc output to a file in the following way:
>>>
>>>    file = open('messages', 'w')
>>>    stdout = sys.stdout
>>>    stderr = sys.stderr
>>>    sys.stdout = file
>>>    sys.stderr = file
>>>    res = optimize.fmin_tnc(..., messages=1)
>>>    file.close()
>>>    sys.stdout = stdout
>>>    sys.stderr = stderr
>>>
>>> But this does not work. I realized that fmin_tnc calls C routine which
>>> probably uses its own stdout, stderr. Is there a way to catch its
>>> output?
>>
>> I'm afraid you are right.  The quickest way I can think of is to grab
>> it using standard unix pipes:
>>
>> python optimise_script.py 2>&1 | cat > out.txt
> 
> There is a way to redirect it to a (temporary) file from within
> Python. You can then read this file. I have an old and
> not-recently-tested implementation over here:
> 
>   http://www.enthought.com/~rkern/cgi-bin/hgwebdir.cgi/redir/
> 
Hi!
Thanks for that! It works very nice. Tested with fmin_tnc:

    import redirfile as redir
    rout = redir.Redirector(redir.STDOUT)
    rerr = redir.Redirector(redir.STDERR)
    rout.start()
    rerr.start()
    res = optimize.fmin_tnc(..., messages=1)
    out = rout.stop()
    err = rerr.stop()

Greetings,
-- 
Marek Wojciechowski



More information about the SciPy-user mailing list