Get Parent Directory in Python

  1. Get the Parent Directory in Python Using the path.parent() Method of the pathlib Module
  2. Get the Parent Directory in Python Using the pardir() Method of the os Module
  3. Get the Parent Directory in Python Using the dirname() Method of the os Module

This tutorial will explain various methods to get the parent directory of a path in Python. The parent directory is a directory that is above or higher than the given directory or file. For example, the parent directory of a path C:\folder\subfolder\myfile.txt is C:\folder\subfolder. Every directory except the root directory has a parent directory.

Get the Parent Directory in Python Using the path.parent() Method of the pathlib Module

The path.parent() method, as the name suggests, returns the parent directory of the given path passed as an argument in the form of a string. Therefore, to get the parent directory of a path, we need to pass the path string to the path.parent() method of the pathlib module.

The example code below demonstrates how to use path.parent() to get the parent directory of a path in Python:

from pathlib import Path

path1 = Path(r"C:\folder\subfolder\myfile.txt")
path2 = Path(r"C:\Myfile.txt")
print(path1.parent)
print(path2.parent)

Output:

C:\folder\subfolder
C:\

Get the Parent Directory in Python Using the pardir() Method of the os Module

os.pardir is a constant string referring to the parent directory. It is '..' for Windows and POSIX OS, and '::' for Mac OS.

When we combine the given path and os.pardir in the os.path.join() method, we could get the parent directory of the given directory.

The example code below demonstrates how to use the os.pardir and path.join() method of the os module to get the parent directory of a path:

import os.path

path1 = r"C:\folder\subfolder\myfile.txt"
path2 = r"C:\Myfile.txt"

print(os.path.abspath(os.path.join(path1, os.pardir)))
print(os.path.abspath(os.path.join(path2, os.pardir)))

Output:

C:\folder\subfolder
C:\

As mentioned above, we could also get the same result if we replace os.pardir with '..' if the OS is Windows or POSIX.

import os.path

path1 = r"C:\folder\subfolder\myfile.txt"
path2 = r"C:\Myfile.txt"

print(os.path.abspath(os.path.join(path1, '..')))
print(os.path.abspath(os.path.join(path2, '..')))

Output:

C:\folder\subfolder
C:\

Get the Parent Directory in Python Using the dirname() Method of the os Module

The dirname() method of the os module takes path string as input and returns the parent directory as output.

The example code below demonstrates how to use the dirname() to get the parent directory of a path:

import os.path

path1 = Path(r"C:\folder\subfolder\myfile.txt")
path2 = Path(r"C:\Myfile.txt")

print(os.path.dirname(path1))
print(os.path.dirname(path2))

Output:

C:\folder\subfolder
C:
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Directory

  • Create Directory in Python
  • Open All the Files in a Directory in Python
  • Related Article - Python Path

  • List Subdirectories in Python
  • Delete Files and Directories Using Python