求相对路径

#!c:/python33/python
#求相对路径,来自php笔试题.b相对于a所在的路径.

def getpathinfo(a, b):
    a_list = a.split('/')
    b_list = b.split('/')
    #if len(b_list) < len(a_list):
    #    a_list, b_list = b_list, a_list
    result = ''
    try:
        for i in range(len(b_list)):
            if b_list[i] == a_list[i]:
                result += '../'
            else:#如果两个列表长度相等
                result += (b_list[i] + '/')
    except IndexError:
        result += '/'.join(b_list[i:])

    return result

b = '/a/b/c/d/e.php'
a = '/a/b/12/34/56/78/91/2/c.php'

print(getpathinfo(a, b))

 

posted on 2013-03-20 10:52  mtima  阅读(188)  评论(0编辑  收藏  举报

导航