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

Question:

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.

Asked By: Michael Moreno

||

Answers:

Here it is in a single os method :

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

Works with or without the ./ in the beginning.

Answered By: ticster

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

import os

os.path.abspath(os.path.dirname(__file__))
Answered By: Karol Milewczyk
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.