python 獲取腳本所在目錄


平時寫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,sys
dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
print "running from", dirname
print "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.py
import sys, os
print "script: sys.argv[0] is", repr(sys.argv[0])
print "script: __file__ is", repr(__file__)
print "script: cwd is", repr(os.getcwd())
import whereutils
whereutils.show_where()
 
C:\junk\so>type \python26\lib\site-packages\whereutils.py
import sys, os
def 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.py
script: 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]要更可靠些。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM