Redirecting stderr in Python: Using contextlib.redirect_stderr

Redirecting stderr in Python: Using contextlib.redirect_stderr

Want stderr in your Python program to go somewhere else? Use redirect_stderr in contextlib:

import sys
from contextlib import redirect_stderr
with redirect_stderr(open('yikes.txt', 'w')):
   print('hi on screen')
   print('danger!', file=sys.stderr)