获取目录的姓氏:

str = "/folderA/folderB/folderC/folderD"
editstr = str.split("/")[-1]
print(editstr)

folderD


如何获取folderD之前的所有目录(没有最后一个斜杠)?例如:

editstr = ???
print(editstr)

/folderA/folderB/folderC

最佳答案

There is a module for this.

>>> import os
>>> s = "/folderA/folderB/folderC/folderD"
>>> os.path.basename(s)
'folderD'
>>> os.path.dirname(s)
'/folderA/folderB/folderC'

关于python - 如何获取字符串名称?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35232392/

10-10 05:37