Python os.pardir Method
os.pardir() gets the parent directory (the previous directory) of the current directory, displaying the directory name as a string.
Note: Windows and POSIX return ..
.
Python os.pardir Syntax
The pardir() method syntax format is as follows:
os.pardir
Python os.pardir Parameters
- None.
Python os.pardir Return Value
Returns the parent directory of the current directory, the default value is ..
.
Python os.pardir Example#1
The following example demonstrates the use of the pardir() method.
import os
# Output default value .
print(os.pardir)
Output:
Python os.pardir Example#2
Print the parent directory of the current directory.
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))
Output:
Python os.pardir Example#3
Get the parent node of the specified path.
import os
# path
path = "/home/deepinout/Camera"
print("Path:", path)
# parent
parent = os.path.join(path, os.pardir)
print("parent:", parent)
# prints the relative file path
# for the current directory (parent)
print("\nParent:", os.path.relpath(parent))
Output: