如何在Python中只获取路径的最后一部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3925096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get only the last part of a path in Python?
提问by pepero
In Python, suppose I have a path like this:
在 Python 中,假设我有这样的路径:
/folderA/folderB/folderC/folderD/
How can I get just the folderDpart?
我怎样才能得到那folderD部分?
采纳答案by Fred Foo
Use os.path.normpath, then os.path.basename:
使用os.path.normpath,然后os.path.basename:
>>> os.path.basename(os.path.normpath('/folderA/folderB/folderC/folderD/'))
'folderD'
The first strips off any trailing slashes, the second gives you the last part of the path. Using only basenamegives everything after the last slash, which in this case is ''.
第一个去掉任何尾部斜杠,第二个给你路径的最后一部分。Using onlybasename给出最后一个斜杠之后的所有内容,在这种情况下是''.
回答by Srikar Appalaraju
You could do
你可以做
>>> import os
>>> os.path.basename('/folderA/folderB/folderC/folderD')
UPDATE1:This approach works in case you give it /folderA/folderB/folderC/folderD/xx.py. This gives xx.py as the basename. Which is not what you want I guess. So you could do this -
UPDATE1:如果你给它/folderA/folderB/folderC/folderD/xx.py,这种方法有效。这给出了 xx.py 作为基本名称。我猜这不是你想要的。所以你可以这样做 -
>>> import os
>>> path = "/folderA/folderB/folderC/folderD"
>>> if os.path.isdir(path):
dirname = os.path.basename(path)
UPDATE2:As lars pointed out, making changes so as to accomodate trailing '/'.
UPDATE2:正如 lars 所指出的,进行更改以适应尾随的“/”。
>>> from os.path import normpath, basename
>>> basename(normpath('/folderA/folderB/folderC/folderD/'))
'folderD'
回答by GSto
path = "/folderA/folderB/folderC/folderD/"
last = path.split('/').pop()
回答by Andrew Sledge
str = "/folderA/folderB/folderC/folderD/"
print str.split("/")[-2]
回答by mshsayem
A naive solution(Python 2.5.2+):
一个天真的解决方案(Python 2.5.2+):
s="/path/to/any/folder/orfile"
desired_dir_or_file = s[s.rindex('/',0,-1)+1:-1] if s.endswith('/') else s[s.rindex('/')+1:]
回答by user1767754
I was searching for a solution to get the last foldername where the file is located, I just used splittwo times, to get the right part. It's not the question but google transfered me here.
我正在寻找一种解决方案来获取文件所在的最后一个文件夹名,我只使用split了两次,以获得正确的部分。这不是问题,但谷歌将我转移到这里。
pathname = "/folderA/folderB/folderC/folderD/filename.py"
head, tail = os.path.split(os.path.split(pathname)[0])
print(head + " " + tail)
回答by Mike Mitterer
Here is my approach:
这是我的方法:
>>> import os
>>> print os.path.basename(
os.path.dirname('/folderA/folderB/folderC/folderD/test.py'))
folderD
>>> print os.path.basename(
os.path.dirname('/folderA/folderB/folderC/folderD/'))
folderD
>>> print os.path.basename(
os.path.dirname('/folderA/folderB/folderC/folderD'))
folderC
回答by jinnlao
With python 3 you can use the pathlibmodule (pathlib.PurePathfor example):
使用 python 3,您可以使用pathlib模块(pathlib.PurePath例如):
>>> import pathlib
>>> path = pathlib.PurePath('/folderA/folderB/folderC/folderD/')
>>> path.name
'folderD'
If you want the last folder name where a file is located:
如果您想要文件所在的最后一个文件夹名称:
>>> path = pathlib.PurePath('/folderA/folderB/folderC/folderD/file.py')
>>> path.parent.name
'folderD'

