如何从框架获取文件路径

问题描述 投票:0回答:1

要从框架中获取文件名,我可以这样做:

exc_type, exc_value, exc_traceback = sys.exc_info()
current_frame = exc_traceback.tb_frame
filename = current_frame.f_code.co_filename
# example.py
# how to get '/Users/david/Desktop/Log/example.py' ?

但是,我没有找到获取文件路径的方法。如何从堆栈中获取完整的filepath? (实际上,我目前看到的唯一方法是使用traceback.format_exc()中的正则表达式,但这似乎很粗糙。)

python python-3.x introspection
1个回答
0
投票

[os.path.abspath应该为您解决这个问题:

os.path.abspath
© www.soinside.com 2019 - 2024. All rights reserved.