redirect to > and to console ?

Tim Hammerquist tim at vegeta.ath.cx
Fri Jul 26 06:40:29 EDT 2002


Gerhard Haering graced us by uttering:
> "Shagshag13" wrote:
>> i use many python script like that :
>> ./myscript.py file_in > file_out
>> 
>> i wish i would sometimes like to force output to be redirect to
>> console instead of file file_out, is there any way of doing that?
>> (for example to display errors message while file_in will contain
>> only data processed) [...]
> 
> Unix and Windows consoles have two different output streams: the
> standard output one and the error stream.
> 
> Standard output is accessible from Python as sys.stdout, the error
> stream as sys.stderr.
> 
> To write to the error stream, either one of
> 
>     sys.stderr.write("foobar\n")
> 
> and
>     
>     print>> sys.stderr, "foobar"

One thing that concerns me is how well do the COMMAND.COM/CMD.EXE shells
handle redirection?  I know that DOS used to support the standard '>'
redirection, but I don't remember any robust way of handling stderr on
the command line.

Various *nix (and cygwin, yes) shells can use:

  $ command > file_out 2> file_err

  $ command > file_both 2>&1

  $ command &> file_both

...and I think I heard CMD.EXE had slightly better support, but how does
Win32's native command line stream redirection really compare?

Tim Hammerquist
-- 
You can lead a boss to a decision, but you can't make him think.
    -- Simon, BOFH



More information about the Python-list mailing list