How do I get the path and name of the file that is currently executing?

后端 未结 29 2740
你的背包
你的背包 2020-11-22 06:43

I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process.

For example, let\'s say I have th

29条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 07:18

    I used the approach with __file__
    os.path.abspath(__file__)
    but there is a little trick, it returns the .py file when the code is run the first time, next runs give the name of *.pyc file
    so I stayed with:
    inspect.getfile(inspect.currentframe())
    or
    sys._getframe().f_code.co_filename

提交回复
热议问题