关于Python3中的stderr日志文件写入?


环境:Linux Mint


 # 当前目录中的文件hello.txt内容为空,则在日志文件er-ror.log中记录异常信息。
# 如果文件hello.txt的内容不为空,则在日志文件error.log中记录正确的信息。

import sys,time

sys.stderr = open("record.log","a")
f = open(r"./hello.txt","r")
t = time.strftime("%Y-%m-%d %X",time.localtime())
context = f.read()
if context:
    sys.stderr.write(t +" " + context)
else:
    raise Exception,t + "异常信息"

运行以上代码时,出现如下错误,求解答,谢谢。


 raise Exception,t + "异常信息"
                   ^
SyntaxError: invalid syntax

python python3.x

吃瓜笨蛋圈9 8 years, 7 months ago

raise Exception(t + "异常信息")

linsunx answered 8 years, 7 months ago

Your Answer