How to get the parent directory in Python?

This article is translated from: 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 . eg _

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

and 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.


#1st Floor

Reference: https://stackoom.com/question/C03V/ How to get parent directory in Python


#2nd Floor

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 "------------------------------------------------------------"

#3rd floor

GET Parent Directory Path and make New directory (name new_dir) Get the parent directory path and make a new directory (name new_dir)

Get Parent Directory Path to get the parent directory path

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

Example 1Example 1

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

Example 2Example 2

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

#4th floor

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

>>> 'D:\Dir1'

.. So a helps _..


#5th Floor

import os.path

os.path.abspath(os.pardir)

#6th floor

An alternate solution of @kender @kender's another solution

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

where yourpath is the path you want the parent for .yourpath

yourpath But this solution is not perfect, since it will not handle the case where 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): The input path is absolute (Linux path):

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