Maybe it is something obvious, but what is going on with this code?
import sys
myerr = file("myerr.txt", "w")
sys.stderr = myerr
try:
raise Exception, "some error"
finally:
myerr.close()
sys.stderr = sys.__stderr__
I would expect the error message to be written into "myerr.txt", instead
it is displayed on the console, on regular stderr (?) and "myerr.txt" is
empty. I guess I misunderstood something ...
Michele Simionato 2 1437
Michele Simionato wrote: Maybe it is something obvious, but what is going on with this code?
import sys myerr = file("myerr.txt", "w") sys.stderr = myerr try: raise Exception, "some error" finally: myerr.close() sys.stderr = sys.__stderr__
I would expect the error message to be written into "myerr.txt", instead it is displayed on the console, on regular stderr (?) and "myerr.txt" is empty. I guess I misunderstood something ...
Michele Simionato
Hi,
os.close(2)
os.dup2(myerr.fileno(), 2)
Works, but is dirty ...
HtH, Roland
Michele Simionato wrote: Maybe it is something obvious, but what is going on with this code?
import sys myerr = file("myerr.txt", "w") sys.stderr = myerr try: raise Exception, "some error" finally: myerr.close() sys.stderr = sys.__stderr__
I would expect the error message to be written into "myerr.txt", instead it is displayed on the console, on regular stderr (?) and "myerr.txt" is empty. I guess I misunderstood something ...
I'd say you have to handle the exception before the original stderr is
restored, e. g:
import sys
import traceback
myerr = file("myerr.txt", "w")
sys.stderr = myerr
try:
try:
raise Exception("some error")
except:
traceback.print_exc()
finally:
myerr.close()
sys.stderr = sys.__stderr__
As print_exc() allows you to specify a file parameter, you could of course
entirely drop the try ... finally.
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David Douard |
last post by:
Hi everybody,
let me explain by problem:
I am working on an application which consists in a C++ dll (numeric
computations) and a Python IHM (Python/Tk), which must run under Linux and
win32. My...
|
by: Laszlo Zsolt Nagy |
last post by:
Hello,
I have this code:
s = smtplib.SMTP()
s.set_debuglevel(1)
s.connect(host=smtp_host)
s.set_debuglevel(0)
log("Connected, sending e-mail")
sys.stdout.flush()
|
by: Fuzzyman |
last post by:
Hello,
I'm trying to redirect standard out in a single namespace.
I can replace sys.stdout with a custom object - but that affects all
namespaces.
There will be code running simultaneously...
|
by: praetor.michael |
last post by:
I have a DLL written in C that writes to stderr. I have a win32
console application that makes calls to the DLL. In the console app I
redirect stderr to a file using freopen. The problem I'm...
|
by: SamG |
last post by:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).
|
by: dmoran21 |
last post by:
Hi All, I am working on a program to take input from a txt file, do
some calculations, and then output the results to another txt file.
The program that I've written compiles fine for me, however,...
|
by: reubendb |
last post by:
Hello,
I have some function that output too much stuff to stderr when called,
and I have no control over the function itself. So I thought as a
workaround, I redirect stderr to /dev/null before...
|
by: Guillaume Dargaud |
last post by:
Hello all,
I have some error checking using the function 'perror', which writes
messages on stderr. I'd like to send all error messages to a file instead.
Is there some way to do this, short of...
|
by: Lincoln Yeoh |
last post by:
Hi,
I've just started to learn python (I've been using perl for some years).
How do I redirect ALL stderr stuff to syslog, even stderr from
external programs that don't explicitly change their...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |