Get Parent Directory in Python

Get Parent Directory in Python

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.


import_contacts

You may also like:

Related Posts

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • Return vs Print in Python
    16 April

    Return vs Print in Python

    Table of ContentsReturn vs Print in PythonConclusion Return vs Print in Python In Python, we work with a wide range of functions that make our code simple. We can also create functions using the def keyword. A function can be defined as per our requirement and can display something or return a value. This is […]

  • Floor division in Python
    10 April

    Floor division in Python

    Table of ContentsWhat is Floor Division in Python?How to implement floor division in Python?Using the // operator to implement floor division in Python.Using the // operator to implement floor division on negative integers in Python.Using the // operator to implement floor division on floating-point numbers in Python.Using the // operator to implement floor division with […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.