如何获取父目录位置

zjm*_*126 123 python path

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

Mar*_*tos 159

你可以反复使用dirname来爬高:dirname(dirname(file)).但是,这只能到根包.如果这是一个问题,请使用os.path.abspath:dirname(dirname(abspath(file))).

  • 我知道OP知道`dirname`.每个人都不明白将dirname应用于目录会产生父目录. (37认同)
  • `dirname`确实**NOT**总是返回父目录; http://twitter.com/#!/ActiveState/status/671049326788608 (4认同)
  • @Sridhar:这取决于你的观点; 我认为一个以`/`结尾的路径不代表叶子目录条目本身,但它的内容,这就是为什么,例如,`mv xxx yyy /`如果`yyy`不是一个预先存在的目录就会失败.在任何情况下,即使我们把你的观点视为一个给定的,它在我的答案的背景下是无关紧要的.`file`和`dirname`的结果都不会以`/`结尾. (2认同)

joe*_*ler 54

os.path.abspath不验证任何东西,所以如果我们已经附加了字符串,__file__就不需要打扰dirname或加入或者任何一个.只需__file__视为目录并开始攀登:

# climb to __file__'s parent's parent:
os.path.abspath(__file__ + "/../../")
Run Code Online (Sandbox Code Playgroud)

这远比那些os.path.abspath(os.path.join(os.path.dirname(__file__),".."))可管理的复杂得多dirname(dirname(__file__)).攀登超过两个级别开始变得荒谬.

但是,既然我们知道要攀爬多少级别,我们可以通过一个简单的小功能来清理它:

uppath = lambda _path, n: os.sep.join(_path.split(os.sep)[:-n])

# __file__ = "/aParent/templates/blog1/page.html"
>>> uppath(__file__, 1)
'/aParent/templates/blog1'
>>> uppath(__file__, 2)
'/aParent/templates'
>>> uppath(__file__, 3)
'/aParent'
Run Code Online (Sandbox Code Playgroud)

  • os.path.abspath(os.path.join(__ file__,“ ..”,“ ..”))会更便携吗? (3认同)
  • 这很好,但如果标准库添加了一个方便的功能来完成这个也很酷...不想每次我需要这个功能时来到SO (2认同)

Gav*_*hen 29

使用相对路径pathlib在Python 3.4+模块:

from pathlib import Path

Path(__file__).parent
Run Code Online (Sandbox Code Playgroud)

您可以使用多个调用parent在路径中进一步:

Path(__file__).parent.parent
Run Code Online (Sandbox Code Playgroud)

作为指定parent两次的替代方法,您可以使用

Path(__file__).parents[1]
Run Code Online (Sandbox Code Playgroud)

  • 如果您需要将路径作为字符串来修改,则可以使用“str(Path(__file__).parent)”。 (2认同)
  • 似乎在 python 3.8.5 中你需要 `Path(__file__).resolve().parent` (2认同)
  • 谢谢,我知道“.parent”,但不知道“.parents”。IMO,这比链接“.parent”或“os.path.dirname”要好得多。 (2认同)

Fel*_*ing 11

os.path.dirname(os.path.abspath(__file__))
Run Code Online (Sandbox Code Playgroud)

应该给你的路径a.

但如果b.py是当前执行的文件,那么你可以通过这样做来实现同样的目标

os.path.abspath(os.path.join('templates', 'blog1', 'page.html'))
Run Code Online (Sandbox Code Playgroud)


Sun*_*wen 9

os.pardir是一种更好的方式../,更具可读性.

import os
print os.path.abspath(os.path.join(given_path, os.pardir))  
Run Code Online (Sandbox Code Playgroud)

这将返回given_path的父路径


Mun*_*Ali 5

一种简单的方法可以是:

import os
current_dir =  os.path.abspath(os.path.dirname(__file__))
parent_dir = os.path.abspath(current_dir + "/../")
print parent_dir
Run Code Online (Sandbox Code Playgroud)