convert a fully qualified path to relative path

convert a fully qualified path to relative path

Download torelativepath.zip

Synopsis:

torelativepath.py


torelativepath.py

Synopsis
def toRelativePath(path, prefix):
   from tounixpath import toUnixPath
   from todospath import toDosPath
   path = toUnixPath(path)
   prefix = toUnixPath(prefix)
   if not prefix.endswith('/'): prefix = prefix + '/'
   path = path.replace(prefix, '')
   return toDosPath(path)

#-- unit tests
def test_torelativepath():
   ut.utassert(toRelativePath("c:/bin/bob", "c:/bin"),     'bob');
   ut.utassert(toRelativePath("c:/bin/bob", "c:/bin/"),    'bob');
   ut.utassert(toRelativePath("c:/bin/bob", "c:\\bin\\"),  'bob');

   ut.utassert(toRelativePath("c:/bin/bob/", "c:/bin"),    'bob\\');
   ut.utassert(toRelativePath("c:/bin/bob/", "c:/bin/"),   'bob\\');
   ut.utassert(toRelativePath("c:/bin/bob/", "c:\\bin\\"), 'bob\\');

   ut.utassert(toRelativePath("c:/bin/billy/bob", "c:\\bin\\"), 'billy\\bob');
   ut.utassert(toRelativePath("c:/bin/billy/bob/", "c:\\bin\\"), 'billy\\bob\\');

   ut.utassert(toRelativePath("c:/bin/billy/bob", "c:\\"), 'bin\\billy\\bob');
   ut.utassert(toRelativePath("c:/bin/billy/bob/", "c:/"), 'bin\\billy\\bob\\');

   ut.utassert(toRelativePath("c:\\bin/billy/bob", "c:\\"), 'bin\\billy\\bob');
   ut.utassert(toRelativePath("c:\\bin/billy/bob/", "c:/"), 'bin\\billy\\bob\\');
   






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010