Python By Example

stdin, stdout and stderr functions in System module

by Jiffin Joachim Gomez on 24th February 2017

This is a simple Python script to familiarize you with the system module (sys).

Python system module is something cool if you are really interested in python. As of now, I am only showing how you can use stdin, stdout and stderr functions. You can do a lot of cool stuffs using system module. The stdin function can be used to get input data from the python shell. The stdout function is used for outputs and stderr is used to show error messages.

#Import System module
import sys

print("Enter a number")
#input is read using stdin
x=int(sys.stdin.readline())
print(x)

#output printed using stdout
sys.stdout.write('This is stdout text\n')

#Error message is shown using stderr
sys.stderr.write('This is a stderror\n')

Program Output


Enter a number
10
10
This is stdout text
This is a stderror
>>> 

Post a comment

Comments

Nothing yet..be the first to share wisdom.