Writing to SdtErr

sburrious sburr at home.com
Mon Oct 22 18:24:10 EDT 2001


Michael Hudson <mwh at python.net> wrote in message news:<u8ze36bit.fsf at python.net>...
> "Mike Peat" <mike at unicorn-fs.com> writes:
> 
> > Can anyone tell me how to:

> > 2) Write (print) to the StdErr channel instead of StdOut?
> 
> print >> sys.stderr, "foo"

Don't let Alex Martelli catch you promoting the use of ">>."  :)

Two alternatives:

(1)
$ cat > stderrwrite.py
import sys
sys.stderr.write("Now go away,\n")
print "or I shall taunt you a second time!"

$ python stderrwrite.py > afile
Now go away,

$ cat afile
or I shall taunt you a second time!

(2)
$ cat > redirect.py
import sys
stdoutholder = sys.stdout
sys.stdout = sys.stderr
print "Your father was a hamster,"
sys.stdout = stdoutholder
print "and your mother smelled of elderberries!"

<03:17pm> ~
$ python redirect.py > afile
Your father was a hamster,

<03:18pm> ~
$ cat afile
and your mother smelled of elderberries!



More information about the Python-list mailing list