例如,
a.boo
方法调用 b.foo
方法。在 b.foo
方法中,我该如何获取 a 的文件名(我不想将 __file__
传递给 b.foo
方法)...a.boo
方法调用 b.foo
方法。在 b.foo
方法中,我该如何获取 a 的文件名(我不想将 __file__
传递给 b.foo
方法)...inspect
模块来实现此功能:frame = inspect.stack()[1]
module = inspect.getmodule(frame[0])
filename = module.__file__
inspect.getmodule()
在某些情况下会返回None
,因此更加可靠的方法是:
filename = frame[0].f_code.co_filename
- dux2filename = frame[1]
(或在Python 3.5+中使用frame.filename
)? - Aran-Fey要获取完整的文件名(包括路径和扩展名),在被调用者中使用:
import inspect
filename = inspect.stack()[1].filename
使用inspect.stack()来检索调用者的文件名。另外,以下代码也会剪切掉完整文件名开头的路径和结尾的文件扩展名:
# Callee.py
import inspect
import os.path
def get_caller_info():
# first get the full filename (including path and file extension)
caller_frame = inspect.stack()[1]
caller_filename_full = caller_frame.filename
# now get rid of the directory (via basename)
# then split filename and extension (via splitext)
caller_filename_only = os.path.splitext(os.path.basename(caller_filename_full))[0]
# return both filename versions as tuple
return caller_filename_full, caller_filename_only
然后可以这样使用:
# Caller.py
import callee
filename_full, filename_only = callee.get_caller_info()
print(f"> Filename full: {filename_full}")
print(f"> Filename only: {filename_only}")
# Output
# > Filename full: /workspaces/python/caller_filename/caller.py
# > Filename only: caller
inspect.getmodule()
返回None
也可用:frame = inspect.stack()[1]
filename = frame[0].f_code.co_filename
inspect
模块来完成,具体来说是inspect.stack
函数:import inspect
import os.path
def get_caller_filepath():
# get the caller's stack frame and extract its file path
frame_info = inspect.stack()[1]
filepath = frame_info[1] # in python 3.5+, you can use frame_info.filename
del frame_info # drop the reference to the stack frame to avoid reference cycles
# make the path absolute (optional)
filepath = os.path.abspath(filepath)
return filepath
import b
print(b.get_caller_filepath())
# output: D:\Users\Aran-Fey\a.py
traceback
模块:import traceback
你可以像这样打印回溯信息:
print traceback.format_stack()
我已经很久没有使用过这个了,但这应该足以让你开始。
import inspect
print inspect.stack()[1][1]
inspect.stack()[1].filename
是首选语法,支持Python 3.5及以上版本。 - Stefan之前的解决方案有问题(因为某些原因返回了“<string>”)。这是我的解决方案:
traceback.format_stack()[0].split('"')[1]