有一个名为
inspect的模块提供这些信息。
用法示例:
import inspect
def PrintFrame():
callerframerecord = inspect.stack()[1] # 0 represents this line
# 1 represents line at caller
frame = callerframerecord[0]
info = inspect.getframeinfo(frame)
print info.filename # __FILE__ -> Test.py
print info.function # __FUNCTION__ -> Main
print info.lineno # __LINE__ -> 13
def Main():
PrintFrame() # for this line
Main()
但是,请记住,有一种更简单的方法来获取当前正在执行的文件的名称:
print __file__