____tz_zs

总结

__file__
在IDE中获取的是绝对路径,在终端启动时,启动位置是本模块,则为相对路径,由外部模块调用则获取的是未经解析的绝对路径。

sys.path[0]
忠实的表示启动的文件所在的路径

os.getcwd()
获取的是工作空间的路径。IDE中可以设置working directory,终端启动时则是指你cd到的那个路径下。

os.path.abspath(os.path.split(os.path.abspath(os.path.realpath(__file__)))[0] + “/…/”)


此方法是我写的获取项目绝对路径的工具方法。注意此文件的位置固定为项目的util文件夹下。

补充:

sys.argv

是调用脚本时使用的路径,list格式。是调用python命令的时候的第一个参数。

如,在ide启动时,ide默认配置的参数如图

python怎么得到文件所在路径 python获得文件路径_ide

['/home/zmate/tzzs/mytz/development/test_start.py']

在终端启动

:~/tzzs/mytz/development$ python test_start.py 
['test_start.py']
:~/tzzs/mytz$ python development/test_start.py
['development/test_start.py']

使用对比demo

项目名称
mytz
项目目录结构
development/test_start.py
util/tool_get_project_path.py

代码 tool_get_project_path.py

#!/usr/bin/python2.7
# -*- coding:utf-8 -*-

"""
@author:    tzzs
"""
import os
import sys


def get_project_path():
    print(__file__)  # 此py文件所在的路径
    print(sys.path[0])  # 启动的py文件所在的路径
    print(os.getcwd())  # 工作空间的路径(working directory),默认为启动的py文件所在路径,但ide中可以自定义设置。
    project_path = os.path.split(os.path.abspath(os.path.realpath(__file__)))[0] + "/../"
    print(os.path.abspath(project_path))


if __name__ == '__main__':
    get_project_path()

代码 test_start.py

#!/usr/bin/python2.7
# -*- coding:utf-8 -*-

"""
@author:    tzzs
"""
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
sys.path.append(sys.path[0] + "/../")

from util.tool_get_project_path import get_project_path

get_project_path()

IED启动

tool_get_project_path.py

直接启动,此时默认工作目录为 /home/user/tzzs/mytz/util

/home/user/tzzs/mytz/util/tool_get_project_path.py
/home/user/tzzs/mytz/util
/home/user/tzzs/mytz/util
/home/user/tzzs/mytz

修改工作目录(working directory)为 /home/user/tzzs/mytz/

/home/user/tzzs/mytz/util/tool_get_project_path.py
/home/user/tzzs/mytz/util
/home/user/tzzs/mytz
/home/user/tzzs/mytz

test_start.py

直接启动,此时默认工作目录为 /home/user/tzzs/mytz/development

/home/user/tzzs/mytz/util/tool_get_project_path.pyc
/home/user/tzzs/mytz/development
/home/user/tzzs/mytz/development
/home/user/tzzs/mytz

修改工作目录(working directory)为 /home/user/tzzs/mytz/

/home/user/tzzs/mytz/util/tool_get_project_path.pyc
/home/user/tzzs/mytz/development
/home/user/tzzs/mytz
/home/user/tzzs/mytz

使用终端启动

tool_get_project_path.py

在文件位置启动

:~/tzzs/mytz/util$ python tool_get_project_path.py
tool_get_project_path.py
/home/user/tzzs/mytz/util
/home/user/tzzs/mytz/util
/home/user/tzzs/mytz

在项目根目录启动

:~/tzzs/mytz$ python util/tool_get_project_path.py
util/tool_get_project_path.py
/home/user/tzzs/mytz/util
/home/user/tzzs/mytz
/home/user/tzzs/mytz

test_start.py

在文件位置启动

:~/tzzs/mytz/development$ python test_start.py 
/home/user/tzzs/mytz/development/../util/tool_get_project_path.pyc
/home/user/tzzs/mytz/development
/home/user/tzzs/mytz/development
/home/user/tzzs/mytz

在项目根目录启动

:~/tzzs/mytz$ python development/test_start.py 
/home/user/tzzs/mytz/development/../util/tool_get_project_path.pyc
/home/user/tzzs/mytz/development
/home/user/tzzs/mytz
/home/user/tzzs/mytz