Find path to currently running file

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

    import sys, os
    
    file = sys.argv[0]
    pathname = os.path.dirname(file)
    print 'running from %s' % os.path.abspath(pathname)
    print 'file is %s' % file
    

    Check os.getcwd() (docs)

提交回复
热议问题