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.

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")

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

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.


Want to join us? Join for tips, strategies, and resources that I use in my solo cloud agency to build well-architected, resilient, and cost-optimized AWS solutions on AWS.

Join 1k+ AWS Cloud enthusiasts
Loved by engineers worldwide


Danny Steenman

A Senior AWS Cloud Engineer with over 9 years of experience migrating workloads from on-premises to AWS Cloud.

I have helped companies of all sizes shape their cloud adoption strategies, optimizing operational efficiency, reducing costs, and improving organizational agility.

Connect with me today to discuss your cloud aspirations, and let’s work together to transform your business by leveraging the power of AWS Cloud.

I need help with..
stacked cubes
Improving or managing my CDK App.Maximize the potential of your AWS CDK app by leveraging the expertise of a seasoned CDK professional.
Reducing AWS Costs.We can start by doing a thorough assessment of your current AWS infrastructure, identifying areas with potential for cost reduction and efficiency improvement.
Verifying if my infrastructure is reliable and efficient.We’ve created a comprehensive AWS Operations Checklist that you can utilize to quickly verify if your AWS Resources are set up reliably and efficiently.