如何在Python中获取父目录?

本文翻译自:How do I get the parent directory in Python?

Could someone tell me how to get the parent directory of a path in Python in a cross platform way. 有人能告诉我如何以跨平台的方式在Python中获取路径的父目录。 Eg 例如

C:\Program Files ---> C:\

and

C:\ ---> C:\

If the directory doesn't have a parent directory, it returns the directory itself. 如果目录没有父目录,则返回目录本身。 The question might seem simple but I couldn't dig it up through Google. 问题可能看似简单,但我无法通过谷歌进行挖掘。


#1楼

参考:https://stackoom.com/question/C03V/如何在Python中获取父目录


#2楼

import os
print"------------------------------------------------------------"
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
print("example 1: "+SITE_ROOT)
PARENT_ROOT=os.path.abspath(os.path.join(SITE_ROOT, os.pardir))
print("example 2: "+PARENT_ROOT)
GRANDPAPA_ROOT=os.path.abspath(os.path.join(PARENT_ROOT, os.pardir))
print("example 3: "+GRANDPAPA_ROOT)
print "------------------------------------------------------------"

#3楼

GET Parent Directory Path and make New directory (name new_dir ) 获取父目录路径创建新目录 (名称为new_dir

Get Parent Directory Path 获取父目录路径

os.path.abspath('..')
os.pardir

Example 1 例1

import os
print os.makedirs(os.path.join(os.path.dirname(__file__), os.pardir, 'new_dir'))

Example 2 例2

import os
print os.makedirs(os.path.join(os.path.dirname(__file__), os.path.abspath('..'), 'new_dir'))

#4楼

os.path.abspath('D:\Dir1\Dir2\..')

>>> 'D:\Dir1'

So a .. helps 所以..帮助


#5楼

import os.path

os.path.abspath(os.pardir)

#6楼

An alternate solution of @kender @kender的另一种解决方案

import os
os.path.dirname(os.path.normpath(yourpath))

where yourpath is the path you want the parent for. 其中yourpath是您希望父级的路径。

But this solution is not perfect, since it will not handle the case where yourpath is an empty string, or a dot. 但是这个解决方案并不完美,因为它不会处理你的yourpath是空字符串或点的情况。

This other solution will handle more nicely this corner case: 这个其他解决方案将更好地处理这个角落的情况:

import os
os.path.normpath(os.path.join(yourpath, os.pardir))

Here the outputs for every case that can find (Input path is relative): 这里可以找到的每种情况的输出(输入路径是相对的):

os.path.dirname(os.path.normpath('a/b/'))          => 'a'
os.path.normpath(os.path.join('a/b/', os.pardir))  => 'a'

os.path.dirname(os.path.normpath('a/b'))           => 'a'
os.path.normpath(os.path.join('a/b', os.pardir))   => 'a'

os.path.dirname(os.path.normpath('a/'))            => ''
os.path.normpath(os.path.join('a/', os.pardir))    => '.'

os.path.dirname(os.path.normpath('a'))             => ''
os.path.normpath(os.path.join('a', os.pardir))     => '.'

os.path.dirname(os.path.normpath('.'))             => ''
os.path.normpath(os.path.join('.', os.pardir))     => '..'

os.path.dirname(os.path.normpath(''))              => ''
os.path.normpath(os.path.join('', os.pardir))      => '..'

os.path.dirname(os.path.normpath('..'))            => ''
os.path.normpath(os.path.join('..', os.pardir))    => '../..'

Input path is absolute (Linux path): 输入路径是绝对的(Linux路径):

os.path.dirname(os.path.normpath('/a/b'))          => '/a'
os.path.normpath(os.path.join('/a/b', os.pardir))  => '/a'

os.path.dirname(os.path.normpath('/a'))            => '/'
os.path.normpath(os.path.join('/a', os.pardir))    => '/'

os.path.dirname(os.path.normpath('/'))             => '/'
os.path.normpath(os.path.join('/', os.pardir))     => '/'

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/107512149