>>> import os
>>> path = "/folderA/folderB/folderC/folderD"
>>> if os.path.isdir(path):
dirname = os.path.basename(path)
UPDATE2: 正如 lars 指出的那样,进行更改以适应尾随的“/”。
>>> from os.path import normpath, basename
>>> basename(normpath('/folderA/folderB/folderC/folderD/'))
'folderD'
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 -
>>> 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 '/'.
>>> from os.path import normpath, basename
>>> basename(normpath('/folderA/folderB/folderC/folderD/'))
'folderD'
I was searching for a solution to get the last foldername where the file is located, I just used split two times, to get the right part. It's not the question but google transfered me here.
在我当前的项目中,我经常将路径的后部传递给函数,因此使用 Path 模块。为了以相反的顺序获取第 n 部分,我使用:
from typing import Union
from pathlib import Path
def get_single_subpath_part(base_dir: Union[Path, str], n:int) -> str:
if n ==0:
return Path(base_dir).name
for _ in range(n):
base_dir = Path(base_dir).parent
return getattr(base_dir, "name")
path= "/folderA/folderB/folderC/folderD/"
# for getting the last part:
print(get_single_subpath_part(path, 0))
# yields "folderD"
# for the second last
print(get_single_subpath_part(path, 1))
#yields "folderC"
此外,为了以包含剩余路径的路径的相反顺序传递第 n 部分,我使用:
from typing import Union
from pathlib import Path
def get_n_last_subparts_path(base_dir: Union[Path, str], n:int) -> Path:
return Path(*Path(base_dir).parts[-n-1:])
path= "/folderA/folderB/folderC/folderD/"
# for getting the last part:
print(get_n_last_subparts_path(path, 0))
# yields a `Path` object of "folderD"
# for second last and last part together
print(get_n_last_subparts_path(path, 1))
# yields a `Path` object of "folderc/folderD"
During my current projects, I'm often passing rear parts of a path to a function and therefore use the Path module. To get the n-th part in reverse order, I'm using:
from typing import Union
from pathlib import Path
def get_single_subpath_part(base_dir: Union[Path, str], n:int) -> str:
if n ==0:
return Path(base_dir).name
for _ in range(n):
base_dir = Path(base_dir).parent
return getattr(base_dir, "name")
path= "/folderA/folderB/folderC/folderD/"
# for getting the last part:
print(get_single_subpath_part(path, 0))
# yields "folderD"
# for the second last
print(get_single_subpath_part(path, 1))
#yields "folderC"
Furthermore, to pass the n-th part in reverse order of a path containing the remaining path, I use:
from typing import Union
from pathlib import Path
def get_n_last_subparts_path(base_dir: Union[Path, str], n:int) -> Path:
return Path(*Path(base_dir).parts[-n-1:])
path= "/folderA/folderB/folderC/folderD/"
# for getting the last part:
print(get_n_last_subparts_path(path, 0))
# yields a `Path` object of "folderD"
# for second last and last part together
print(get_n_last_subparts_path(path, 1))
# yields a `Path` object of "folderc/folderD"
Note that this function returns a Pathobject which can easily be converted to a string (e.g. str(path))
发布评论
评论(10)
使用
os.path.normpath
去掉任何结尾的斜杠,然后os. path.basename
为您提供路径的最后一部分:仅使用
basename
提供最后一个斜杠之后的所有内容,在本例中为''
。Use
os.path.normpath
to strip off any trailing slashes, thenos.path.basename
gives you the last part of the path:Using only
basename
gives everything after the last slash, which in this case is''
.对于 python 3,您可以使用
pathlib
模块(例如pathlib.PurePath
) :如果您想要文件所在的最后一个文件夹名称:
With python 3 you can use the
pathlib
module (pathlib.PurePath
for example):If you want the last folder name where a file is located:
您可以执行
UPDATE1:如果您将其指定为/folderA/folderB/folderC/folderD/xx.py,则此方法有效。这给出了 xx.py 作为基本名称。我猜这不是你想要的。所以你可以这样做 -
UPDATE2: 正如 lars 指出的那样,进行更改以适应尾随的“/”。
You could do
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 -
UPDATE2: As lars pointed out, making changes so as to accomodate trailing '/'.
这是我的方法:
Here is my approach:
我正在寻找一种解决方案来获取文件所在的最后一个文件夹名称,我只是使用了两次 split 来获取正确的部分。这不是问题,但谷歌把我转到这里了。
I was searching for a solution to get the last foldername where the file is located, I just used
split
two times, to get the right part. It's not the question but google transfered me here.如果您使用本机 python 包 pathlib ,这非常简单。
假设您有folderD 中某个文件的路径。
If you use the native python package pathlib it's really simple.
Suppose you have the path to a file in folderD.
我喜欢 Path 的 parts 方法:
I like the parts method of Path for this:
在我当前的项目中,我经常将路径的后部传递给函数,因此使用
Path
模块。为了以相反的顺序获取第 n 部分,我使用:此外,为了以包含剩余路径的路径的相反顺序传递第 n 部分,我使用:
请注意,此函数返回一个
Path< /code> 可以轻松转换为字符串的对象(例如
str(path)
)During my current projects, I'm often passing rear parts of a path to a function and therefore use the
Path
module. To get the n-th part in reverse order, I'm using:Furthermore, to pass the n-th part in reverse order of a path containing the remaining path, I use:
Note that this function returns a
Path
object which can easily be converted to a string (e.g.str(path)
)