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

来源:互联网 发布:xmr显卡算力 知乎 编辑:程序博客网 时间:2024/05/21 09:50

例如: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-8import inspectimport osdef get_root_path():    script_path=inspect.getfile(inspect.currentframe())    root=os.path.dirname(script_path)    return  rootcur=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-8r''' 处理文件'''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-8import defineimport file_utilif __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))


0 0
原创粉丝点击