python中os.path.dirname(__file__)和sys.path[0]使用
2014-02-14 21:09
417 查看
python中os.path.dirname(__file__)的使用
(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:
python c:/python27/test/test.py
那么将输出 c:/python27/test/
(2).当"print os.path.dirname(__file__)"所在脚本是以相对路径被运行的, 那么将输出空目录,比如:
python test.py
那么将输出空字符串
以下代码可以保证在任何时候都切换到当前执行脚本所在目录中,该当当做公式熟记在心。
(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:
python c:/python27/test/test.py
那么将输出 c:/python27/test/
(2).当"print os.path.dirname(__file__)"所在脚本是以相对路径被运行的, 那么将输出空目录,比如:
python test.py
那么将输出空字符串
以下代码可以保证在任何时候都切换到当前执行脚本所在目录中,该当当做公式熟记在心。
abspath = os.path.dirname(__file__) sys.path.append(abspath) print abspath if abspath=='': os.chdir(sys.path[0]) else: os.chdir(abspath)
相关文章推荐
- python中os.path.dirname(__file__)的使用
- python中的os.path.dirname(__file__)的使用
- python中os.path.dirname(__file__)的使用
- python中的os.path.dirname(__file__)的使用
- python中的os.path.dirname(__file__)的使用
- python中os.path.dirname(__file__)的使用
- python中的os.path.dirname(__file__)的使用
- python中的os.path.dirname(__file__)的使用
- 记录下os.path.dirname(__file__)使用
- Python os.path.dirname(__file__) os.path.join(str,str)
- python零碎知识os.path.dirname(__file__)
- 记录下os.path.dirname(__file__)使用
- Python中获取路径os.getcwd()和os.path.dirname(os.path.realpath(__file__))的区别和对比
- 转: Python中的os.path.dirname(__file__)
- python的os.path.realpath(__file__),os.getcwd(),sys.path() 的区别
- Python有关 os.path.dirname(__file__)
- Python中获取路径os.getcwd()和os.path.dirname(os.path.realpath(__file__))的区别和对比
- Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)
- Python中的os.path.dirname(__file__)
- 浅析python3中的os.path.dirname(__file__)的使用