Easy Tutorial
❮ Python Odd Even Python Func Print ❯

Python3 os.pardir Method

Python3 OS File/Directory Methods


Overview

The os.pardir() method retrieves the parent directory of the current directory, displaying the directory name as a string.

Note: Windows and POSIX return ...

Syntax

The syntax for the pardir() method is as follows:

os.pardir

Parameters

-

None.

Return Value

Returns the parent directory of the current directory, with the default value being ...

Example

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

Example

import os 

# Output the default value ..
print(os.pardir)

Executing the above program outputs:

..

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

Executing the above program outputs:

Current working directory:  /Users/tutorialpro/python

Parent directory: /Users/tutorialpro

Python3 OS File/Directory Methods

❮ Python Odd Even Python Func Print ❯