paganotti
paganotti

Reputation: 5661

Python function translate relative path ../../ into full path

is there a function that translate relative path ../test.txt into a full path? example:

the full path is:

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

the relative path is:

../test.txt

I want build this result:

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

As you can see translate ../ into "work" folder for build entire path.

Is there general function that acomplish this task?

Upvotes: 3

Views: 1612

Answers (2)

sth
sth

Reputation: 229754

You can use normpath() from the os.path module to get a normalize 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))

Upvotes: 1

Thanasis Petsas
Thanasis Petsas

Reputation: 4448

use this:

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

Documentation

Upvotes: 16

Related Questions