平时写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]要更可靠些。