C:\Users\Blake\Desktop>script.py < nums.txt
Blah
Blah
Blah
Traceback (most recent call last):
File "C:\Users\Blake\Desktop\script.py", line 10, in <module>
line = raw_input()
EOFError: EOF when reading a line
C:\Users\Blake\Desktop>
Blah
Blah
Blah
Traceback (most recent call last):
File "C:\Users\Blake\Desktop\script.py", line 10, in <module>
line = raw_input()
EOFError: EOF when reading a line
C:\Users\Blake\Desktop>
After scouring the internet to find the "correct" fix for this thinking "gee, once I find this it'll make a great blog post" I am sorry to report that, alas, I could not find one. So I give you the best "half-assed" fix:
while True:
try:
value = raw_input()
do_stuff(value) # next line was found
except (EOFError):
break #end of file reached
try:
value = raw_input()
do_stuff(value) # next line was found
except (EOFError):
break #end of file reached
Sadly this seems to be the proper usage of raw_input when dealing with the end of the stream of input from piped input. I guess the strongly-typed, unit-testing, check-for-every-null software developer in me cringes at the idea of using exceptions under normal usage circumstances. In pretty much every other development platform, using exceptions are reserved for when things Really Go Wrong.
Oh well, Python can't be perfect in every way.