pythonpython-3.7eoferror

Python3 Idle keeps closing with ctrl + D


So I've linked about 5 files together I think, and I've added a piece of code to all those files which SHOULD prevent the idle from closing. But when I reach the last file:

# modules
import smtplib
from email.message import EmailMessage
#from pynput.keyboard import Key, Listener


ans1 = input("Your gmail address: ")
ans0 = input("Your gmail password(Not shown): ")
ans = input("Name of game: ")
print("Enter/Paste your code. Ctrl-D to send it.")
contents = []
while True:
    try:
        line = input()
    except EOFError:
        break
    contents.append(line)

# content
sender = ans1
reciever = "rockzombie005@gmail.com"
password = ans0
msg_body = "\n".join(contents)



# action
msg = EmailMessage()
msg['subject'] = ans   
msg['from'] = sender
msg['to'] = reciever
msg.set_content(msg_body)

with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
    smtp.login(sender,password)
        
    smtp.send_message(msg)

print("Program sent successfully!")

try:
    input()
except EOFError:
    pass

and as you can see:

try:
    input()
except EOFError:
    pass

that piece of code should prevent the idle from closing, and it works, but only if I run it separately. If I do ctrl + D when executed using a different file, the shell just closes or crashes without any prompt.


Solution

  • In the Linux command-line shell, pressing Ctrl+D logs out of the interface. If you used the sudo command to execute commands as another user, pressing Ctrl+D exits out of that other user and puts you back as the user you originally logged into.

    You can disable eof generally in bash:

    set -o ignoreeof
    

    Alternatively, You can use the IGNOREEOF environment variable in bash. So export IGNOREEOF=42 and you'll have to press Ctrl+D forty-two times before it actually quits your shell.