How to handle EOFError for raw_input () in python on Mac OS X

My python program has two raw_input() calls

The first raw_input() should accept multi-line input from the user. The user can issue Ctrl + D (Ctrl + Z in the windows) to end the input.

The second raw_input() should take another input from the user with a request of type (y / n).

Unfortunately (on Mac OS X only?), The second raw_input() raises an EOFError when stdin completes (with Ctrl + D) first raw_input() .

See my sample code below for more details -

 mailBody = '' signature = 'Later!' print 'Compose your mail:' while True: try: # Hit ^D after entering some text mailBody+= raw_input() mailBody+='\n' except EOFError: break # This raw_input() throws EOFError too. Because, stdin is terminated for the session # when EOF (^D) is issues at first raw_input() method (Where as, it doesn't raise EOFError in Linux) opt = raw_input("Do you want to add signature to your mail? (y/N): ").lower() print '-'*10+'Your Mail' if opt == 'y': print mailBody+"\n"+signature else: print mailBody print '-'*19 

Program Output:

 -1- abhinay@MacBook code/py % python prompt.py Compose your mail: hello there! how is everybody? Do you want to add signature to your mail? (y/N): Traceback (most recent call last): File "prompt.py", line 11, in <module> opt = raw_input("Do you want to add signature to your mail? (y/N): ").lower() EOFError 

How to make a second prompt not to raise an EOFError . Please, help!

EDIT:

I edited my question to make things simple.

I ran my code above on the Linux System, it works without any problems. That is, the user was asked to make a second selection of raw_input () to select "(y / N)".

+4
source share
1 answer

It is quite normal that when standard input is completed (once in control-D, in Unix-derived systems - I think it is control-Z on Windows), it remains terminated after that (unless you close and open it in the meantime , of course).

+6
source

All Articles