AppDividend
Latest Code Tutorials

Python Print to Stderr Tutorial with Example

0

Python sys module provides file-like objects that represent stdinstdout, and stderr.  The sys module helps us to access the variables controlled by the interpreter. To use any of these objects, we need to import the sys module first.

import sys

Let’s understand the terms stdin, stdout, and stderr objects in detail.

What is Standard Input

In Python, the stdin object is used for all interactive input (including calls to input() function). The stdin file object handles a user program that is used to get information from the user. The sys.stdin object can be used to get the input from a command line directly. It is used for standard input.

import sys

for line in sys.stdin:
    if 'q' == line.rstrip():
        break
    print(f'Your Input : {line}')

print("You exited the program")

Output

kgb was dogging me
Your Input : kgb was dogging me

q
Exit

What is Standard Output

The stdout object is used for the output of print() and expression statements and the prompts of input(). It writes standard information to this object. The output is returned via the Standard output (stdout).

The sys.stdout.write() function prints the number of letters within the text too when used in interactive mode. Unlike print, sys.stdout.write doesn’t switch to a new line after one text is displayed.

import sys

sys.stdout.write('yello, this is homer')

Output

yello, this is homer

What is Standard Error

The stderr is used to write error information to this file-handle. The interpreter’s own prompts and their error messages go to the stderr object. The stderr() function prints Exceptions and Error messages.

import sys

stdout_file = sys.stdout
stderr_file = sys.stderr

s_list = ['Simpson!', 'Yello', 'from Homer']

for y_t in s_list:
    # Prints to stdout
    stdout_file.write(y_t + '\n')
    # Tries to add an Integer with string. Raises an exception
    try:
        y_t = y_t + 100
    # Catch all exceptions
    except:
        stderr_file.write('Exception Occurred!\n')

Output

Simpson!
Exception Occurred!
Yello
Exception Occurred!
from Homer
Exception Occurred!

Python Print to Stderr

To print the error message. you can use the stderr file object in Python.

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 to stderr.

You can also refer to the much easier and flexible than the 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 the same as the print() function in Python.

That is for Python Print to Stderr tutorial.

Leave A Reply

Your email address will not be published.

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