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:

/Users/paganotti/Documents/Project/simple/work/paga/

      

relative path:

../test.txt

      

I want to plot this result:

/Users/paganotti/Documents/Project/simple/work/test.txt

      

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

Is there a general function that accomplishes this task?

+3


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))

      

+1


source


use this:

import os
os.path.abspath('../test.txt')

      



Documentation

+15


source







All Articles