Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.pardir Method

Python3 os.pardir Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

os.pardir() gets the parent directory (the level above) of the current directory, showing the directory name as a string.

Note: Windows and POSIX return ..

Syntax

The **pardir()** method syntax is as follows:


os.pardir

Parameters

Return Value

Returns the parent directory of the current directory, defaulting to ..

Example

The following example demonstrates the use of the pardir() method:

Example

import

os

 

Output the default value ..

print

(

os

.

pardir

)

The output of the above program is:


..

Print the parent directory of the current directory:

Example

import

os

 

 

Current working directory

path

=

os

.

getcwd

(

)

print

(

"Current working directory: "

,

path

)

 

Parent directory

parent

=

os

.

path

.

join

(

path

,

os

.

pardir

)

 

Parent directory

print

(

"\nParent directory:"

,

os

.

path

.

abspath

(

parent

)

)

The output of the above program is:

当前工作目录:  /Users/runoob/python

父目录: /Users/runoob

Python3 OS File/Directory Methods Python3 OS File/Directory Methods