Python Print to Stderr

To print the error message, you can use the “stderr file object” in Python. The stderr() function prints Exceptions and Error messages.

import sys

def print_to_stderr(*a): 
 
  # Here a is the array holding the objects 
  # passed as the arguement of the function 
  print(*a, file = sys.stderr) 
 
 print_to_stderr("That's an error")

Output

That's an error

As you can see that we have printed it for stderr.

You can also refer to the much more accessible and flexible than other methods.

from __future__ import print_function
import sys


def modified_print(*args, **kwargs):
  print(*args, file=sys.stderr, **kwargs)


modified_print("asian", "paints", "indigo", "paints", sep="||")

Output

asian||paints||indigo||paints

Here, the modified_print() function works like Python’s print() function.

That is for Python Print to Stderr.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.