得到函数所在脚本的绝对路径

例如:test.py中引用define.py中的变量。两个脚本不在同一目录下。我想知道define.py脚本的绝对路径

目录结构

root

|

|——define.py

|——file_util.py

|

|——test

           |——test.py


主要代码为

script_path=inspect.getfile(inspect.currentframe())

define.py脚本

#!/usr/bin/python
# encoding:utf-8
import inspect
import os


def get_root_path():
    script_path=inspect.getfile(inspect.currentframe())
    root=os.path.dirname(script_path)
    return  root

cur=get_root_path()
g_xcf_root=os.sep.join((cur,"..","dy_cike_xcf"))
g_xcf_assets=os.sep.join((g_xcf_root,"assets"))
g_xcf_config=os.sep.join((g_xcf_assets,"config"))
g_xcf_cocos=os.sep.join((g_xcf_assets,"cocostudio"))
g_xcf_skeleton=os.sep.join((g_xcf_assets,"skeleton"))
g_xcf_font=os.sep.join((g_xcf_assets,"font"))
g_working_root=os.sep.join((cur,"android","assets"))
g_working_config=os.sep.join((g_working_root,"config"))
g_working_skeleton=os.sep.join((g_working_root,"skeleton"))
g_working_font=os.sep.join((g_working_root,"font"))

file_util.py

#!/usr/bin/python
# encoding:utf-8
r'''
 处理文件
'''
import os

__author__ = 'andrew'


def list_files_with_filter(root, suffix):
    hint="list_files_with_filter"
    print("hint = {0}, root={1}".format(hint,root))
    os_listdir = os.listdir(root)
    for f in os_listdir:
        if (f.endswith(suffix)):
            path = os.sep.join((root, f))
            yield path

test.py

#!/usr/bin/python
# encoding:utf-8
import define
import file_util


if __name__=="__main__":
    paths=file_util.list_files_with_filter(define.g_xcf_font,".fnt")

    #通过yield获得的paths是一个生成器generator,不是列表
    #只能list(paths)转换为列表,或者在for in中使用
    print("{0}, {1}".format(paths,type(paths)))
    for p in paths:
        print("p {0}".format(p))





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值