How to get a relative path in Python

When you’re working on a project with multiple people, it could be possible that you’re needing to import files or data from a specific location within the project without writing down the full path.

Therefore you want to find out the path relative to the current working directory where you run your Python code.

A relative path starts with /./ or ../.

To get a relative path in Python you first have to find the location of the working directory where the script or module is stored. Then from that location, you get the relative path to the file want.

What is a relative path in Python?

A relative path in Python is a path that describes the location of a directory relative to the entry point where you run the Python script.

So let’s say you run your Python code in ~/home/projects/example-project/

This is the entry point where you run the top-level code of your python module and acts as the starting point for the relative path. A relative path usually starts with /./ or ../.

To give a further explanation, let’s look at the files and directories that are in this example-project.

├── cdk.context.json
├── cdk.json
├── mypy.ini
├── requirements.txt
├── src
│  ├──
│  ├── bin
│  │  ├──
│  │  ├── environments
│  │  └──
│  ├── lib
│  │  ├──
│  │  ├── api
│  │  ├── compute
│  │  ├── constructs
│  │  ├── database
│  │  ├── skeleton
│  │  └── storage
│  └──
├── tests
│  ├──
│  └──
└── tox.ini

So for example, if you need to access ~/home/projects/example-project/src/lib then the relative path is ./src/lib/ in this Python project.

Using relative paths simplifies the code since you don’t need to write the full absolute path in order to find a file or directory in your Python project.

How to find the relative path in Python

You can run your Python script on different operating systems, therefore you want to automatically find the full path of the file you wish to import into your code instead of hardcoding it. This can be accomplished by combining the absolute path with the relative path in order to get the file or folder in your project.

We’ll use the from the previous example and from this working directory we want to get the ./src/lib relative path.

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

import os

absolute_path = os.path.dirname(__file__)
relative_path = "src/lib"
full_path = os.path.join(absolute_path, relative_path)

First, you have to import the os module in Python so you can run operating system functionalities in your code.

Then you create the variable absolute_path which fetches the current directory relative to the root folder. This is the full path to your working directory, in this case, ~/home/projects/example-project/.

The advantage to getting the absolute path on your operating system is that it makes it possible to run the script on different systems on different working directories.

The relative_path variable is a string in which you define the location of the folder that you want to fetch relative to the working directory. In this case, "src/lib".

Then we use the absolute_path and combine it via join with the relative_path to get the full path to the lib folder which results in:



As you can see, a relative path can be useful to simplify your code without having to write down the full path to a folder or file in your Python code.

Instead, you only point to the location of a directory relative to your working directory or the entry point of your Python script.

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

Danny Steenman

Is a Principal Cloud Consultant with a background in DevOps Engineering and thorough hands-on experience in architecting and building highly scalable distributed systems on AWS Cloud using Infrastructure as Code.

A prominent leader who is passionate about sharing AWS technical expertise by writing technical articles.