David542
David542

Reputation: 110380

How to get the filepath from a frame

To get the filename from a frame I can do:

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' ?

However, I don't see a way to get the path of the file. How would one get the complete filepath from the stack? (Actually, the only way I currently see how to get it is using a regex from traceback.format_exc(), but that seems pretty crude.)

Upvotes: 2

Views: 701

Answers (1)

David542
David542

Reputation: 110380

os.path.abspath should solve this for you:

filepath = os.path.abspath(current_frame.f_code.co_filename)

Upvotes: 4

Related Questions