我知道要返回到父目录,应该使用 ..
parentname = os.path.abspath(os.path.join(yourpath, os.path.pardir))
但是如果我想获取几个文件夹之前的目录名称怎么办?
比如说,我有一个路径 /stuff/home/blah/pictures/myaccount/album,我想要获取 "myaccount" 和 "album" 两个文件夹的名称(不是路径,只是名称)来在我的脚本中使用,我该怎么做呢?
.
或//
这样奇怪格式的合法路径时,这并不是特别健壮的。 - Nick Bastinos.path.abspath()
来预处理路径字符串。 - hzmnormpath
更健壮 - 我会更新它.. :-) - Nick Bastin