0

当我使用流重定向执行 Python 脚本时,我不断收到 EOF 错误。

脚本

name=input('Enter your name : ')
print ('Welcome ' + name)
input('Press \'ENTER\' to exit!')

执行命令:

helloworld.py < input.dat

错误:

Enter your name : Welcome Gunit
Press 'ENTER' to exit!Traceback (most recent call last):
  File "D:\Reference\Python\Codes\helloworld.py", line 28, in <module>
    input('Press \'ENTER\' to exit!')
EOFError: EOF when reading a line
4

1 回答 1

1

您的代码从文件中读取两行(一行用于名称,另一行用于“Enter to exit”)。您的输入文件中只有一行。

因此,Python 在读取第 2 行之前到达文件结束指示。

要修复,请确保您input.dat至少有两行,或删除第二个input()电话。

于 2017-10-11T16:20:08.237 回答