How to get an absolute path in Python
Tags: example, python
Published: 2022-09-15 17:41:21

---
title: How to get an absolute path in Python
published: true
date: 2022-06-25 20:01:33 UTC
tags: example,python
canonical_url: https://towardsthecloud.com/get-absolute-path-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?](https://towardsthecloud.com/#What_is_an_absolute_path_in_Python)
*   [How to find the absolute path in Python](https://towardsthecloud.com/#How_to_find_the_absolute_path_in_Python)
*   [Conclusion](https://towardsthecloud.com/#Conclusion)

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](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:

    /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.

> [How to get a relative path in Python](https://towardsthecloud.com/get-relative-path-python)

<iframe class="wp-embedded-content" sandbox="allow-scripts" style="position:absolute;clip:rect(1px, 1px, 1px, 1px)" src="https://towardsthecloud.com/get-relative-path-python/embed#?secret=WhIUh3Enhs#?secret=SudPIZztmf" width="500" height="282"></iframe>

[Source](https://towardsthecloud.com/how-to-get-absolute-path-python)