[Tutor] Telnetlib
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sun, 4 Aug 2002 20:34:07 -0700 (PDT)
> > Also, when you set the telnet debug level, all debug messages go by
> > default to sys.stdout. Is there a way to redirect to a file? I can't
> > find this in the documentation. Perl's Telnet module allows to do this
Hi Llazarre,
Yes, there's a way of redirecting standard error. One way is to
temporarily set 'sys.stderr' out to another file, like this:
###
>>> sys.stderr = open("/home/dyoo/redirected_stderr.txt", "w")
>>> 1/0
>>>
###
Notice that no error pops up here, even though we did something bad in
dividing by zero. When we open up that file, we'll see our missing
output:
###
>>> sys.stderr.close()
>>> f = open("/home/dyoo/redirected_stderr.txt")
>>> f.read()
'Traceback (most recent call last):\n File "<stdin>", line 1, in
?\nZeroDivisionError: integer division or modulo by zero\n'
###
It's not a good thing to leave 'sys.stderr' hanging in a script, so we
often save the old sys.stderr, temporarily redirect it, and then set it
back once we're done.
Hope this makes sense!