[f2py] Redirecting output

Pearu Peterson pearu@cens.ioc.ee
Sun, 10 Nov 2002 11:04:32 +0200 (EET)


On Sat, 9 Nov 2002, Jim Scarborough wrote:

> I have a Fortran routine that generates plenty of output to stdout and
> stderr, and I would like to save the results to a file, though what I
> thought would be the usual technique doesn't work:
>  
> def _saveOutput(logName,function,logHeader="",*args,**kwargs): 
>    """Save stdout and stderr from running the given function to the 
>    given log file name.""" 
>    s=open(logName,'w') 
>    s.write('Start time: ' + time.asctime() + "\n") 
>    s.write(logHeader + "\n") 
>    sys.stdout=sys.stderr=s 
>    try: 
>       apply(function,args,kwargs) 
>    finally: 
>       print 'End time: ' + time.asctime() 
>       sys.stdout,sys.stderr=sys.__stdout__,sys.__stderr__ 
>       s.close() 
> # _saveOutput 
>  
> Other ideas?   

This is OT.

Other ideas:
1) Use
  python yourscript.py > logfile.log 2>&1
2) Use script:
  script logfile.log
  python yourscript.py
  ^D
3) os.ttyname(sys.stdout.fileno()) tells you where stdout and stderr are
directed. It is possible to read messages from pts devices,
e.g. ttysnoop does this. I suggest asking comp.lang.python how to do that
from Python. It seems that Python has all the required tools for that
(socket,termios,etc.)

HTH,
	Pearu