help(input)
shows what keyboard shortcuts produce EOF, namely, Unix: Ctrl-D, Windows: Ctrl-Z+Return:
input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading.
You could reproduce it using an empty file:
$ touch empty
$ python3 -c "input()" < empty
Traceback (most recent call last):
File "<string>", line 1, in <module>
EOFError: EOF when reading a line
You could use /dev/null
or nul
(Windows) as an empty file for reading. os.devnull
shows the name that is used by your OS:
$ python3 -c "import os; print(os.devnull)"
/dev/null
Note: input()
happily accepts input from a file/pipe. You don't need stdin
to be connected to the terminal:
$ echo abc | python3 -c "print(input()[::-1])"
cba
Either handle EOFError
in your code:
try:
reply = input('Enter text')
except EOFError:
break
Or configure your editor to provide a non-empty input when it runs your script e.g., by using a customized command line if it allows it: python3 "%f" < input_file