[英]How to get the filepath from a frame
要從框架中獲取文件名,我可以執行以下操作:
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()
的正則表達式,但這似乎很粗糙。)
os.path.abspath
應該為你解決這個問題:
filepath = os.path.abspath(current_frame.f_code.co_filename)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.