1

I tried to use this code for redirecting the error stream:

import sys
file = open("log", "w")
sys.stderr.write = file.write
variable     #for raising a NameError

I tried this too:

import sys
file = open("log", "w")
sys.stderr = file
variable

The file is created but even after raising the errors there's no data inside the file. It's empty. I think so this is a bug because I saw questions on stack overflow which give the same answer but the code just doesn't work for me. Also, I'm using python 3.10.0

Devyansh
  • 66
  • 5
  • Can you link that question?? –  Nov 23 '21 at 04:28
  • https://stackoverflow.com/questions/1956142/how-to-redirect-stderr-in-python @Avi – Devyansh Nov 23 '21 at 04:31
  • If you create a file like `script.py`, paste the second example and run it, you get exactly what you expected. You're likely doing something else in addition that's causing your issue. – Grismar Nov 23 '21 at 04:31
  • I don't understand what `variable` is doing there. What are you trying to do with that? –  Nov 23 '21 at 04:31
  • @Avi as mentioned in the code itself, it's for raising a NameError (I raise the NameError for testing whether the error has been redirected to a file) – Devyansh Nov 23 '21 at 04:33
  • Alright, I missed that comment. But the second is working fine for me. Is that the full code?? –  Nov 23 '21 at 04:35
  • @Grismar it did not work – Devyansh Nov 23 '21 at 04:36
  • @Avi yes it's the only thing – Devyansh Nov 23 '21 at 04:37
  • I am using python 3.9 and it's working just fine. –  Nov 23 '21 at 04:40
  • 1
    "it did not work" you say that, but clearly it *does* work for myself and others. Since I use Python (I have 3.10 as well, but it also works on previous version) in an otherwise clean environment, it must be something else in your environment. Either you're running it from some interpreter, in a non-standard console, etc. - but your code does exactly what you say you want it to do. Just stick the code in a `script.py` file and run it from the command line in a clean environment with `python script.py`; you'll need to provide more information about exactly what you're doing and how. – Grismar Nov 23 '21 at 04:46
  • You can try making a new environment, as suggested by @Grismar –  Nov 23 '21 at 04:53
  • ok so you both want me to use new environment, so do you mean I reinstall python? Grismar said he use's 3.10.0 and it worked for him so I suppose I should reinstall python too... – Devyansh Nov 23 '21 at 05:58
  • do you it run directly in system console/terminal? Some IDEs may redirect error to own file/stream to catch it and display in IDE. – furas Nov 23 '21 at 07:37
  • @furas I am currently running the code in python's IDLE interpreter – Devyansh Nov 24 '21 at 03:29
  • I tried it in `IDLE shell` and in `IDLE editor` and both save in file if I uses `sys.stderr = file` but not if I uses `sys.stderr.write = file.write`. I would always use `sys.stderr = file` because it may need also other function to run - ie. `flush()`, etc. On some systems file needs `close()` to save data on disk because system may not save it at once but keep data in buffer and wait for `close()`. I tested on Linux Mint. – furas Nov 24 '21 at 05:18
  • @furas, thx the same case is happening with me. I tried running ```sys.stderr = file``` in sublime text and the log was made. – Devyansh Nov 24 '21 at 15:37

0 Answers0