Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 等效于os.getpardir()?_Python - Fatal编程技术网

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
可能是一种更好的方法,我编辑了我的答案以反映这一点。