Python获取脚本路径(三种方式)

|---bin
  |---test01.py
|---src
  |---aaa.py
|---testcase
  |---group01
     |---pathtest.py

pathtest.py:

import os
import sys
import inspect
inspect.getfile(inspect.currentframe())和inspect.stack()[0][1]得到的是定义该函数的脚本文件的路径,包含脚本名称
inspect.stack()[1][1]得到的是调用该脚本的脚本文件的路径,包含脚本名称
#return the script path where the function is defined
def test():
    this_file=inspect.getfile(inspect.currentframe())
    path=os.path.abspath(os.path.dirname(this_file))
    return path

#return the script path where the function is called
def script_path():
    caller_file=inspect.stack()[1][1]
    return os.path.abspath(os.path.dirname(caller_file))

#return the script path where the Python interpreter is started
def test02():
    path=os.path.realpath(sys.argv[0])
    if os.path.isfile(path):
        path=os.path.dirname(path)
    return os.path.abspath(path)

def current_path():
    path=os.path.realpath(sys.path[0])
    if os.path.isfile(path):
        path=os.path.dirname(path)
        return os.path.abspath(path)
    else:
        caller_file=inspect.stack()[1][1]
        return os.path.abspath(os.path.dirname(caller_file))

aaa.py:

import sys
import os
import inspect
sys.path.append("..\\testcase\\group01")
import pathtest

#path="aaa"
#print pathtest.test()

def caller_func():
    caller_path=pathtest.script_path()
    return caller_path

def def_func():
    def_path=pathtest.test()
    return def_path

def real_func():
    real_func=pathtest.test02()
    return real_func

def curr_func():
    cur_func=pathtest.current_path()
    return cur_func

print caller_func()
print def_func()
print real_func()
print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"



test01.py:

import sys
import os
import inspect
sys.path.append("..\\src")
import aaa

def func_a():
    return aaa.caller_func()

def func_b():
    return aaa.def_func()

def func_c():
    return aaa.real_func()

def func_d():
    return aaa.curr_func()

print func_a()
print func_b()
print func_c()
print func_d()

参考:http://blog.csdn.net/bupteinstein/article/details/6534177
Python获取路径的三种情况:获取函数定义所在脚本路径,获取调用函数脚本所在路径,获取启动Python解释器脚本所在路径
最常用的方式就是pathtest.py中current_path中获取路径的方式。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取Python脚本所在路径有两种情况,一是获取当前脚本文件所在的路径,二是获取启动Python解释器的脚本所在的路径。 如果你想获取当前脚本文件所在的路径,你可以使用如下代码: ```python import os current_script_path = os.path.abspath(os.path.dirname(__file__)) ``` 这段代码通过`__file__`获取当前脚本的文件路径,然后使用`os.path.dirname()`函数获取该文件所在的目录路径,最后使用`os.path.abspath()`函数获取目录的绝对路径。 如果你想获取启动Python解释器的脚本所在的路径,你可以使用如下代码: ```python import inspect, os caller_file = inspect.stack()[1][1] caller_script_path = os.path.abspath(os.path.dirname(caller_file)) ``` 这段代码使用了`inspect`模块的`stack()`函数获取调用该函数的脚本的文件路径,然后使用`os.path.dirname()`函数获取该文件所在的目录路径,最后使用`os.path.abspath()`函数获取目录的绝对路径。 请根据你的具体需求选择合适的方法来获取脚本所在路径。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python语言获取脚本文件所在路径](https://blog.csdn.net/bupteinstein/article/details/6534177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值