# Feel free to do anything with this code. # Nicolas Fleury (nid_oizo@yahoo.com_remove_the_) import sys def splitall(path): if sys.platform == 'win32': all = path.replace('/', '\\').split('\\') if len(all[0]) == 2 and all[0][1] == ':': all[0] = all[0] + '\\' elif all[0] == '': # path was beginning with \ or / all[0] = '/' if all[-1] == '': all = all[:-1] return all else: all = path.split('/') if all[0] == '': # path was beginning with \ or / all[0] = '/' if all[-1] == '': all = all[:-1] return all def abstorel(path, relToPath): "Make an absolute path relative to another path." if not os.path.isabs(path): raise Exception('Path "' + path + '" should be absolute') if not os.path.isabs(relToPath): raise Exception('Path "' + relToPath + '" should be absolute') all = splitall(path) if sys.platform == 'win32': if (os.path.splitdrive(path)[0] and os.path.splitdrive(relToPath)[0] and os.path.splitdrive(path)[0] != os.path.splitdrive(relToPath)[0]): return path # different drives; keep absolute path = splitall(path)[1:] relToPath = splitall(relToPath)[1:] samePathCount = 0 for i in range(max(len(path), len(relToPath))): if path[i] != relToPath[i]: break samePathCount += 1 newPath = ['..'] * (len(relToPath) - samePathCount) newPath += path[samePathCount:] return "\\".join(newPath)