Find path to currently running file

后端 未结 8 1903
礼貌的吻别
礼貌的吻别 2020-12-01 01:35

How can I find the full path to the currently running Python script? That is to say, what do I have to do to achieve this:

$ pwd
/tmp
$ python baz.py
running         


        
8条回答
  •  北海茫月
    2020-12-01 01:45

    The running file is always __file__.

    Here's a demo script, named identify.py

    print __file__
    

    Here's the results

    MacBook-5:Projects slott$ python StackOverflow/identify.py 
    StackOverflow/identify.py
    MacBook-5:Projects slott$ cd StackOverflow/
    MacBook-5:StackOverflow slott$ python identify.py 
    identify.py
    

提交回复
热议问题