Get Parent Directory in Python

[ad_1]

Get parent directory in python.

A simple way to understand the parent directory is to think of it as a directory that is at a higher position in the hierarchy from the specified directory. All the directories must have a parent directory, with the root directory being the only exception to this statement. This tutorial focuses on the different ways in which we can get parent directory in python.

The first part of the article demonstrates the different ways available to get parent directory in Python, while the latter part demonstrates the different ways available to get parent directory two levels up in python.

how To Get Parent Directory in Python?

The first three of the four mentioned methods below utilize the os module for implementation.

  • Using the os.path.abspath() function along with the os.pardir string.
  • Using the os.path.dirname() function.
  • Using the os.path.relpath() function along with the os.path.dirname() function.
  • Using the pathlib module functions.

Using the pathlib module functions to get parent directory in python.

The pathib module provides various different classes and functions that are utilized in dealing with file path systems in Python. This module was introduced in Python 3.4 and only works in versions released after it.

The path.parent() function from the pathlib module carries out the process of returning the parent directory to the given specified directory passed as the argument of the function. The given directory needs to be passed as a string to this function.

The following code uses the pathlib module functions to get parent directory in python.


The above code provides the following output:

/Users/Java2blog/files

Using the os.path.dirname() function to get parent directory in python.

The os.path.dirname() function simply returns the parent directory as the output while taking the current directory as the input by passing it as an argument.

Similar to the method above, the os.path.dirname() function is also a part of the OS module that needs to be imported to the given code in order to implement this method.

The following code uses the os.path.dirname() function to get parent directory in python.


The above code provides the following output:

Current Directory: /tmp/sessions/bec378e70356b79f
Parent Directory: /tmp/sessions

Using the os.path.abspath() function along with the os.pardir string to get parent directory in python.

The os.path.abspath() function is utilized in getting the absolutized and normalized function of the given specified path. The os.pardir string is utilized to refer to the parent directory by the operating system and is utilized along with the os.path.abspath() function and the os.path.join() function, which is basically utilized for joining multiple components of the path in a clever manner.

The OS module needs to be imported to the Python code in order to implement this method.

The following code uses the os.path.abspath() function along with the os.pardir string to get parent directory in python.


The above code provides the following output:

Current Directory: /tmp/sessions/7c10feef200f71d3
Parent Directory: /tmp/sessions

Using the os.path.relpath() function along with the os.path.dirname() function to get parent directory in python.

The os.relpath() function basically provides the relative filepath to a given specified directory.

The os.relpath() function can be utilized with the os.path.dirname() function to not only find out the parent directory but also the grandparent directory.

The following code uses the os.path.relpath() function along with the os.path.dirname() function to get parent directory in python.


The above code provides the following output:

Projects / aa.md

We should note the os.path.relpath() function does not check the existence of the specified and only works on calculating the relative path.

how To Get Parent Directory Two Levels up In Python?

  • Using the pathlib module.
  • Using the os.path.abspath() function along with the os.path.join() function.
  • Using the os.path.dirname() function twice.
  • Using the os.path.relpath() function along with the os.path.dirname() function.

Using the pathlib module to get parent directory two levels up in python.

The pathlib module functions not only help in getting the parent directory, but they can also help in getting the parent directory two levels up or simply the grandparent directory in python.

The following code uses the pathlib module to get parent directory two levels up in python.


The above code provides the following output:

/Users/Java2blog

In the above code, the defined sequence parents is utilized to gain access to the parent directories and the number 1 specified with it indicates the second level of the parent directory being specified.

Using the os.path.abspath() function along with the os.path.join() function to get parent directory two levels up in python.

If we use the os.path.abspath() function along with the os.path.join() function together in a certain way, we can implement the task of getting parent directory two levels up in Python. The os module needs to be imported to the python code before proceeding with this method.

The following code uses the os.path.abspath() function along with the os.path.join() function to get parent directory two levels up in python.


The above code provides the following output:

/Users/Java2blog

Using the os.path.dirname() function twice to get parent directory two levels up in python.

We now already know the use and working of the os.path.dirname() function as mentioned in the first part of the article. The os.path.dirname() function can be intelligently utilized twice to get the parent directory two levels up in Python.

The following code uses the os.path.dirname() function twice to get parent directory two levels up in python.

The above code provides the following output:

/Users/Java2blog

Using the os.path.relpath() function along with the os.path.dirname() function to get parent directory two levels up in python.

The main utilization of the os.path.relpath() function comes when there is a need to find the parent directory multiple levels up which is not directly possible by the other methods.

The os.path.relpath() function along with the os.path.dirname() function can be utilized even in this case to implement the given task at hand.

The following code uses os.path.relpath() function along with the os.path.dirname() function to get parent directory two levels up in python.

The above code provides the following output:

Java2blog / Projects / aa.md

Here, we make a small tweak and specify the level as 2 in the user-defined function so that the loop runs twice and can get parent directory two levels up in python.

Conclusion.

This article was focused on and provided the different ways available to get parent directory in python. First, we look at the methods available to get parent directory in python, while the latter half of the article demonstrates how to get parent directory two levels up in python. The latter utilizes more or less the same functions as the former but with a different working and some significant tweaks to the code, both of which have been thoroughly explained in the article.

[ad_2]

Leave a Comment