redirect to > and to console ?

Gerhard Haering gerhard.haering at gmx.de
Fri Jul 26 05:33:07 EDT 2002


"Shagshag13" wrote:
> hello,
> 
> 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"

will work fine. I'd use the print>> form, because it has the advantage that it
annoys the Martellibot, among others ;-)

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20  A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))



More information about the Python-list mailing list