open() and EOFError

Terry Reedy tjreedy at udel.edu
Mon Jul 7 20:49:56 CEST 2014


On 7/7/2014 8:39 AM, Roy Smith wrote:

> On a different topic, I've always disliked embedding instructions to
> "type Ctrl-D".  The problem is, how to generate an EOF (or interrupt, or
> whatever) is configurable in the tty driver (see below).  In theory, the
> user could have remapped EOF to be something else.  Maybe they're a
> die-hard Windows fan, and insist on being able to type Ctrl-Z for EOF.
> Not to mention that things like readline are probably running the
> terminal in raw mode and remapping everything all over again.
>
> On the other hand, telling the user to "generate EOF", while technically
> more correct, is not very useful for most people.  I suppose you could
> interrogate the tty driver to find out what the current EOF character
> is, and stick that in your prompt.  I don't even know if that's possible
> with the standard Python library, and I doubt it would be very portable.
> I'm not sure what the best solution is here.

Avoid EOFError. Much better, I think, is the somewhat customary

s = input("Enter something, or hit <return> to exit")
if not s: sys.exit()
else: <process s>

-- 
Terry Jan Reedy




More information about the Python-list mailing list