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

后端 未结 29 2930
你的背包
你的背包 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 06:57

    I think this is cleaner:

    import inspect
    print inspect.stack()[0][1]
    

    and gets the same information as:

    print inspect.getfile(inspect.currentframe())
    

    Where [0] is the current frame in the stack (top of stack) and [1] is for the file name, increase to go backwards in the stack i.e.

    print inspect.stack()[1][1]
    

    would be the file name of the script that called the current frame. Also, using [-1] will get you to the bottom of the stack, the original calling script.

提交回复
热议问题