How to get only the last part of a path in Python?

后端 未结 9 531
长发绾君心
长发绾君心 2020-11-28 19:01

In Python, suppose I have a path like this:

/folderA/folderB/folderC/folderD/

How can I get just the folderD part?

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 19:43

    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.

    pathname = "/folderA/folderB/folderC/folderD/filename.py"
    head, tail = os.path.split(os.path.split(pathname)[0])
    print(head + "   "  + tail)
    

提交回复
热议问题