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

后端 未结 29 2414
你的背包
你的背包 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:13

    Most of these answers were written in Python version 2.x or earlier. In Python 3.x the syntax for the print function has changed to require parentheses, i.e. print().

    So, this earlier high score answer from user13993 in Python 2.x:

    import inspect, os
    print inspect.getfile(inspect.currentframe()) # script filename (usually with path)
    print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
    

    Becomes in Python 3.x:

    import inspect, os
    print(inspect.getfile(inspect.currentframe())) # script filename (usually with path)
    print(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) ) # script directory
    

提交回复
热议问题