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.

4
  • Please show more representative code and input. Commented May 2, 2017 at 23:01
  • I updated it a bit. Does that give you a better idea of what's going on? Commented May 2, 2017 at 23:11
  • Possible duplicate of How to finish sys.stdin.readlines() input? Commented May 2, 2017 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. Commented May 2, 2017 at 23:52

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent! Just what I was looking for and thank you for the repeated help!
If that is helpful to you, please accept the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.