Python 等效于os.getpardir()?
有没有比执行以下操作更简单的方法来获取父目录的字符串Python 等效于os.getpardir()?,python,Python,有没有比执行以下操作更简单的方法来获取父目录的字符串 '/'.join(os.getcwd().split('/')[:-1]) 我不知道有什么方法可以做到这一点,但我会这样做: os.path.abspath(os.path.join(os.getcwd(), os.pardir)) 编辑:这种方法有效,但可能更好。我不知道有什么方法可以做到这一点,但我会做一些类似的事情: os.path.abspath(os.path.join(os.getcwd(), os.pardir)) 编辑:
'/'.join(os.getcwd().split('/')[:-1])
我不知道有什么方法可以做到这一点,但我会这样做:
os.path.abspath(os.path.join(os.getcwd(), os.pardir))
编辑:这种方法有效,但可能更好。我不知道有什么方法可以做到这一点,但我会做一些类似的事情:
os.path.abspath(os.path.join(os.getcwd(), os.pardir))
编辑:这种方法有效,但可能更好。我相信这对你们来说会简单得多
os.path.abspath(os.path.pardir)
甚至更简单
os.path.dirname(os.getcwd())
我相信这对你来说会简单得多
os.path.abspath(os.path.pardir)
甚至更简单
os.path.dirname(os.getcwd())
这个方法可能有用:
os.path.dirname(os.getcwd())
这个方法可能有用:
os.path.dirname(os.getcwd())
可能重复的请不要像这样假设
os.set=='/'
。在os.path
:split
,join
,basename
,等等中有现成的函数用于此。可能重复的请不要像这样假定os.set=='/'
。在os.path
,split
,join
,basename
,等等中有现成的函数用于此。os.getcwd()已经是绝对路径,因此os.path.abspath是冗余的,os.path.join(x,os.pardir)几乎与os.path.dirname(x)相同os.path.join(os.getcwd(),os.pardir)
基本上只是将/..
附加到路径,在路径上调用os.path.abspath
,然后将其转换回原来的路径dirname
可能是一种更好的方法,我编辑了我的答案以反映.os.getcwd()已经是绝对路径,因此os.path.abspath是冗余的,os.path.join(x,os.pardir)几乎与os.path.dirname(x)os.path.join(os.getcwd(),os.pardir)
基本上只是将/../code>附加到路径上,对其调用os.path.abspath
,然后将其转换回原来的状态dirname
可能是一种更好的方法,我编辑了我的答案以反映这一点。