python 擷取指令碼所在目錄

來源:互聯網
上載者:User

標籤:rac   src   執行檔案   檔案   logger   絕對路徑   split   執行   from   

平時寫python經常會想獲得指令碼所在的目錄,例如有個檔案跟指令檔放在一個相對的目錄位置,那就可以通過指令檔的目錄找到對應的檔案,即使以後指令檔移到其他地方,指令碼也基本不需要改動(相對於寫死目錄的好處)。下面通過一些代碼進行一下對比。

 

   這是我寫的一段代碼在:/root/printfabcd/py/filePath.py

 

 

Python代碼  
  1. 20 logger.debug("sys.path:"+sys.path[0])  
  2. 21 logger.debug("sys.argv:"+sys.argv[0])  
  3. 22 logger.debug("__file__:"+__file__)  
  4. 23 logger.debug("os.getcwd():"+os.getcwd())  
  5. 24 logger.debug("os.path.realpath():"+os.path.realpath(__file__))  
  6. 25 logger.debug("(os.path.abspath(__file__)):"+os.path.abspath(__file__))  
  7. 26 logger.debug("os.path.dirname(os.path.realpath()):"+os.path.dirname(os.path.realpath(__file__)))  
  8. 27 logger.debug("os.path.dirname(os.path.abspath(__file__)):"+os.path.dirname(os.path.abspath(__file__)))  

 

   在/root/printfabcd/py 目錄下執行:python filePath.py

 

 

Python代碼  
  1. 2011-11-27 13:20:20,090 DEBUG filePath 20 sys.path:/root/printfabcd/py  
  2. 2011-11-27 13:20:20,090 DEBUG filePath 21 sys.argv:filePath.py  
  3. 2011-11-27 13:20:20,090 DEBUG filePath 22 __file__:filePath.py  
  4. 2011-11-27 13:20:20,090 DEBUG filePath 23 os.getcwd():/root/printfabcd/py  
  5. 2011-11-27 13:20:20,091 DEBUG filePath 24 os.path.realpath():/root/printfabcd/py/filePath.py  
  6. 2011-11-27 13:20:20,091 DEBUG filePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/filePath.py  
  7. 2011-11-27 13:20:20,091 DEBUG filePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py  
  8. 2011-11-27 13:20:20,091 DEBUG filePath 27 os.path.dirname(os.path.abspath(__file__)):/root/printfabcd/py  

 

  在/root 目錄下執行:python printfabcd/py/filePath.py

 

  2

Python代碼  
  1. 2011-11-27 13:20:39,227 DEBUG filePath 20 sys.path:/root/printfabcd/py  
  2. 2011-11-27 13:20:39,227 DEBUG filePath 21 sys.argv:printfabcd/py/filePath.py  
  3. 2011-11-27 13:20:39,227 DEBUG filePath 22 __file__:printfabcd/py/filePath.py  
  4. 2011-11-27 13:20:39,228 DEBUG filePath 23 os.getcwd():/root  
  5. 2011-11-27 13:20:39,228 DEBUG filePath 24 os.path.realpath():/root/printfabcd/py/filePath.py  
  6. 2011-11-27 13:20:39,228 DEBUG filePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/filePath.py  
  7. 2011-11-27 13:20:39,228 DEBUG filePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py  
  8. 2011-11-27 13:20:39,228 DEBUG filePath 27 os.path.dirname(os.path.abspath(__file__)):/root/printfabcd/py  

 

 從上面兩個輸出可以看出來,如果想獲得指令碼所在目錄最簡單就是通過sys.path[0],os.getcwd()獲得的是執行指令碼的目錄。

 

下面在做一下測試 我在/root/printfabcd/py/下建立一個軟串連tfilePath.py 指向filePath.py,執行tfilePath.py

 

 

Python代碼  
  1. 2011-11-27 13:20:57,466 DEBUG tfilePath 20 sys.path:/root/printfabcd/py  
  2. 2011-11-27 13:20:57,466 DEBUG tfilePath 21 sys.argv:printfabcd/py/tfilePath.py  
  3. 2011-11-27 13:20:57,466 DEBUG tfilePath 22 __file__:printfabcd/py/tfilePath.py  
  4. 2011-11-27 13:20:57,467 DEBUG tfilePath 23 os.getcwd():/root  
  5. 2011-11-27 13:20:57,467 DEBUG tfilePath 24 os.path.realpath():/root/printfabcd/py/filePath.py  
  6. 2011-11-27 13:20:57,467 DEBUG tfilePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/tfilePath.py  
  7. 2011-11-27 13:20:57,467 DEBUG tfilePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py  
  8. 2011-11-27 13:20:57,468 DEBUG tfilePath 27 os.path.dirname(os.path.abspath(__file__)):/root/printfabcd/py  

 

 

  仔細看os.path.realpath()和os.path.abspath()可以看出他們的區別,即使你建立了軟串連,realpath還是可以拿到真正對應的檔案。我是一個python的初學者懂得不多,如果發現什麼問題,請多多指正。

 

 

 

下面是__file__與sys.argv[0]的一個對比

 

轉載自:http://andylin02.iteye.com/blog/933237

 

python __file__ 與argv[0]

在python下,擷取當前執行主指令碼的方法有兩個:sys.argv[0]和__file__。

sys.argv[0]

擷取主執行檔案路徑的最佳方法是用sys.argv[0],它可能是一個相對路徑,所以再取一下abspath是保險的做法,像這樣:

import os,sysdirname, filename = os.path.split(os.path.abspath(sys.argv[0]))print "running from", dirnameprint "file is", filename
__file__

__file__ 是用來獲得模組所在的路徑的,這可能得到的是一個相對路徑,比如在指令碼test.py中寫入:

#!/usr/bin/env python
print __file__

  • 按相對路徑./test.py來執行,則列印得到的是相對路徑,
  • 按絕對路徑執行則得到的是絕對路徑。
  • 而按使用者目錄來執行(~/practice/test.py),則得到的也是絕對路徑(~被展開)
  • 所以為了得到絕對路徑,我們需要 os.path.realpath(__file__)。

而在Python控制台下,直接使用print __file__是會導致  name ‘__file__’ is not defined錯誤的,因為這時沒有在任何一個指令碼下執行,自然沒有 __file__的定義了。

__file__和argv[0]差異

在主執行檔案中時,兩者沒什麼差異,不過要是在不同的檔案下,就不同了,下面樣本:

C:\junk\so>type \junk\so\scriptpath\script1.pyimport sys, osprint "script: sys.argv[0] is", repr(sys.argv[0])print "script: __file__ is", repr(__file__)print "script: cwd is", repr(os.getcwd())import whereutilswhereutils.show_where() C:\junk\so>type \python26\lib\site-packages\whereutils.pyimport sys, osdef show_where():    print "show_where: sys.argv[0] is", repr(sys.argv[0])    print "show_where: __file__ is", repr(__file__)    print "show_where: cwd is", repr(os.getcwd()) C:\junk\so>\python26\python scriptpath\script1.pyscript: sys.argv[0] is ‘scriptpath\\script1.py‘script: __file__ is ‘scriptpath\\script1.py‘script: cwd is ‘C:\\junk\\so‘show_where: sys.argv[0] is ‘scriptpath\\script1.py‘show_where: __file__ is ‘C:\\python26\\lib\\site-packages\\whereutils.pyc‘show_where: cwd is ‘C:\\junk\\so‘

所以一般來說,argv[0]要更可靠些。

python 擷取指令碼所在目錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.