Standard Files (stdin, stdout, stderr) and Redirection
1. What are Standard Files?
Every program in an operating system automatically gets three default data streams:
• stdin – Standard Input
• stdout – Standard Output
• stderr – Standard Error
2. stdin (Standard Input)
stdin is the input stream used by a program.
Example: input() in Python reads from stdin.
Example:
x = input('Enter name: ')
print(x)
3. stdout (Standard Output)
stdout is the stream where a program writes normal output.
Example:
print('Hello World')
This prints to stdout.
4. stderr (Standard Error)
stderr is used to display error messages.
Example:
print(10/0) → this error is sent to stderr.
5. Redirection in Command Line
Redirection allows sending output to files instead of console.
Common redirection operators:
> – Redirect stdout (overwrite)
>> – Redirect stdout (append)
2> – Redirect stderr
&> – Redirect both stdout and stderr
Examples of Redirection
1. Save output to a file:
python [Link] > [Link]
2. Append output to a file:
python [Link] >> [Link]
3. Save only errors to a file:
python [Link] 2> [Link]
4. Save both output and error:
python [Link] &> all_output.txt