这段代码是在b.py中获取templates/blog1/page.html:
path = os.path.join(os.path.dirname(__file__), os.path.join('templates', 'blog1/page.html'))
Run Code Online (Sandbox Code Playgroud)
但我想获得父目录位置:
aParent
|--a
| |---b.py
| |---templates
| |--------blog1
| |-------page.html
|--templates
|--------blog1
|-------page.html
Run Code Online (Sandbox Code Playgroud)
以及如何获得aParent位置
谢谢
更新:
这是正确的:
dirname=os.path.dirname
path = os.path.join(dirname(dirname(__file__)), os.path.join('templates', 'blog1/page.html'))
Run Code Online (Sandbox Code Playgroud)
要么
path = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
Run Code Online (Sandbox Code Playgroud)