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/os2emxpath.py

    r2 r381  
    158158
    159159supports_unicode_filenames = False
     160
     161
     162def relpath(path, start=curdir):
     163    """Return a relative version of a path"""
     164
     165    if not path:
     166        raise ValueError("no path specified")
     167    start_list = abspath(start).split(sep)
     168    path_list = abspath(path).split(sep)
     169    # Remove empty components after trailing slashes
     170    if (start_list[-1] == ''):
     171        start_list.pop()
     172    if (path_list[-1] == ''):
     173        path_list.pop()
     174    if start_list[0].lower() != path_list[0].lower():
     175        unc_path, rest = splitunc(path)
     176        unc_start, rest = splitunc(start)
     177        if bool(unc_path) ^ bool(unc_start):
     178            raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
     179                                                                % (path, start))
     180        else:
     181            raise ValueError("path is on drive %s, start on drive %s"
     182                                                % (path_list[0], start_list[0]))
     183    # Work out how much of the filepath is shared by start and path.
     184    for i in range(min(len(start_list), len(path_list))):
     185        if start_list[i].lower() != path_list[i].lower():
     186            break
     187    else:
     188        i += 1
     189
     190    rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
     191    if not rel_list:
     192        return curdir
     193    return join(*rel_list)
Note: See TracChangeset for help on using the changeset viewer.