page

Apr 18, 2018

How do you read from stdin in Python?

How do you read from stdin in Python?

https://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python


Here's from Learning Python:
import sys
data = sys.stdin.readlines()
print "Counted", len(data), "lines."

On Unix, you could test it by doing something like:
% cat countlines.py | python countlines.py 
Counted 3 lines.
On Windows or DOS, you'd do:
C:\> type countlines.py | python countlines.py 
Counted 3 lines.

No comments:

Post a Comment