code: def stderr_redirected(to=os.devnull):
      
      fd = sys.stderr.fileno()
  
      # assert that Python and C stdio write using the same file descriptor
      # assert libc.fileno(ctypes.c_void_p.in_dll(libc, "stderr")) == fd == 1
      
      def _redirect_stderr(to):
          sys.stderr.close()  # + implicit flush()
          os.dup2(to.fileno(), fd)  # fd writes to 'to' file
          sys.stderr = os.fdopen(fd, 'w')  # Python writes to fd
  
      with os.fdopen(os.dup(fd), 'w') as old_stderr:
          with open(to, 'w') as file:
              _redirect_stderr(to=file)
          try:
              yield  # allow code to be run with the redirected stderr
          finally:
              _redirect_stderr(to=old_stderr)
docs: import os
      
      with stderr_redirected(to=filename):
          print("from Python")
          os.system("echo non-Python applications are also supported")