close

How to get absolute path of a pathlib.Path object?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to get absolute path of a pathlib.Path object in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to get absolute path of a pathlib.Path object?

  1. How to get absolute path of a pathlib.Path object?

    You're looking for the method .absolute, if my understanding is correct, whose documentation states:

  2. get absolute path of a pathlib.Path object

    You're looking for the method .absolute, if my understanding is correct, whose documentation states:

Method 1

You’re looking for the method .absolute, if my understanding is correct, whose documentation states:

>>> print(p.absolute.__doc__)
Return an absolute version of this path.  This function works
        even if the path doesn't point to anything.

        No normalization is done, i.e. all '.' and '..' will be kept along.
        Use resolve() to get the canonical path to a file.

With a test file on my system this returns:

>>> p = pathlib.Path('testfile')
>>> p.absolute()
PosixPath('/home/jim/testfile')

Method 2

If you simply want the path and do not want to check if the file exists, you can do

str(p)

as document in the Operations section.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read