Python expand './subfolder' into 'home/username/project/subfolder'

Viewed 18

I have a function that takes in a path relative to the current working directory:

get_full_path('./subfolder'):

It should expand the ./ notation into the full root path, returning:

home/username/project/subfolder

Ideally I'd like a single os method that can do this.

2 Answers

Here it is in a single os method :

os.path.abspath("./some/relative/path")

Works with or without the ./ in the beginning.

This will return the path to the directory where the script is located.

import os

os.path.abspath(os.path.dirname(__file__))
Related