Ignore:
Timestamp:
Mar 13, 2014, 4:58:53 PM (11 years ago)
Author:
dmik
Message:

Add OS/2 version of os.path.relpath.

Based on the NT version (with a bug fix related to
trailing slashes).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/trunk/Lib/os2knixpath.py

    r348 r381  
    246246
    247247supports_unicode_filenames = False
     248
     249
     250def relpath(path, start=curdir):
     251    """Return a relative version of a path"""
     252
     253    if not path:
     254        raise ValueError("no path specified")
     255    start_list = abspath(start).split(sep)
     256    path_list = abspath(path).split(sep)
     257    # Remove empty components after trailing slashes
     258    if (start_list[-1] == ''):
     259        start_list.pop()
     260    if (path_list[-1] == ''):
     261        path_list.pop()
     262    if start_list[0].lower() != path_list[0].lower():
     263        unc_path, rest = splitunc(path)
     264        unc_start, rest = splitunc(start)
     265        if bool(unc_path) ^ bool(unc_start):
     266            raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
     267                                                                % (path, start))
     268        else:
     269            raise ValueError("path is on drive %s, start on drive %s"
     270                                                % (path_list[0], start_list[0]))
     271    # Work out how much of the filepath is shared by start and path.
     272    for i in range(min(len(start_list), len(path_list))):
     273        if start_list[i].lower() != path_list[i].lower():
     274            break
     275    else:
     276        i += 1
     277
     278    rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
     279    if not rel_list:
     280        return curdir
     281    return join(*rel_list)
Note: See TracChangeset for help on using the changeset viewer.