Open In App

Get parent of current directory using Python

Improve
Improve
Like Article
Like
Save
Share
Report

In Python, OS module is used to interact with the operating system. It comes under Python’s standard utility modules. This module provides a portable way of using operating system 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 os.path.abspath() to get parent of current directory

os.path.abspath() can be used to get the parent directory. This method is used to get the normalized version of the path. This function also needs the help of os.path.join() and os.pardir(). 
os.path.join() method in Python join one or more path components intelligently. This method concatenates various path components with exactly one directory separator (‘/’) following each non-empty part except the last path component. If the last path component to be joined is empty then a directory separator (‘/’) is put at the end. 

Syntax: os.path.abspath(path)
Parameters: 
path: A path-like object representing a file system path.
Return Type: Returns a string that is a normalized version of the path. 

Example: 

Python3




# Python program to get parent
# directory
 
 
import os
 
# get current directory
path = os.getcwd()
print("Current Directory", path)
 
# prints parent directory
print(os.path.abspath(os.path.join(path, os.pardir)))


Output:  

 

Using os.path.dirname() to get parent of current directory

os.path.dirname() method in Python is used to get the directory name from the specified path. 

Syntax: os.path.dirname(path)
Parameter: 
path: A path-like object representing a file system path.
Return Type: This method returns a string value which represents the directory name from the specified path. 

Example:  

Python3




# Python program to get parent
# directory
 
 
import os
 
# get current directory
path = os.getcwd()
print("Current Directory", path)
print()
 
# parent directory
parent = os.path.dirname(path)
print("Parent directory", parent)


Output: 

 

Using os.path.relpath() and os.path.dirname()

In the above examples, getting the parent directory was limited to one level, i.e. we were only able to get the parent of current directory upto one level only. Suppose we want to find the parent to the parent directory, then the above code fails. This can be achieved by using os.path.relpath() and os.path.dirname() together. 
os.path.relpath() method in Python is used to get a relative filepath to the given path either from the current working directory or from the given directory.

Syntax: os.path.relpath(path, start = os.curdir)
Parameter: 

  • path: A path-like object representing the file system path. 
  • start (optional): A path-like object representing the file system path. 
    The relative path for given path will be computed with respect to the directory indicated by start. The default value of this parameter is os.curdir which is a constant string used by the operating system to refer to the current directory.

Return Type: This method returns a string value which represents the relative file path to given path from the start directory.0222  

To get the parent directory according to levels specified by the user, we will create a function getParent() which will take path and levels as arguments. Inside the function, a for loop will iterate level+1 numbers of time and os.path.dirname() will be called inside the for loop. Calling this function inside the for loop will give us the starting point from which os.path.relpath() will give the relative file path.

Example:

Python3




# Python program to get the
# parent directory
 
 
import os.path
 
# function to get parent
def getParent(path, levels = 1):
    common = path
 
    # Using for loop for getting
    # starting point required for
    # os.path.relpath()
    for i in range(levels + 1):
 
        # Starting point
        common = os.path.dirname(common)
 
    # Parent directory upto specified
    # level
    return os.path.relpath(path, common)
 
path = 'D:/Pycharm projects / GeeksforGeeks / Nikhil / gfg.txt'
print(getParent(path, 2))


Output:

 

Using Path().resolve().parents to get parent of current directory

Syntax: Path(path).resolve().parents[0]

Parameter: 

  • path: path of the file or folder whose parent we want to fetch.

Return Type: It returns WindowsPath.parents object.

Here we are using the path module to get the parent of current directory and instead of parents[0] we can also use parent.parent to fetch the parent of the current directory.

Example:

Python3




from pathlib import Path
 
d = Path("C:\\Users\\DELL\\Downloads").resolve().parents[0]
 
print(d)


Output:

 



Last Updated : 13 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads