[Tutor] redirect stdout and stderr?

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Thu, 20 Jul 2000 13:58:39 -0700 (PDT)

On Thu, 20 Jul 2000, Brian Wisti wrote:

> I am at a bit of a loss here.  What I'm trying seems simple enough: make my 
> application send stdout (debug) messages to one file, and stderr (error) 
> messages to a different file.

Yes, that should work fine.  Here's a small interpreter session that
demonstrates redirection:

>>> import sys
>>> sys.stdout = open("test.txt", 'a')
>>> print "Hello World!"
>>> print "This is a test!"
>>> sys.stdout = sys.__stdout__  # restore stdout back to normal
>>> print "Hello World!"
Hello World!
>>> print open("test.txt").read()
Hello World!
This is a test!

Your code looks perfectly fine; you're omitting the 'import sys' in your
code, but that should be in the front of your class definition.  
Otherwise, I can't seem to find anything that would cause a problem.  You
should definitely see a 'Starting server' line in your log.  Strange.

> Do I need to close and reopen these files any time the program has something 
> interesting to say?  That would be annoying, but not impossible.

No, you have it the right way.  They stay open till the file objects are

> Please forgive my ignorance.  I know very little about file handling, and 
> even less about file handling in Python.

Try the small example above, and make sure that it works.  After that...
hmm... I can't think of a good reason why it would do that.  Hopefully
someone else on the list might give better advice.

I have to ask a silly question: do you have enough hard drive space, and
that you're not over disk quota?

Good luck to you.