问题描述:

While trying to write a program to certain specifications, which include requiring the program to read information from stdin and print output to stdout, I have run into an unexpected problem. The problem is that sys.stdin.readline() doesn't finish reading the line provided to it on stdin.

Minimal Example

#!/bin/python

from sys import stdin

myline = stdin.readline().split()

The line to be read is found at http://pastebin.com/k3DsD5f6

On my MacBook Pro running 10.8.5 and Python 2.7, the program hangs indefinitely at the line involving the readline() command.

The exact terminal output I get (assuming I name this snippet of code so_error.py) is as follows.

python so_error.py

30887 92778 36916 47794 38336 85387 60493 16650 41422 2363 90028 68691 20060 97764 13927 80541 83427 89173 55737 5212 95369 2568 56430 65783 21531 22863 65124 74068 3136 13930 79803 34023 23059 33070 98168 61394 18457 75012 78043 76230 77374 84422 44920 13785 98538 75199 94325 98316 64371 66414 3527 76092 68981 59957 41874 6863 99171 6997 97282 2306 20926 77085 36328 60337 26506 50847 21730 61314 25858 16125 53896 19583 546 98815 33368 15435 90365 44044 13751 71088 26809 17277 47179 95789 93585 5404 2652 92755 12400 99933 95061 49677 93369 47740 10013 36227 98587 48095 97540 40796 80571 51435 60379 97468 66602 10098 12903 73318 70493 26653 60757 97302 60281 24287 9442 53866 29690 28445 46620 58441 44730 58032 8118 38098 5772 34482 90676 20710 98928 4568 77857 79498 72354 54587 76966 55307 64684 6220 28625 51529 32872 5733 48830 9504 30020 58271 63369 59709 86716 26341 18150 47797 724 42619 2246 22847 93452 92922 43556 92380 97489 37765 88229 69842 92351 65194 41501 57035 87765 70125 24915 36988 75857 73744 46

It always stops at this exact point, so this is likely some kind of an overflow error. The code works just fine with a short line.

网友答案:

Your problem is not readline() at all but the way you execute the script. Since you're reading from stdin you need to supply stdin with the data to be read. If you don't, the program will hang indefinitely (same as if you ran cat with no arguments).

I took your example code and input file and was able to successfully run it like this:

$ python so_error.py <longline.txt

It exits successfully and rather quickly. I also added print myline at the end of your script to verify that it was indeed parsing everything correctly.

相关阅读:
Top