python脚本的文件名和行号

我如何获得python脚本中的文件名和行号。

完全是我们从exception回溯中获得的文件信息。 在这种情况下,不会引发exception。

inspect.currentframe().f_back.f_lineno 

从ActiveState代码 。

感谢mcandre,答案是:

 from inspect import currentframe, getframeinfo frameinfo = getframeinfo(currentframe()) print frameinfo.filename, frameinfo.lineno 

是否使用currentframe().f_back取决于您是否使用函数。

直接调用:

 from inspect import currentframe, getframeinfo cf = currentframe() filename = getframeinfo(cf).filename print "This is line 5, python says line ", cf.f_lineno print "The filename is ", filename 

调用一个为你做的function:

 from inspect import currentframe def get_linenumber(): cf = currentframe() return cf.f_back.f_lineno print "This is line 7, python says line ", get_linenumber() 

文件名: __file__sys.argv[0]
行: inspect.currentframe().f_lineno (不是inspect.currentframe()。f_back.f_lineno如上所述)

只是为了贡献,

python中有一个linecache模块,这里有两个可以帮助的链接。

linecache模块文档
linecache源代码

从某种意义上讲,你可以将整个文件“转储”到caching中,并使用类中的linecache.cache数据读取它。

 import linecache as allLines ## have in mind that fileName in linecache behaves as any other open statement, you will need a path to a file if file is not in the same directory as script linesList = allLines.updatechache( fileName ,None) for i,x in enumerate(lineslist): print(i,x) #prints the line number and content #or for more info print(line.cache) #or you need a specific line specLine = allLines.getline(fileName,numbOfLine) #returns a textual line from that number of line 

有关其他信息,对于error handling,您可以简单地使用

 from sys import exc_info try: raise YourError # or some other error except Exception: print(exc_info() ) 
 import inspect file_name = __FILE__ current_line_no = inspect.stack()[0][2] current_function_name = inspect.stack()[0][3] #Try printing inspect.stack() you can see current stack and pick whatever you want 

方便,如果在一个共同的文件中使用 – 打印文件名称,行号和来电者的function:

 import inspect def getLineInfo(): print(inspect.stack()[1][1],":",inspect.stack()[1][2],":", inspect.stack()[1][3]) 

更好地使用系统也 –

 print dir(sys._getframe()) print dir(sys._getframe().f_lineno) print sys._getframe().f_lineno 

输出是:

 ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'f_back', 'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_globals', 'f_lasti', 'f_lineno', 'f_locals', 'f_restricted', 'f_trace'] ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real'] 14