Intercept and Tag STDOUT values

Wes S. MornIHATESPAMThdr at NOSPAMverizon.net
Wed Oct 13 05:55:12 CEST 2004


How can I redirect and tag sys.stdout values. I tried creating a class
module that saved the old stdout and replaced it, but I can't seem to figure
out how to do such.

Here is what I got...

class cStdOutRedirect :
 stdOld = ""
 def __main__(self) :
  stdOld = sys.stdout
  sys.stdout = self
  return True
 def write(self,str) :
  stdOld.write("REDIRECTED OUTPUT: " + str)
  return True
 def close(self) :
  sys.stdout = stdOld
  return True

then in the main code...

c = cStdOutRedirect()
print "hello world"
c.close()

I'm in a high school Python class and this is well beyond where the
instructor is now and he isn't too fond of answering advanced questions. I
have programmed in quite a few other languages, so I understand quite a bit
of Python.

-Wes





More information about the Python-list mailing list