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.py
?
?
2011-11-27 13:20:20,090 DEBUG filePath 20 sys.path:/root/printfabcd/py2011-11-27 13:20:20,090 DEBUG filePath 21 sys.argv:filePath.py2011-11-27 13:20:20,090 DEBUG filePath 22 __file__:filePath.py2011-11-27 13:20:20,090 DEBUG filePath 23 os.getcwd():/root/printfabcd/py2011-11-27 13:20:20,091 DEBUG filePath 24 os.path.realpath():/root/printfabcd/py/filePath.py2011-11-27 13:20:20,091 DEBUG filePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/filePath.py2011-11-27 13:20:20,091 DEBUG filePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py2011-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
2011-11-27 13:20:39,227 DEBUG filePath 20 sys.path:/root/printfabcd/py2011-11-27 13:20:39,227 DEBUG filePath 21 sys.argv:printfabcd/py/filePath.py2011-11-27 13:20:39,227 DEBUG filePath 22 __file__:printfabcd/py/filePath.py2011-11-27 13:20:39,228 DEBUG filePath 23 os.getcwd():/root2011-11-27 13:20:39,228 DEBUG filePath 24 os.path.realpath():/root/printfabcd/py/filePath.py2011-11-27 13:20:39,228 DEBUG filePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/filePath.py2011-11-27 13:20:39,228 DEBUG filePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py2011-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
?
?
2011-11-27 13:20:57,466 DEBUG tfilePath 20 sys.path:/root/printfabcd/py2011-11-27 13:20:57,466 DEBUG tfilePath 21 sys.argv:printfabcd/py/tfilePath.py2011-11-27 13:20:57,466 DEBUG tfilePath 22 __file__:printfabcd/py/tfilePath.py2011-11-27 13:20:57,467 DEBUG tfilePath 23 os.getcwd():/root2011-11-27 13:20:57,467 DEBUG tfilePath 24 os.path.realpath():/root/printfabcd/py/filePath.py2011-11-27 13:20:57,467 DEBUG tfilePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/tfilePath.py2011-11-27 13:20:57,467 DEBUG tfilePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py2011-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下,获取当前执行主脚本的方法有两个:sys.argv[0]和__file__。
获取主执行文件路径的最佳方法是用sys.argv[0],它可能是一个相对路径,所以再取一下abspath是保险的做法,像这样:
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]要更可靠些。
?
?
?
?