平時寫python常常會想得到腳本所在的目錄,例若有個文件跟腳本文件放在一個相對的目錄位置,那就能夠經過腳本文件的目錄找到對應的文件,即便之後腳本文件移到其餘地方,腳本也基本不須要改動(相對於寫死目錄的好處)。下面經過一些代碼進行一下對比。python
這是我寫的一段代碼在:/root/printfabcd/py/filePath.py測試
- 20 logger.debug("sys.path:"+sys.path[0])
- 21 logger.debug("sys.argv:"+sys.argv[0])
- 22 logger.debug("__file__:"+__file__)
- 23 logger.debug("os.getcwd():"+os.getcwd())
- 24 logger.debug("os.path.realpath():"+os.path.realpath(__file__))
- 25 logger.debug("(os.path.abspath(__file__)):"+os.path.abspath(__file__))
- 26 logger.debug("os.path.dirname(os.path.realpath()):"+os.path.dirname(os.path.realpath(__file__)))
- 27 logger.debug("os.path.dirname(os.path.abspath(__file__)):"+os.path.dirname(os.path.abspath(__file__)))
在/root/printfabcd/py 目錄下執行:python filePath.pyspa
- 2011-11-27 13:20:20,090 DEBUG filePath 20 sys.path:/root/printfabcd/py
- 2011-11-27 13:20:20,090 DEBUG filePath 21 sys.argv:filePath.py
- 2011-11-27 13:20:20,090 DEBUG filePath 22 __file__:filePath.py
- 2011-11-27 13:20:20,090 DEBUG filePath 23 os.getcwd():/root/printfabcd/py
- 2011-11-27 13:20:20,091 DEBUG filePath 24 os.path.realpath():/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:20,091 DEBUG filePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:20,091 DEBUG filePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py
- 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.pydebug
2code
- 2011-11-27 13:20:39,227 DEBUG filePath 20 sys.path:/root/printfabcd/py
- 2011-11-27 13:20:39,227 DEBUG filePath 21 sys.argv:printfabcd/py/filePath.py
- 2011-11-27 13:20:39,227 DEBUG filePath 22 __file__:printfabcd/py/filePath.py
- 2011-11-27 13:20:39,228 DEBUG filePath 23 os.getcwd():/root
- 2011-11-27 13:20:39,228 DEBUG filePath 24 os.path.realpath():/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:39,228 DEBUG filePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:39,228 DEBUG filePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py
- 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()得到的是執行腳本的目錄。blog
下面在作一下測試 我在/root/printfabcd/py/下建立一個軟鏈接tfilePath.py 指向filePath.py,執行tfilePath.pyip
- 2011-11-27 13:20:57,466 DEBUG tfilePath 20 sys.path:/root/printfabcd/py
- 2011-11-27 13:20:57,466 DEBUG tfilePath 21 sys.argv:printfabcd/py/tfilePath.py
- 2011-11-27 13:20:57,466 DEBUG tfilePath 22 __file__:printfabcd/py/tfilePath.py
- 2011-11-27 13:20:57,467 DEBUG tfilePath 23 os.getcwd():/root
- 2011-11-27 13:20:57,467 DEBUG tfilePath 24 os.path.realpath():/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:57,467 DEBUG tfilePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/tfilePath.py
- 2011-11-27 13:20:57,467 DEBUG tfilePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py
- 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的初學者懂得很少,若是發現什麼問題,請多多指正。get
下面是__file__與sys.argv[0]的一個對比string
轉載自:http://andylin02.iteye.com/blog/933237it
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]要更可靠些。