D
D
DVoropaev2020-06-16 22:50:44
linux
DVoropaev, 2020-06-16 22:50:44

How to redirect error output in python?

I have a python3 script which I am running on linux. Sometimes this script throws a bunch of errors and exceptions. I want all error and exception messages to be written to a file and the output of the script to remain clean. To do this, I do this:
myscript.py 2> errors.log
But I don't want to write "2> errors.log" every time, I want the script itself to be able to redirect the stream with errors.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2020-06-16
@DVoropaev

sys.stderr = open("/.../errors.log", "a")

R
Roman Mirilaczvili, 2020-06-17
@2ord

Wrap the program start in a shell script - it suggests itself in the first place.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question