python - 如何获取父目录位置
这段代码是在b.py中获取templates / blog1 / page.html:
path = os.path.join(os.path.dirname(__file__), os.path.join('templates', 'blog1/page.html'))
但我想获得父目录位置:
aParent
|--a
| |---b.py
| |---templates
| |--------blog1
| |-------page.html
|--templates
|--------blog1
|-------page.html
以及如何获得aPartment位置
谢谢
更新:
这是正确的:
dirname=os.path.dirname
path = os.path.join(dirname(dirname(__file__)), os.path.join('templates', 'blog1/page.html'))
要么
path = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
zjm1126 asked 2019-06-27T10:07:24Z
11个解决方案
148 votes
您可以反复应用dirname以爬升:dirname(dirname(file))。但是,这只能到根包。 如果这是一个问题,请使用os.path.abspath:dirname(dirname(abspath(file)))。
Marcelo Cantos answered 2019-06-27T10:07:38Z
49 votes
os.path.abspath(os.path.join(os.path.dirname(__file__),".."))不验证任何内容,因此如果我们已经将字符串附加到dirname(dirname(__file__)),则无需使用dirname或加入或其中任何一项。 只需将__file__视为目录并开始攀登:
# climb to __file__'s parent's parent:
os.path.abspath(__file__ + "/../../")
这比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'
joemaller answered 2019-06-27T10:08:20Z
22 votes
在Python 3.4+中使用parent模块的相对路径:
from pathlib import Path
Path(__file__).parent
您可以使用多次调用parent进一步查看路径:
Path(__file__).parent.parent
作为两次指定parent的替代方法,您可以使用
Path(__file__).parents[1]
Gavriel Cohen answered 2019-06-27T10:09:02Z
10 votes
os.path.dirname(os.path.abspath(__file__))
应该给你b.py的路径。
但是如果b.py是当前执行的文件,那么你可以通过这样做来实现同样的目标
os.path.abspath(os.path.join('templates', 'blog1', 'page.html'))
Felix Kling answered 2019-06-27T10:09:36Z
8 votes
os.pardir是更好的方式../,更具可读性。
import os
print os.path.abspath(os.path.join(given_path, os.pardir))
这将返回given_path的父路径
Sun Liwen answered 2019-06-27T10:10:11Z
4 votes
一个简单的方法可以是:
import os
current_dir = os.path.abspath(os.path.dirname(__file__))
parent_dir = os.path.abspath(current_dir + "/../")
print parent_dir
Muneeb Ali answered 2019-06-27T10:10:39Z
3 votes
可以加入两个..文件夹,获取父文件夹的父文件?
path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"..",".."))
YOU answered 2019-06-27T10:11:07Z
3 votes
使用以下内容跳转到上一个文件夹:
os.chdir(os.pardir)
如果你需要多次跳转,那么在这种情况下,一个简单的解决方案就是使用一个简单的装饰器。
Marco smdm answered 2019-06-27T10:11:43Z
1 votes
这是另一个相对简单的解决方案:
不使用normpath(在“file.txt”等一个级别的参数或“..”之类的相对父级上不能正常工作)
不使用normpath(避免对当前工作目录的任何假设),而是保留路径的相对特征
它只使用normpath和join:
def parent(p):
return os.path.normpath(os.path.join(p, os.path.pardir))
# Example:
for p in ['foo', 'foo/bar/baz', 'with/trailing/slash/',
'dir/file.txt', '../up/', '/abs/path']:
print parent(p)
结果:
.
foo/bar
with/trailing
dir
..
/abs
Stefaan answered 2019-06-27T10:12:36Z
0 votes
我认为使用这个更好:
os.path.realpath(__file__).rsplit('/', X)[0]
In [1]: __file__ = "/aParent/templates/blog1/page.html"
In [2]: os.path.realpath(__file__).rsplit('/', 3)[0]
Out[3]: '/aParent'
In [4]: __file__ = "/aParent/templates/blog1/page.html"
In [5]: os.path.realpath(__file__).rsplit('/', 1)[0]
Out[6]: '/aParent/templates/blog1'
In [7]: os.path.realpath(__file__).rsplit('/', 2)[0]
Out[8]: '/aParent/templates'
In [9]: os.path.realpath(__file__).rsplit('/', 3)[0]
Out[10]: '/aParent'
dongweiming answered 2019-06-27T10:13:04Z
0 votes
我试过了:
import os
os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))), os.pardir))
spicyramen answered 2019-06-27T10:13:28Z