Python function translates relative path ../../ to full path

is there a function that translates the relative path .. /test.txt into a full path? Example:

full path:



relative path:



I want to plot this result:



As you can see translate ../ into the "work" folder to create the whole path.

Is there a general function that accomplishes this task?


source to share

2 answers

You can use normpath()

from module os.path

to get the normalized path from one with ".."

and similar constructs:

base = '/Users/paganotti/Documents/Project/simple/work/paga/'
rel = '../test.txt'

print os.path.normpath(os.path.join(base, rel))




use this:

import os





All Articles