How to get an absolute path in Python

dannysteenman

Danny Steenman

Posted on September 15, 2022

How to get an absolute path in Python

Operating systems such as Windows, Linux, or macOS have different path structures in which operating system files are stored.

Therefore when you run Python scripts on these machines and want to fetch files or directories, you want to find the file’s absolute path relative to the current directory automatically instead of hardcoding it for every system.

An absolute path is also known as the full path and starts with / in Linux and macOS and C:/ on Windows.

To find an absolute path in Python you import the os module then you can find the current working directory using os.path.abspath("insert-file-name-here") in your Python script.

Table of Contents

What is an absolute path in Python?

An absolute path in Python is the full path starting from the root of the operating file system up until the working directory.

So let’s say you run your Python code in /Users/dannysteenman/home/projects/example-project/app.py. This is the entry point where you run the top-level code of your python module.

Then this is the absolute path of your working directory /Users/dannysteenman/home/projects/example-project/.

How to find the absolute path in Python

As mentioned at the beginning of this article you can run your Python app on different operating systems, therefore you want to automatically find the full path of the file you wish to import in your code instead of hardcoding it.

So given a path such as "src/examplefile.txt", how do you find the file’s absolute path relative to the current working directory (/Users/dannysteenman/home/projects/example-project/) in Python?

To get the absolute path in Python you write the following code:

import os

os.path.abspath("src/examplefile.txt")
Enter fullscreen mode Exit fullscreen mode

To explain the Python code: first, you have to import the os module in Python so you can run operating system functionalities in your code.

Then you use the os.path library to return a normalized absolutized version of the pathname path.

This will result in the following output:

/Users/dannysteenman/home/projects/example-project/src/examplefile.txt
Enter fullscreen mode Exit fullscreen mode




Conclusion

As you can see an absolute path helps you find the full path of a file or directory relative to the current working directory. This is useful since you get the flexibility to find files or folders and return the correct path on different operating systems.

To get an absolute path in Python you use the os.path.abspath library. Insert your file name and it will return the full path relative from the working directory including the file.

If you need guidance on finding a relative path in Python, read the following article below.

How to get a relative path in Python

Source

💖 💪 🙅 🚩
dannysteenman
Danny Steenman

Posted on September 15, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related