Created
September 25, 2012 06:56
-
-
Save lqik2004/3780365 to your computer and use it in GitHub Desktop.
getRelativePath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
import decimal | |
path1='/Users/liuchao/Desktop/hah.mp3' | |
paht2='/Users/liuchao/Desktop/EZ/EzRead.py' | |
def get_Relative_Path(path1,path2): | |
#split,拆分成list | |
p1Array=path1.split('/') | |
p2Array=paht2.split('/') | |
p1Length=len(p1Array) | |
p2Length=len(p2Array) | |
i=0 | |
#从头开始向后比较,找到第一个不同的位置,赋值给i | |
while (i<min(p1Length,p2Length)) & (p1Array[i]==p2Array[i]): | |
i=i+1 | |
#把p1Array从i开始除了最后一个的所有元素都修改成“..”,最后一个元素修改成“.” | |
for x in xrange(i,len(p1Array)): | |
p1Array[x]='..' | |
p1Array[-1]='.' | |
#合并地址 | |
reletivePath='/'.join(p1Array[i:]+p2Array[i:]) | |
return reletivePath | |
print get_Relative_Path(path1,paht2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment