1

The following code I got from http://forums.devshed.com/python-programming-11/redirect-stdout-stderr-file-500952.html which tells how to redirect a stderr to a file. I tried it but the error message is not getting saved to the file. While I am not using

sys.stderr = file_err

the error is displayed in the idle terminal, when I am assigning file_err to sys.stderr the error is not displayed in the idle terminal, and it is not being copied to the file_name.log

import sys

original_stderr = sys.stderr
file_err = open('file_name.log', 'w') # I tried with .txt also
sys.stderr = file_err
print(list[file]) # Used to create a NameError
sys.stderr = original_stderr
file_err.close()

Am I supposed to write it to the file or is there an error in the program?

jww
  • 97,681
  • 90
  • 411
  • 885
Jeril
  • 7,858
  • 3
  • 52
  • 69
  • 1
    This code snippet prints a Traceback error in the `file_name.log` on my machine. What happens at yours ? What do you expect to happen ? How do you run it ? – nos Nov 03 '15 at 13:25
  • Possible duplicate of [Redirect the standard error output to /dev/null in python](http://stackoverflow.com/questions/32327073/redirect-the-standard-error-output-to-dev-null-in-python) – skyking Nov 03 '15 at 13:36
  • What's wrong with redirecting stderr from your shell ? And if you want logging, there's a dedicated package in the stdlib that has much more to offer... – bruno desthuilliers Nov 03 '15 at 13:37
  • I only have one question: Did you run the code in Python Shell? – Remi Guan Nov 03 '15 at 13:43
  • @nos I am just learning the working of standard streams, and sys.stdout was working fine. So i was trying with sys.stderr. I just want the error message to be copied to a file. I am working with python 3.4. – Jeril Nov 05 '15 at 03:09
  • @user2825570 You have since added that you use the idle terminal, so that's probably why. idle hijacks stderr in a way that probably interfers with this code. If you save your code to an file open an normal command line/console, and run "python yourfile.py", it will work as you except. – nos Nov 05 '15 at 12:11

1 Answers1

3

Your code works fine (I mean: the redirection to stderr.). Note however than from python3.5 you can use contextlib.redirect_stderr's context manager to do this:

from contextlib import redirect_stderr

with open('filename.log', 'w') as stderr, redirect_stderr(stderr):
    # errors from here are logged to the file.

Note:

  • You don't have to explicitly call close.
  • You don't have to explicitly save the old stderr and restore it at the end.

By the way: instead of saving the old stderr value you could simply use sys.__stderr__.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • 2
    Sorry but the op's code doesn't "work fine" - everything after the `print(list[file])` line will be ignored, since the exception is not handled. – bruno desthuilliers Nov 03 '15 at 13:35
  • @brunodesthuilliers what I meant is that the *redirection* works fine. The fact that the program ends prematurely is an other matter... AFAIK it is *expected*, since if you catch the error nothing is written to stderr.. – Bakuriu Nov 03 '15 at 14:01
  • this might well be a problem if the OP expects anything to happen after the exception is raised ;) – bruno desthuilliers Nov 03 '15 at 16:14
  • @brunodesthuilliers I am getting the following error while running your code: 'ImportError: cannot import name 'redirect_stderr'' – Jeril Nov 05 '15 at 03:01
  • @user2825570 do you have python **3.5**? That's when `redirect_stderr` was introduced. – Bakuriu Nov 05 '15 at 05:45
  • @Bakuriu The error is being displayed in the shell; whereas it is not getting saved in the file. I installed python 3.5. #redirect_stdout# is working fine, but not redirect_stderr – Jeril Nov 07 '15 at 06:29
  • @user2825570 I cannot reproduce. In fact if you look at the [source code](https://hg.python.org/cpython/file/3.5/Lib/contextlib.py#l196) you can see that `redirect_stderr` and `redirect_stdout` are implemented in exactly in the same way, so if one works the other must work too. Are you sure that the error is happening inside the `with` block? Could you edit your question including the new program you are testing? – Bakuriu Nov 07 '15 at 07:22
  • @Bakuriu could you please refer this [link]http://stackoverflow.com/questions/33579238/python-sys-stderr-not-getting-saved-to-txt-or-log/33580815#33580815 – Jeril Nov 07 '15 at 09:02
  • @user2825570 Using `with` with `redirect_stderr` already handles flushing and closing the stream. You may have that problem in the original code if the original `stderr` is buffered. – Bakuriu Nov 07 '15 at 09:35