gpt4 book ai didi

python - 如何获取字符串中的名字?

转载 作者:行者123 更新时间:2023-11-30 23:00:32 28 4
gpt4 key购买 nike

获取目录的姓氏:

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com