pynput 键盘记录器不写入文件
pynput keylogger does not write to file
这个键盘记录程序有问题。它必须写入一个文件,但它并没有这样做。怎么了?
程序必须监听键盘并将其写入文件(之前,会检查文件是否存在)。但它不会写入文件,它只会创建文件。
from pynput import keyboard
import os
if os.path.exists("prova3.txt") == True:
f = open("prova3.txt","a")
else:
f = open("prova3.txt","x")
def on_press(key):
try:
f.writelines("///key [ {0} ] pressed ///".format(
key.char))
except AttributeError:
f.writelines("///special key {0} pressed///".format(
key))
def on_release(key):
f.writelines(["///key [ {0} ] released ///".format(
key)])
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
首先,确保您注意到来自 pynput docs 的警告:
Starting a keyboard listener may be subject to some restrictions on your platform.
如果你和我一样Mac,那么你必须这样做:
- The process must run as root.
- Your application must be white listed under Enable access for assistive devices.
对于第二个项目,检查来自相关 SO post 的 these steps。
现在,对于实际的文件写入问题,一种解决方案是在完成文件后调用 f.flush()
after calling f.writelines
to ensure that data is actually written to the file. (see this related post for some explanation: what exactly the python's file.flush() is doing?). I am not familiar with pynput's underlying implementation, but the docs says it uses threads and that seems to affect File I/O. It's also good practice to call f.close()
。
from pynput import keyboard
if os.path.exists("prova3.txt"):
f = open("prova3.txt", "a")
else:
f = open("prova3.txt", "x")
def on_press(key):
try:
f.writelines("///key [ {0} ] pressed ///".format(
key.char))
except AttributeError:
f.writelines("///special key {0} pressed///".format(
key))
f.flush()
def on_release(key):
f.writelines(["///key [ {0} ] released ///".format(
key)])
f.flush()
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
f.close()
另一种更好的做法是use a with
statement when writing to the file。这使得 f
和 pynput listener
都在同一个上下文中。通过这种方式,不需要调用 flush
(但如果你愿意,你仍然可以)。
from pynput import keyboard
with open("prova3.txt", "a") as f:
def on_press(key):
try:
f.writelines("///key [ {0} ] pressed ///".format(
key.char))
except AttributeError:
f.writelines("///special key {0} pressed///".format(
key))
def on_release(key):
f.writelines(["///key [ {0} ] released ///".format(
key)])
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
这个键盘记录程序有问题。它必须写入一个文件,但它并没有这样做。怎么了?
程序必须监听键盘并将其写入文件(之前,会检查文件是否存在)。但它不会写入文件,它只会创建文件。
from pynput import keyboard
import os
if os.path.exists("prova3.txt") == True:
f = open("prova3.txt","a")
else:
f = open("prova3.txt","x")
def on_press(key):
try:
f.writelines("///key [ {0} ] pressed ///".format(
key.char))
except AttributeError:
f.writelines("///special key {0} pressed///".format(
key))
def on_release(key):
f.writelines(["///key [ {0} ] released ///".format(
key)])
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
首先,确保您注意到来自 pynput docs 的警告:
Starting a keyboard listener may be subject to some restrictions on your platform.
如果你和我一样Mac,那么你必须这样做:
- The process must run as root.
- Your application must be white listed under Enable access for assistive devices.
对于第二个项目,检查来自相关 SO post 的 these steps。
现在,对于实际的文件写入问题,一种解决方案是在完成文件后调用 f.flush()
after calling f.writelines
to ensure that data is actually written to the file. (see this related post for some explanation: what exactly the python's file.flush() is doing?). I am not familiar with pynput's underlying implementation, but the docs says it uses threads and that seems to affect File I/O. It's also good practice to call f.close()
。
from pynput import keyboard
if os.path.exists("prova3.txt"):
f = open("prova3.txt", "a")
else:
f = open("prova3.txt", "x")
def on_press(key):
try:
f.writelines("///key [ {0} ] pressed ///".format(
key.char))
except AttributeError:
f.writelines("///special key {0} pressed///".format(
key))
f.flush()
def on_release(key):
f.writelines(["///key [ {0} ] released ///".format(
key)])
f.flush()
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
f.close()
另一种更好的做法是use a with
statement when writing to the file。这使得 f
和 pynput listener
都在同一个上下文中。通过这种方式,不需要调用 flush
(但如果你愿意,你仍然可以)。
from pynput import keyboard
with open("prova3.txt", "a") as f:
def on_press(key):
try:
f.writelines("///key [ {0} ] pressed ///".format(
key.char))
except AttributeError:
f.writelines("///special key {0} pressed///".format(
key))
def on_release(key):
f.writelines(["///key [ {0} ] released ///".format(
key)])
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()