相关疑难解决方法(0)

如何获取父目录位置

这段代码是在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)

python path

123
推荐指数
6
解决办法
12万
查看次数

标签 统计

path ×1

python ×1