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

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.