例如: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))