How To Print To stderr In Python?

The standard error or stderr is used to output errors and error messages in Linux systems. It is especially used in the bash shell to redirect errors and separate the errors from informational messages. Python provides different methods in order to print specific messages into the stderr. The most popular and easy way is using the print() method and provides the stderr as the output.

Print Standard Output or stdout with print() Method

First, start with a standard example where we will print to the standard output. Actually, the print() method prints the given data into the standard output or stdout.

print("This is standard output")

Even the print() method redirects or prints to the standard output or stdout we can also explicitly specify the standard output or stdout with the file parameter of the print() method.

import sys

print("This is standard output",file=sys.stdout)

Print Standard Error or stderr with print() Method

The print() method can be used to standard error or stderr. The file parameter should be set with the sys.stderr which is provided via the sys module. In the following examples, we will print the given string value or string variable into the stderr.

import sys


#Print string value to stderr
print("This is standard error",file=sys.stderr)


#Print string variable to stderr
errmessage = "This is standard error"
print(errmessage ,file=sys.stderr)


#Print list variable to stderr
errmessage = ["Error","Number",123]
print(errmessage ,file=sys.stderr)

The biggest advantage of using the print() method to print into standard error or stderr the print() method can convert provided data into the string. The data can be an integer, list, tuple, etc.

Print Standard Error or stderr with sys.write() Method

Python also provides the sys.stderr.write() method which can be used to print into the standard error or stderr. The write() method is less flexible than print() method in order to print into stderr because the write() method does not converts provided data automatically into the string type. So the provided data should be string type if not it should be converted properly. Also, the write() does not add a new line at the end of the data automatically. If we need the end of the line should be added explicitly with the "\n" characters.

import sys

#Print string value to stderr
sys.stderr.write("This is standard error\n")


#Print string variable to stderr
errmessage = "This is standard error\n"
sys.stderr.write(errmessage)

Create Special Print Function To Print Standard Error or stderr

If we will print messages and data into the stderr frequently and do not want to write related print() or write() method again and again with parameters we can create a special function which will use the print() or write() method in order to print into stderr. The special Error Printing Function named as print_err() but you can change it into whatever you want.

import sys

def print_err(err_message):
   print(err_message,file=sys.stderr)


#Print string value to stderr
print_err("This is standard error")


#Print string variable to stderr
errmessage = "This is standard error"
print_err(errmessage)


#Print list variable to stderr
errmessage = ["Error","Number",123]
print_err(errmessage)

Leave a Comment