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:
```python showLineNumbers import os
os.path.abspath("src/examplefile.txt")
To explain the Python code: first, you have to import the [`os` module](https://docs.python.org/3/library/os.html) in Python so you can run operating system functionalities in your code.
Then you use the [`os.path`](https://docs.python.org/3/library/os.path.html) library to return a normalized absolutized version of the pathname *path*.
This will result in the following output:
```bash
/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.
Join my newsletter for real-world insights, actionable strategies, and lessons learned the hard way on my journey building a successful AWS Cloud Consulting business, delivered to your inbox.
Subscribe to my newsletter
Read articles from Danny Steenman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Danny Steenman
Danny Steenman
Hi, ๐ my name is Danny. Iโm an AWS Cloud Consultant who likes to build things in the Cloud. I write blog posts about a variety of topics like AWS, DevOps, and Certifications. The goal is to keep things practical and easy to understand.