Get the __file__ of the function one level up in the stack

前端 未结 1 1421
夕颜
夕颜 2021-01-12 07:29

I\'ve found that I\'m using this pattern a lot :

os.path.join(os.path.dirname(__file__), file_path)

so I\'ve decided to put in a function i

相关标签:
1条回答
  • 2021-01-12 08:08

    This should do it:

    inspect.getfile(sys._getframe(1))
    

    sys._getframe(1) gets the caller frame, inspect.getfile(...) retrieves the filename.

    0 讨论(0)
提交回复
热议问题