title: In python, how to check the end of standard input streams (sys.stdin) and do something special on that
body: <p>I want to do something like:</p>
  <pre><code>for line in sys.stdin:
      do_something()
      if is **END OF StdIn**:
          do_something_special()
  </code></pre>
  <p>After a few tries, for now I am doing this:</p>
  <pre><code>while True:
      try:
          line = sys.stdin.next()
          print line,
      except StopIteration:
          print 'EOF!'
          break
  </code></pre>
  <p>Or with this:</p>
  <pre><code>while True:
      line = sys.stdin.readline()
      if not line:
          print 'EOF!'
          break
      print line,
  </code></pre>
  <p>I think both above ways are very similar. I want to know is there a more elegant (pythonic) way to do this?</p>
  <hr />
  <h2>Early failed tries:</h2>
  <p>I first tried to catch the <code>StopIteration</code> from inside or outside of a <code>for</code> loop, but I soon realize that since the <code>StopIteration</code> exception is <strong>build into</strong> <code>for</code> loop itself, both following code snippet didn't work.</p>
  <pre><code>try:
      for line in sys.stdin:
          print line,
  except StopIteration:
      print 'EOF'
  </code></pre>
  <p>or</p>
  <pre><code>for line in sys.stdin:
      try:
          print line,
      except StopIteration:
          print 'EOF'
  </code></pre>