Python Path Discussion
2015-06-06 22:05
676 查看
There are various ways to get the path of a module or package in python, such as through the module attritube __file__, __name__. Other ways including os.path.realpath(), or sys.argv[0], etc; However, it should be noted that all these approaches differs
in behavior greatly based on the specific use cases.
First we need to be clear on three concepts, current working directory, relative path and absolute path. Current directory, working directory or current working directory are actually three different names that refers to the same thing. A current directory
is a directory in which a user/program is working on at a given time. A director is a special type of file that contains a list of objects (also files). A relative path makes sense only when, another absolute path to which it is relative to, is specified.
Note 1: An absolute pathname, also referred to as an absolute path or afull path, is the location of afilesystem object (i.e., file, directory or link) relative to theroot
directory.
( Cited from http://www.linfo.org/absolute_pathname.html)
Note 2: A directory has two attribute that needs attention: the name of the directory and the path of the directory
1. __file__:
Use os.path.realpath(__file__) to return the abs path of the module being called
The value of __file__ attribute by itself:
Python 3.4: always absolute path, but when the __file__ is the attribute of a imported module, the abs path is formatted in Unix style ("/" instead of "\")
Python 2.7 Relative path and absolute path depending on the CWD
2. sys.argv[0]
Returns abs path when the cwd is not the directory of which the entry script is lanuched. Returns relative path when the cwdis the directory of which the entry script is lanuched
3.sys.path[0]
ALWAYS returns the abs path of the directory of the entry point script, no matter what the cwd is.
4. os.getcwd()
always returns the current working directory of the __main__ script, even if this method is called from an imported script.
This indicts that for all script in a python program, either __main__ or others, the current working directory is the cwd of the __main__ script.
in behavior greatly based on the specific use cases.
First we need to be clear on three concepts, current working directory, relative path and absolute path. Current directory, working directory or current working directory are actually three different names that refers to the same thing. A current directory
is a directory in which a user/program is working on at a given time. A director is a special type of file that contains a list of objects (also files). A relative path makes sense only when, another absolute path to which it is relative to, is specified.
Note 1: An absolute pathname, also referred to as an absolute path or afull path, is the location of afilesystem object (i.e., file, directory or link) relative to theroot
directory.
( Cited from http://www.linfo.org/absolute_pathname.html)
Note 2: A directory has two attribute that needs attention: the name of the directory and the path of the directory
1. __file__:
Use os.path.realpath(__file__) to return the abs path of the module being called
The value of __file__ attribute by itself:
Python 3.4: always absolute path, but when the __file__ is the attribute of a imported module, the abs path is formatted in Unix style ("/" instead of "\")
Python 2.7 Relative path and absolute path depending on the CWD
2. sys.argv[0]
Returns abs path when the cwd is not the directory of which the entry script is lanuched. Returns relative path when the cwdis the directory of which the entry script is lanuched
3.sys.path[0]
ALWAYS returns the abs path of the directory of the entry point script, no matter what the cwd is.
4. os.getcwd()
always returns the current working directory of the __main__ script, even if this method is called from an imported script.
This indicts that for all script in a python program, either __main__ or others, the current working directory is the cwd of the __main__ script.
相关文章推荐
- 【PYTHON】对整个文件进行正则表达式匹配
- python绘图中使用公式时,解决\frac{}{}出来的字体太小的问题
- Python虚拟环境virtualenv
- Python打包为exe使用py2exe时中文乱码问题
- 安装python爬虫scrapy踩过的那些坑和编程外的思考
- 利用Python的pip.exe文件将.whl文件解压为py2exe
- python 对入参文本进行预处理成以一个空格为间隔的一维数组
- python 检索文件资源的方法 =>长期更新
- python with语句与上下文管理器
- php调用python
- python 使用scapy进行帧注入
- 设计模式 Template模式
- python 将命令行执行的屏幕输出赋值到out变量
- python字典排序
- python中的浅拷贝和深拷贝
- pythonanywhere下django无法匹配url问题解决
- Python自学笔记系列一——基础语法
- Python中步长索引解析
- Leetcode: Length of Last Word in python
- Python学习日记---函数