所以我在我的代码中遇到了一些麻烦,它将keyboard.type()
识别为类型而不是字符串我拥有所有必需的导入。我怎样才能解决这个问题?
with open("words.txt") as fp:
line = fp.readline()
cnt = 1
while line:
print(line)
line = fp.readline()
cnt += 1
exploit(line)
def exploit(keyboard):
time.sleep(2)
keyboard.type(line)
错误:
File "C:\Users\User\Desktop\ref.py", line 63, in start
exploit(line)
File "C:\Users\User\Desktop\ref.py", line 46, in exploit
keyboard.type("hello")
AttributeError: 'str' object has no attribute 'type'
键盘模块(https://pypi.org/project/pynput/)是导入的
我假设你有一个你没有向我们展示的import keyboard
线。 (下次请提供完整的MCVE)。
问题出在这一行:
def exploit(keyboard):
在此函数中无法访问keyboard
模块,因为其名称已被您在此处定义的keyboard
参数覆盖。例如,如果您调用exploit("Hello")
,则此代码将尝试执行"Hello".type(line)
。但该字符串没有名为type
的方法。您可能打算使用名称line
。
def exploit(line):
time.sleep(2)
keyboard.type(line)
您可能会认为函数需要声明它在签名中使用的模块。不是这种情况。在文件顶部导入的模块将可以在该文件中的任何位置访问,而无需执行任何特殊操作。