Finding the relative path of a file from a dir

Peter Hansen peter at engcorp.com
Fri Jan 20 08:16:48 EST 2006


Suresh Jeevanandam wrote:
> 	a = '/home/suresh/doc/html/a1/'
> 	b = '/home/suresh/doc/'
> 
> I am looking for a standard function which will return the location of b 
> relative to a i.e. '../..'
> 
> 	I have gone through the os and os.path modules, but could not find any 
> function of use. Should I write my own?

Don't write your own.  This is yet another thing that Jason Orendorff's 
path.py module can do easily:

 >>> from path import path
 >>> b = path('/users/peter/')
 >>> a = path('/users/peter/synergy/tiki.txt')
 >>> a.relpathto(b)
path(u'..\\..')

-Peter




More information about the Python-list mailing list