python read from stdin-write to file

python read from stdin :

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():
break
print(f'Display Message from sys.stdin *****{line}*****')
print("Final")
Output:
Tom

Display Message from sys.stdin *Tom

Jerry
Display Message from sys.stdin *Jerry
Exit
Final

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:
break
print(f'Display Message from input() *****{data}*****')
print("END")
Output:
Please enter the input message:
TOM
Display Message from input() *****TOM*****'
Please enter the input message:
JERRY
Display Message from input() *****JERRY*****'
Please enter the input message:
EXIT
END
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():
break
print(f'Display Message from fileinput.input() *****{data}*****')
print("END")
Output:
SACHIN
Display Message from fileinput.input() *****SACHIN*****
*****
ROLEX
Display Message from fileinput.input() *****ROLEX*****
*****
END
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.
BEST OF LUCK!!!!
Previous
Next Post »

comparison python vs r vs sas vs spss

python  vs r vs sas vs spss : Welcome everyone, Today we will see comparison between  r vs python vs sas vs spss . So Let's start : ...

Get Free Crash Course Subscription Valid Only For 10000 Subscriber.

Enter your email address:

Delivered by FeedBurner