Get Absolute Path in Python

Python absolute path

We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative paths of files in our programs. In this article, we will discuss two ways to get the absolute path to a file in python from its relative pathname.

What is an absolute path to a file?

The absolute path to a file is the entire file path from the root file to the current file. For instance, Look at the following image. 

file-path image

You can observe that the sample.txt path is present in the java2blog folder. The java2blog folder is present in the codes folder.

Here, the absolute path of the file sample.txt will always be “/home/aditya1117/codes/java2blog/sample.txt”

On the other hand, its relative path will always change according to the folder from which we are viewing the file.  For instance, 

  • If we are currently in the java2blog folder, the relative path of the file will be /sample.txt.
  • However, If we move to the folder codes, the relative path to the file sample.txt will be /java2blog/sample.txt.

So, the relative path to a file changes according to the folder we are currently in. Whereas, the absolute path to a file never changes until the file is moved to another location. We can access the file using the absolute path from any directory. Whereas, we will have to update the relative path if we move to any other directory even if the file is at the same location.

How to get absolute path in Python

Let us now look at the ways to get the absolute path to a file in python.

Use abspath to get the absolute path to file

The os module in python provides us with the abspath() method under the os.path property. The abspath() method takes the relative path to a file and returns the absolute path in the string format as shown in the following example.

Output:

Here, the sample.txt file is present in the same folder as our source code file. So, the relative file has been given as only the file name.

use pathlib module to get the absolute path to file

Instead of using the os module, we can use the pathlib module to get the absolute path to a file in python. Here, we will use the Path() function and the absolute() method to get the absolute path to a file.

The Path() function takes the file name as input and creates a path object.  We can invoke the absolute() method on the path object to get the absolute path to a given file as follows.

Output:

Conclusion

In this article, we have discussed the relative path and the absolute path to a file. We have also discussed two approaches to get the absolute path in python. You can choose any of the approaches according to your preference as both the approaches have almost the same cost of execution.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related Posts

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

  • Python print type of variable
    16 November

    Print type of variable in Python

    Table of ContentsWhat is the type of variable in Python?Ways to print type of variable in PythonUsing the type() functionUsing the isinstance() functionUsing the __class__() functionConclusion What is the type of variable in Python? We use variables to store data in a reserved memory location. The type of a variable defines the memory allocated to […]

  • Python float array to int
    11 November

    Convert float array to int array in Python

    Table of ContentsWhat are the types of arrays?Ways to convert float arrays to int in PythonUsing the numpy.asarray() functionUsing the numpy.astype() functionUsing the numpy.int_() functionUsing the numpy.round_() functionUsing the numpy.rint() functionUsing the numpy.floor() functionUsing the numpy.ceil() functionUsing the numpy.trunc() functionConclusion What are the types of arrays? An array is a common structure that stores multiple […]

  • List intersection in Python
    11 November

    List intersection in python

    Table of ContentsWhat is the list intersection in Python?Ways to find the list intersection in PythonUsing the for loop to achieve list intersection in PythonUsing the set() function to find list intersection in PythonUsing the intersection() function to find list intersection in PythonUsing the filter() and lambda functions to find list intersection in PythonUsing the […]

  • Bilinear Interpolation in Python
    11 November

    Bilinear Interpolation in Python

    Table of ContentsWhat is Bilinear Interpolation?Creating a function to perform bilinear interpolation in PythonUsing the scipy.interpolate.interp2d() function to perform bilinear interpolation in Python What is Bilinear Interpolation? Linear Interpolation in mathematics helps curve fitting by using linear polynomials that make new data points between a specific range of a discrete set of definite data points. […]

  • Euclidean distance in Python
    10 November

    Calculate Eucledian distance in python

    Table of ContentsWhat is the Euclidean Distance?Ways to Calculate the Euclidean distance in PythonUsing the scipy.spatial.distance.euclidean() functionUsing the math.dist() functionUsing the numpy.sqrt(), numpy.square(), numpy.sum() functionsUsing the numpy.linalg.norm() functionUsing the numpy.dot() functionConclusion What is the Euclidean Distance? Euclidean Distance is the line segment between two points in any dimension. Mathematically, we can evaluate it as the […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.