What are Standard Input, Output, and Error Streams | Python

If someone asks us to give input to a program interactive or through typing,

we know what devices are we need for it like the keyboard, Mouse.

Similarly, in case someone states that the output that to displayed,

we know which device it will display right, undoubtedly that will be on the monitor.

So, we can reliably say that the Keyboard does the standard input device and the monitor does the standard output device.

Similarly, any error if happens then determines and also displayed on the monitor.

So, the monitor is also a standard error device.

The Standard input device stdin – reads from the keyboard

standard output device stdout – prints to the display and can redirect as conventional input

standard error device stderr – Same as stdout but commonly only for errors.

Having error output individually allows the user to distract the usual output to a file and still be capable to see error messages.

Certain standard devices are performed as files called standard streams.

In Python, we can use these standard stream files by using the sys module.

After importing, we can use these standard streams like stdin, stdout, and stderr in the same way as you use other files.

Interesting Standard Input, Output Devices as Files

If we import the sys module in our program then, sys.stdin.read() will let you read from the keyboard.

This is due to the keyboard is the regular input device connected to sys.stdin.

Similarly, sys.stdout.write() will let you write on the standard output device, the monitor.sys.stdin and

sys.stdout are standard input and standard output devices respectively do treat as files.

Thus the sys.stdin and sys.stdout are like files that are revealed by the Python when we start Python. The sys.stdin is regularly opened in read mode and sys.stdout is regularly opened in write mode.

The Following Example code fragment explains to us the interesting use of these functions.

It can print the contents of a file on the monitor without using print statement :


import sys
fh = open(r":\poem.txt")
line1 = fh.readline()
line2 = fh.readline()
sys.stderr.write("No errors occurred\n")


We work, we try to be better
No errors occurred

These statements would write on file/device associated with sys.stdout

which is the monitor and also we can see that stderr also displayed its head on monitor.

Python data files with statements:

python’s with statements for the files is very advantageous when we have two related operations which we would like to execute as a pair, with a block of code in between.

The syntax for using with statement is:


with open( <filename>, <filemode>) as <filehandle> : 
<file manipulation statements >

The perfect example for this is opening a file, handling the file, and then closing it :

with open(‘output.txt’, 'w) as f : 
f .write(‘Hi there!') 


The above “with” statement it will automatically close the file after the nested block of code.

The advantage of using a with statement means that it is guaranteed to close the file no matter how the nested block exits.

Even if an exception which is known as a runtime error occurs before the end of the block, the “with” statement will handle it, and close the file.

We will be happy to hear your thoughts

Leave a reply

Open chat
Scan the code
Can we help you?