Course contentsShow
← Python
Lesson 725 of 1,06011. Standard Library EssentialsFree lesson

Capturing Standard Input and Output Streams

Learners will redirect and process standard streams to facilitate piping data between Python scripts and other command-line tools effectively.

In this lesson, you will learn how to bridge your Python scripts with the command line by capturing and processing standard input (stdin) and output (stdout) streams.

The Power of Streams

In a terminal environment, programs are often connected like pipes in plumbing. Your script can receive data from another tool via sys.stdin and pass its results to the next tool via sys.stdout. Instead of hardcoding file paths, you can write "filter" scripts that process whatever data flows into them, making your code significantly more modular and reusable.

Processing Input Streams

To read data passed into your script from a pipe, you interact with sys.stdin. Because it is an iterable object, you can process incoming text line-by-line without loading the entire dataset into memory:

import sys

# This script acts as a filter: it converts input text to uppercase
for line in sys.stdin:
    sys.stdout.write(line.upper())

If you saved this as upper.py, you could run cat data.txt | python upper.py in your terminal. The cat command pushes the file content into your script, which processes it and prints it back to the console.

Redirecting Output

While print() naturally sends output to sys.stdout, there are times when you need explicit control—for example, when you want to send status messages to the error stream (sys.stderr) to keep them separate from the primary data output. This ensures that if a user redirects your script's output to a file (e.g., python script.py > results.txt), the error messages still appear on their screen:

import sys

# Main output goes to the pipe
sys.stdout.write("Processed data line\n")

# Error messages stay visible in the terminal
sys.stderr.write("Warning: Minor issue detected.\n")

By leveraging sys.stdin and sys.stdout, you transform your Python scripts into versatile command-line utilities that play well with existing tools like grep, sort, and awk.

Key Takeaway: By treating sys.stdin and sys.stdout as file-like objects, you can build powerful command-line filters that integrate seamlessly into complex shell workflows.