[Tutor] Redirecting output

Magnus Lyckå magnus@thinkware.se
Tue Jun 17 07:10:02 2003


Rick, instead of redefining sys.stdout, which might confuse the
hell out of someone else (or yourself half a year later) who for
instance places a print statement somewhere for debugging, just
use "f = sys.stdout" in the default case, and use "print >> f, ..."
in all places where you want to be able to redirect output.
Actually, it would probably be clever to use a clearer variable
name than 'f'.

   print error_message

is the same as

   import sys
   error_stream = sys.stdout
   print >> error_stream, error_message

At 19:10 2003-06-16 -0700, Rick Owen wrote:
>What I don't want to do (unless I have to) is
>
>if output:
>    print >> f, error_message
>else:
>    print error_message
>
>Is there a way to explicitly open sys.stdout so that it is assigned to f if no
>output file is specified.

You were really very close here, weren't you? :)


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language