Find path to currently running file

后端 未结 8 1904
礼貌的吻别
礼貌的吻别 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:51

    I would suggest

    import os, sys
    print os.path.split(os.path.abspath(os.path.realpath(sys.argv[0])))[0]
    

    This way you can safely create symbolic links to the script executable and it will still find the correct directory.

提交回复
热议问题