# Log to File
#This function logs line number, time, and message in log.txt
def log(msg):
global LOG_CNT
LOG_CNT += 1
lineNumber = getframeinfo(currentframe()).lineno
dts = time.strftime('%Y-%m-%d %H:%M:%S')
f = open('log.txt', 'a')
f.write('Log %d: Line %s: %s: %s\n' % (LOG_CNT, lineNumber, dts, msg))
f.close()
调用函数时给出以下错误:log(" Test")
[error] script [ AutoSTO ] stopped with error in line 833
[error] AttributeError ( 'tuple' object has no attribute 'lineno' )
[error] --- Traceback --- error source first line: module ( function ) statement 72: main ( log ) lineNumber = getframeinfo(currentframe()).lineno
[error] --- Traceback --- end --------------
答案 0 :(得分:0)
inspect.getframeinfo函数返回Traceback一个命名的tuple类型,而lineno可能是返回的元组对象中一个成员的属性