如何在Python中查找两个目录?

发布于 2024-12-17 11:35:10 字数 265 浏览 3 评论 0 原文

我知道要转到父目录,您应该使用

parentname = os.path.abspath(os.path.join(yourpath, os.path.pardir))

但是如果我想获取几个文件夹上方的目录名称怎么办?

假设我得到了 /stuff/home/blah/pictures/myaccount/album,我想获取“myaccount”和“album”最后两个文件夹的名称(不是路径,只是名称)以在我的脚本。我该怎么做?

I know that to go up to a parent directory, you should use

parentname = os.path.abspath(os.path.join(yourpath, os.path.pardir))

But what if I want to get the name of a directory a few folders up?

Say I am given /stuff/home/blah/pictures/myaccount/album, and I want to get the names of the last two folders of "myaccount" and "album" (not the paths, just the names) to use in my script. How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不乱于心 2024-12-24 11:35:10
>>> p='/stuff/home/blah/pictures/myaccount/album'
>>> os.path.abspath(p).split(os.sep)[-1]
'album'
>>> os.path.abspath(p).split(os.sep)[-2]
'myaccount'
>>> os.path.abspath(p).split(os.sep)[-3]
'pictures'
>>> os.path.abspath(p).split(os.sep)[-4]
'blah'

ETC...

>>> p='/stuff/home/blah/pictures/myaccount/album'
>>> os.path.abspath(p).split(os.sep)[-1]
'album'
>>> os.path.abspath(p).split(os.sep)[-2]
'myaccount'
>>> os.path.abspath(p).split(os.sep)[-3]
'pictures'
>>> os.path.abspath(p).split(os.sep)[-4]
'blah'

etc...

三岁铭 2024-12-24 11:35:10

看起来没有什么特别优雅的东西,但这应该可以解决问题:

>>> yourpath = "/stuff/home/blah/pictures/myaccount/album"
>>> import os.path
>>> yourpath = os.path.abspath(yourpath)
>>> (npath, d1) = os.path.split(yourpath)
>>> (npath, d2) = os.path.split(npath)
>>> print d1
album
>>> print d2
myaccount

请记住 os.path.split 将为第二个组件返回一个空字符串,因此您可能需要确保如果您不以其他方式验证格式,请先将其删除提供的路径。

There doesn't look to be anything particularly elegant, but this should do the trick:

>>> yourpath = "/stuff/home/blah/pictures/myaccount/album"
>>> import os.path
>>> yourpath = os.path.abspath(yourpath)
>>> (npath, d1) = os.path.split(yourpath)
>>> (npath, d2) = os.path.split(npath)
>>> print d1
album
>>> print d2
myaccount

Keep in mind that os.path.split will return an empty string for the second component if the supplied path ends in a trailing slash, so you might want to make sure you strip that off first if you don't otherwise validate the format of the supplied path.

别理我 2024-12-24 11:35:10

将路径拆分为列表并获取最后两个元素怎么样?

>>> import os
>>> path_str = ' /stuff/home/blah/pictures/myaccount/album'
>>> path_str.split(os.sep)
[' ', 'stuff', 'home', 'blah', 'pictures', 'myaccount', 'album']

对于...等相对路径,可以使用os.path.abspath()来预处理路径字符串。

>>> import os
>>> path_str = os.path.abspath('.')
>>> path_str.split(os.sep)
['', 'tmp', 'foo', 'bar', 'foobar']

What about splitting the path to list and get the last two elements?

>>> import os
>>> path_str = ' /stuff/home/blah/pictures/myaccount/album'
>>> path_str.split(os.sep)
[' ', 'stuff', 'home', 'blah', 'pictures', 'myaccount', 'album']

For the relative path such as . and .., os.path.abspath() can be used to pre-process the path string.

>>> import os
>>> path_str = os.path.abspath('.')
>>> path_str.split(os.sep)
['', 'tmp', 'foo', 'bar', 'foobar']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文