Error messages when using the Console Module

P

peter

I am developing code using the effbot Console module, which gives
control over the Windows terminal settings. My problem is that while
this module redirects standard output to a new console window, it
seems to redirect standard error messages to a black hole. So when my
new code fails, I have no error messages to assist in diagnosing the
problem. At times I have been reduced to writing new code a line at a
time!

I'm sure there is an easy solution to this problem, but googling has
yet to reveal it. Can anyone help?

Peter
 
C

Christian Heimes

peter said:
I am developing code using the effbot Console module, which gives
control over the Windows terminal settings. My problem is that while
this module redirects standard output to a new console window, it
seems to redirect standard error messages to a black hole. So when my
new code fails, I have no error messages to assist in diagnosing the
problem. At times I have been reduced to writing new code a line at a
time!

Windows GUI apps don't have valid standard streams. stdin, stdout and
stderr aren't connected and their fileno is -1. I guess you have to
connect the streams manually when you create a console window. C code
can use freopen() but I don't know how to properly redirect the streams
from Python.

MSDN has probably some docs for you.

Christian
 
G

Gabriel Genellina

I am developing code using the effbot Console module, which gives
control over the Windows terminal settings. My problem is that while
this module redirects standard output to a new console window, it
seems to redirect standard error messages to a black hole. So when my
new code fails, I have no error messages to assist in diagnosing the
problem. At times I have been reduced to writing new code a line at a
time!

The Console module doesn't redirect standard output, AFAIK. You can do
that either from inside the script or when invoking it from the command
line:

import sys
sys.stdout = open("stdout.log","w")
sys.stderr = open("stderr.log","w")
print "This goes to stdout"
print >>sys.stderr, "This goes to stderr"

or:

python yourscript.py >stdout.log 2>stderr.log

To redirect all output to the same file, you can use

sys.stdout = sys.stderr = open("output.log","w")

or:
python yourscript.py >output.log 2>&1

The same applies to pythonw.exe too.
 
P

peter

import sys
sys.stdout = open("stdout.log","w")
sys.stderr = open("stderr.log","w")
print "This goes to stdout"
print >>sys.stderr, "This goes to stderr"

This did the trick. Thanks

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top