title: How to read line by line from stdin in python
body: <p>Everyone knows how to count the characters from STDIN in C. However, when I tried to do that in python3, I find it is a puzzle.  (counter.py)</p>
  
  <pre><code>import sys
  chrCounter = 0
  
  for line in sys.stdin.readline():
      chrCounter += len(line)
  
  print(chrCounter)
  </code></pre>
  
  <p>Then I try to test the program by</p>
  
  <pre><code>python3 counter.py &lt; counter.py
  </code></pre>
  
  <p>The answer is only the length of the first line "import sys". In fact, the program ONLY read the first line from the standard input, and discard the rest of them. </p>
  
  <p>It will be work if I take the place of sys.stdin.readline by sys.stdin.read() </p>
  
  <pre><code>import sys
  print(len(sys.stdin.read()))
  </code></pre>
  
  <p>However, it is obviously, that the program is NOT suitable for a large input. Please give me a elegant solution. Thank you! </p>