Python allows users to redirect standard input through stdin and standard output through
stdout toward different destinations such as files, strings, and custom streams. The redirection capability of standard input and standard output is important to capture printed output and read file-based input instead of keyboard entry and log console output for debugging.
Redirecting Standard Output (stdout)
The standard output content displayed in console windows can be redirected to a file connection through the
sys.stdout module. Saving printed data is made possible using this method.
import sys
# Redirect stdout to a file
with open("output.txt", "w") as file:
sys.stdout = file
print("This will be written to output.txt")
# Reset stdout to default
sys.stdout = sys.__stdout__
print("This is printed on the console")
Redirecting Standard Input (stdin)
The input that comes from the keyboard becomes available through stdin, but programmers can redirect it to read from a file instead. Such redirection enables scripts to receive input automatically from specified files.
import sys
# Redirect stdin to read from a file
with open("input.txt", "r") as file:
sys.stdin = file
user_input = input() # Reads the first line from input.txt
print(f"Read from file: {user_input}")
# Reset stdin to default
sys.stdin = sys.__stdin__
Redirecting Using contextlib.redirect_stdout and
contextlib.redirect_stderr The contextlib module allows temporary redirection functions through its
redirect_stdout and redirect_stderr features.
import sys
import contextlib
with open("log.txt", "w") as file:
with contextlib.redirect_stdout(file):
print("This goes to log.txt")
The formal methods have widespread usage in logging systems alongside automation and testing and debugging tasks to determine data processing locations.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Python allows users to redirect standard input through stdin and standard output through
stdouttoward different destinations such as files, strings, and custom streams. The redirection capability of standard input and standard output is important to capture printed output and read file-based input instead of keyboard entry and log console output for debugging.Redirecting Standard Output (stdout)
The standard output content displayed in console windows can be redirected to a file connection through the
sys.stdoutmodule. Saving printed data is made possible using this method.Redirecting Standard Input (stdin)
The input that comes from the keyboard becomes available through stdin, but programmers can redirect it to read from a file instead. Such redirection enables scripts to receive input automatically from specified files.
Redirecting Using
contextlib.redirect_stdoutandcontextlib.redirect_stderrThe
contextlibmodule allows temporary redirection functions through itsredirect_stdoutandredirect_stderrfeatures.The formal methods have widespread usage in logging systems alongside automation and testing and debugging tasks to determine data processing locations.