使用流重定向EOF错误执行python脚本

时间:2017-10-11 16:16:43

标签: python

当我使用stream -redirection执行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

1 个答案:

答案 0 :(得分:0)

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

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

要解决此问题,请确保您的input.dat至少有两行,或删除第二个input()来电。