0

If I have the following Python code:

keyphrase = sys.stdin.read()
print(keyphrase)
domain = input()
print(domain)

I run it as follows:

/Users/me/bin/python /Users/me/PycharmProjects/pem_formatter/test_code.py
Please enter a key:
Key line 1
Key line 2
Key line 3
^D
Key line 1
Key line 2
Key line 3

Please enter a domain:
Traceback (most recent call last):
  File "/Users/me/PycharmProjects/pem_formatter/test_code.py", line 8, in <module>
    domain = input()
EOFError: EOF when reading a line

I tried using a try/except as follows:

try:
    print('Please enter a key:')
    keyphrase = sys.stdin.read()
    print(keyphrase)
except EOFError as error:
    print('EOF Error')

but it made little difference:

/Users/me/bin/python /Users/me/PycharmProjects/pem_formatter/test_code.py
Please enter a key:
Key line 1
Key line 2
Key line 3
^D
Traceback (most recent call last):
  File "/Users/me/PycharmProjects/pem_formatter/test_code.py", line 12, in <module>
    domain = input()
EOFError: EOF when reading a line
Key line 1
Key line 2
Key line 3

Please enter a domain:

Process finished with exit code 1

What do I do in scenarios where I may want to take a multi line input for one variable and a single line for another?

runnerpaul
  • 5,942
  • 8
  • 49
  • 118
  • Thanks @jordanm. I actually came across that. I just can't help feel there must be a better way to do this. I thought `sys.stdin.read()` be a solution but having to Ctrl-D is causing issues. – runnerpaul Apr 16 '20 at 19:20
  • 1
    That's because Ctrl-D sends EOF and closes stdin. – jordanm Apr 16 '20 at 19:25

0 Answers0