Find path to currently running file

后端 未结 8 1918
礼貌的吻别
礼貌的吻别 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 02:03

    The script name will (always?) be the first index of sys.argv:

    import sys
    print sys.argv[0]
    

    An even easier way to find the path of your running script:

    os.path.dirname(sys.argv[0])
    

提交回复
热议问题