首页 > 解决方案 > python中的日志文件

问题描述

你好有人可以帮助我,我有一个 Python 程序,我想在“日志”中注册输出数字,但我想输入所有数字,因为我在程序中使用了 open 然后我使用了 + = 希望na horade在程序跳过一行的文件中写入下一行,但程序出错,请有人帮忙():

hora = str (datetime.now ()). replace (":", "."). split ()
hour = hour [0] + "," + hour [1]
   
name = "log" + time
    
log = open (". txt", "wb")
log + = "," + str (num)

这是日志的一部分

标签: pythonpython-3.xlogging

解决方案


你犯了多个错误。
hour = hour [0] + "," + hour [1] 会给你一个错误,因为它应该是hora而不是hour。正确的行:

hour = hora [0] + "," + hora [1]

第二个错误:

log = open (". txt", "wb")

在编写文本而不是二进制(“wb”)时,您应该使用文本模式(“wt”)。

log = open (". txt", "wt")

第三个错误:
写入文件使用file.write()
So

log + = "," + str (num)

应该

log.append("," + str (num))

推荐阅读