Get parent of current directory using Python
In Python, the OS module is used to interact with the operating system. It comes under Python's standard utility modules and provides a portable way of using OS-dependent functionality. The os and os.path modules include many functions to interact with the file system. OS module provides various ways for getting the parent directory. Some of the ways are:
Using pathlib.Path
pathlib module is modern and more readable than the older os.path methods. It treats file paths like objects, which makes your code clean and easy to understand.
Example 1: Get immediate parent directory
from pathlib import Path
cwd = Path.cwd()
parent = cwd.parent
print(cwd)
print(parent)
Output

Explanation: Path.cwd() returns a Path object representing the current working directory. .parent gives the directory one level above.
Example 2: Go up multiple levels
from pathlib import Path
cwd = Path.cwd()
n = 2 # levels up
parent = cwd.parents[n - 1]
print(parent)
Output

Explanation: cwd.parents is a list-like object. Use parents[1] for 2 levels up, parents[2] for 3 levels up, etc.
Using os.path.abspath()
This method uses os.path utilities to construct and normalize a path pointing to the parent directory.
import os
cwd = os.getcwd()
parent = os.path.abspath(os.path.join(cwd, os.pardir))
print(cwd)
print(parent)
Output

Explanation:
- os.getcwd() returns the current directory.
- os.pardir equals '..' (parent path).
- os.path.join() joins components.
- os.path.abspath() turns it into a clean absolute path.
Using os.path.dirname()
os.path.dirname() extracts the directory portion of a path. Using it once returns the immediate parent; calling it in a loop lets you go multiple levels up. It's a straightforward approach for directory trimming.
Example 1: Get immediate parent directory
import os
cwd = os.getcwd()
parent = os.path.dirname(cwd)
print(cwd)
print(parent)
Output

Explanation: os.getcwd() returns the current working directory and os.path.dirname(cwd) removes the last segment of the path giving us the parent.
Example 2: Go multiple levels up
import os
def parent_dir(path, levels=1):
for _ in range(levels):
path = os.path.dirname(path)
return path
cwd = os.getcwd()
parent_2 = parent_dir(cwd, 2)
print(parent_2)
Output

Explanation: parent_dir() function takes a path and a number of levels to move up, using a loop that repeatedly applies os.path.dirname() to trim the path. os.getcwd() gets the current directory and calling parent_dir(cwd, 2) moves up two levels.
Using os.path.normpath()
By manually joining '..' segments with the current path and normalizing it with os.path.normpath(), this method provides a cleaned-up path pointing to the parent. It’s useful when constructing complex directory structures manually.
import os
cwd = os.getcwd()
parent = os.path.normpath(os.path.join(cwd, '..', '..'))
print(parent)
Output

Explanation:
- os.getcwd() gets the current working directory and os.path.join(cwd, '..', '..') builds a path that goes two levels up using '..'.
- os.path.normpath() cleans and standardizes the path, making it safe and OS-independent.