1

I'm having trouble prompting the user for input after I have already read a file from the command line. My code looks something like this but I get an EOFError every time. Am I missing something? My code:

import sys
file = sys.stdin.read().splitlines()
print (file)
name = input("Input your name: ")
print (name)

Here is what I put into the command line for these lines:

python3 tester.py < example_file.txt
['my file']
Input your name: Traceback (most recent call last):
File "tester.py", line 4, in <module>
name = input("Input your name: ")
EOFError: EOF when reading a line

In this case, I believe I get out of the sys.stdin.read().splitlines() line given that it correctly prints out the information in file. However, once the line containing input() is executed, I am not prompted to input anything and I get an error without pausing to input like normal.

SQ101
  • 13
  • 4
  • Please show more representative code and input. – dawg May 02 '17 at 23:01
  • I updated it a bit. Does that give you a better idea of what's going on? – SQ101 May 02 '17 at 23:11
  • Possible duplicate of [How to finish sys.stdin.readlines() input?](http://stackoverflow.com/questions/5549141/how-to-finish-sys-stdin-readlines-input) – Pedro Castilho May 02 '17 at 23:32
  • @PedroCastilho: That does not solve this issue. How do you read a complete file from stdin then switch to reading stdin interactively. – dawg May 02 '17 at 23:52

1 Answers1

1

You can use the fileinput module to handle multiple streams a little easier.

import fileinput

t_list=[line for line in fileinput.input()]
print(t_list)

name=input('name? ')
print(name)

Then run with the file name, not the string contents of the file:

$ python3 test.py file
['my file\n']
name? bob
bob
dawg
  • 98,345
  • 23
  • 131
  • 206
  • Excellent! Just what I was looking for and thank you for the repeated help! – SQ101 May 02 '17 at 23:59
  • If that is helpful to you, please [accept the answer](http://stackoverflow.com/help/someone-answers) – dawg May 03 '17 at 00:03