Get parent of current directory using Python
Last Updated :
12 Jul, 2025
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
Python
from pathlib import Path
cwd = Path.cwd()
parent = cwd.parent
print(cwd)
print(parent)
Output
Current parent directoryExplanation: Path.cwd() returns a Path object representing the current working directory. .parent gives the directory one level above.
Example 2: Go up multiple levels
Python
from pathlib import Path
cwd = Path.cwd()
n = 2 # levels up
parent = cwd.parents[n - 1]
print(parent)
Output
Parent directory 2 levels upExplanation: 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.
Python
import os
cwd = os.getcwd()
parent = os.path.abspath(os.path.join(cwd, os.pardir))
print(cwd)
print(parent)
Output
Current and absolute parent directoryExplanation:
- 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
Python
import os
cwd = os.getcwd()
parent = os.path.dirname(cwd)
print(cwd)
print(parent)
Output
Current parent directoryExplanation: 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
Python
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
Parent directory 2 levels upExplanation: 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.
Python
import os
cwd = os.getcwd()
parent = os.path.normpath(os.path.join(cwd, '..', '..'))
print(parent)
Output
Normalized parent path 2 levels upExplanation:
- 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.
Similar Reads
Get Current directory in Python In this article, we will cover How to Get and Change the Working Directory in Python. While working with file handling you might have noticed that files are referenced only by their names, e.g. 'GFG.txt' and if the file is not located in the directory of the script, Python raises an error. The conce
3 min read
How to Get directory of Current Script in Python? A Parent directory is a directory above another file/directory in a hierarchical file system. Getting the Parent directory is essential in performing certain tasks related to filesystem management. In this article, we will take a look at methods used for obtaining the Parent directory of the curren
4 min read
How to Create Directory If it Does Not Exist using Python? In this article, We will learn how to create a Directory if it Does Not Exist using Python. Method 1: Using os.path.exists() and os.makedirs() methods Under this method, we will use exists() method takes path of demo_folder as an argument and returns true if the directory exists and returns false if
2 min read
Check if directory contains files using python Finding if a directory is empty or not in Python can be achieved using the listdir() method of the os library. OS module in Python provides functions for interacting with the operating system. This module provides a portable way of using operating system dependent functionality. Syntax: os.listdir(
3 min read
Get Location of Python site-packages Directory A Python installation has a site-packages directory inside the module directory. This directory is where user-installed packages are dropped. A .pth file in this directory is maintained, which contains paths to the directories where the extra packages are installed. In this article, you will learn h
2 min read
How to print all files within a directory using Python? The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to
3 min read