Using stdin, stdout, and stderr in Python

Standard input (stdin), output (stdout) and error (stderr) data are associated with command line executions within shells like the command prompt in Windows or bash in Linux. The standards can be used within a Python script by first importing the sys module. Let’s pretend our command line has the form1:

$ [input] | python script.py > out.log 2> err.log

In this example, the string [input] is the output of a command generating text output to stdout, | is the pipe operator directing the input to Python, > out.log directs Python’s stdout (the output of the script) to the file out.log, and 2> err.log directs Python’s stderr to the file err.log.

We can use these pipeline tools in our own Python script, like this:

import sys
for line in sys.stdin:
	# Process stdin
print("[stdout output]")  # Print statements go to stdout
print("[stderr output]", file=sys.stderr)  # (Python Ver. 3) print to stderr instead of stdout
print >> sys.stderr, "[stderr output]"   # (Python Ver. 2) print to stderr instead of stdout

For example, let’s say we have a text file testInput.txt containing only the words Standard Input. We also have a Python script named script.py with the following code:

import sys
for line in sys.stdin:
	print(line + "to stdout") 
	print(line + "to sterr", file=sys.stderr)

Open up your shell (terminal), and enter this line:

$ cat testInput.txt | python script.py > out.log 2> err.log

Note: If you’re using the Windows command prompt, replace cat with type.

Once you run this code, a file called out.log will be created and it will contain:

Standard Input
to stdout

and err.log will contain:

Standard Input
to sterr

The cat command normally prints the contents of your file to your terminal. By using stdin, we pass cat command output to a Python script, send the output of our Python script (which would normally be printed to the screen) to a file, and send custom error messages to the stderr for our own custom error logging.

If you try to execute the above script.py script using Python version 2, you’ll actually get an error in your err.log file. This demonstrates what happens when you have a syntax error in your code. In this scenario, the standard error (stderr) will automatically be sent to err.log and it’ll contain a message explaining the error encountered, like this:

  File "script.py", line 4
    print(line + "to sterr", file=sys.stderr)
                                 ^
SyntaxError: invalid syntax

Hopefully this helped you figure out the differences between stdin, stdout, and stderr and how you can use them to enhance your Python program. If you found it helpful, subscribe below for more free Python lessons, then share this tutorial on Facebook and Twitter.


Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit

  1. Here we are using the standard notation that “$” indicates an input line to a shell, and that the character itself is not used.