[英]Python: Extract folder name from path
I have a path as a string in python: 我在python中有一个路径作为字符串:
path = "/folder_a/folder_b/folder_c/folder_d"
How can I extract the name of the last folder ("folder_d") from the path
? 如何从
path
提取最后一个文件夹的名称(“ folder_d”)?
You can use os.path.basename : 您可以使用os.path.basename :
>>> path = "/folder_a/folder_b/folder_c/folder_d"
>>> import os
>>> os.path.basename(path)
'folder_d'
Maybe you could try this: 也许您可以尝试以下方法:
y=path.split('/')
last_folder=y[-1]
Which is the last item in the list 列表中的最后一项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.