我的目录结构如下
/home/damon/dev/python/misc/path/
/project/mycode.py
/app/templates/
我需要从 mycode.py 中获取模板文件夹的绝对路径
我试着把mycode.py写成
import os
if __name__=='__main__':
PRJ_FLDR=os.path.dirname(os.path.abspath(__file__))
print 'PRJ_FLDR=',PRJ_FLDR
apptemplates = os.path.join(PRJ_FLDR,'../app/templates')
print 'apptemplates=',apptemplates
我希望应用程序模板是
/home/damon/dev/python/misc/path/app/templates
但是我得到了
/home/damon/dev/python/misc/path/project/../app/templates
如何获得正确的路径?
请您参考如下方法:
那个路径是是正确的,试一试。但是,如果您想为了清晰起见删除多余的“project/../”部分,请使用 os.path.normpath
os.path.normpath(path)Normalize a pathname. This collapses redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B.
http://docs.python.org/2/library/os.path.html#os.path.normpath
