How to python read from stdin and write to file

Spread the love

How to python read from stdin and write to file

Welcome to python tutorials point, In this post we are see in detailed How to python read from stdin.  Before going to this post if you want to learn python tutorials for free then click on python tutorials point.
so Let’s start:
There are three methods for python read from stdin.
  • sys.stdin
  • input() built-in functions
  • fileinput.input() functions
python read from stdin-write in file
fig 01) python read from stdin()
1. Using sys.stdin python read from stdin
python sys stdin is used by the interpreters for standard inputs. Internally, it is calls the input() functions. The input strings is a appended with a newline characters (n) in the end. So, you can use therstrip() module functions to remove it.
Here is a simple programs to read user message from the standard inputs and processing it. The programs will terminate when the user enter “Exit” messages.

import sys

for line in sys.stdin:
if ‘Exit’ == line.rstrip():
print(f’Display Message from sys.stdin *****{line}*****’)
Display Message from sys.stdin *Tom
Display Message from sys.stdin *Jerry

2. Using input() function to python read from stdin :

We can also use input() function to python to read the standard inputs data. We can also display a message to the user.
Here is a very simple examples to read and process the standard inputs message in infinite loops, unless the user enter the Exit messages.

while True:
data = input(“Please enter the input message:n”)
if ‘Exit’ == data:
print(f’Display Message from input() *****{data}*****’)

Please enter the input message:
Display Message from input() *****TOM*****’
Please enter the input message:
Display Message from input() *****JERRY*****’
Please enter the input message:
This input() function doesn not append newlines characters to the user message.

3. Using fileinput module python read from stdin :

We are also use fileinput.input() functions to read from the standard inputs. This fileinput module provide utility function to the loop over standard inputs or a list of file. When we don not provide any argument to the input() functions, it reads arguments from the standard inputs.
This function work in the same way as a sys.stdin and add a newline characters to the end of the user-entered data to it.

import fileinput

for fileinput_line in fileinput.input():
if ‘Exit’ == fileinput_line.rstrip():
print(f’Display Message from fileinput.input() *****{data}*****’)

Display Message from fileinput.input() *****SACHIN*****
Display Message from fileinput.input() *****ROLEX*****
Tags:python read from stdin,python read from stdin and write to file,python read from stdin with timeout
For similar kind of post click on pythonslearning and if you like this post then please comment and share.

sachin Pagar

I am Mr. Sachin pagar Embedded software engineer, the founder of Sach Educational Support(Pythonslearning), a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

This Post Has One Comment

Leave a Reply