python判断是否是正确路径_求一个python的路径对比方法,判断路径是否相等,是否包含...

展开全部

#coding=utf-8

'''

Created on 2014-11-17

@62616964757a686964616fe78988e69d8331333335343364author: Neo

'''

#0:equal

#1:path1 is sub folder of path2

#-1:path2 is sub folder of path1

#2: unrelated

def comparePath(path1, path2):

if not path1 or not path1:

return 2

path1Len =  len(path1)

path2Len =  len(path2)

if path1Len > path2Len:

longPath = path1

shortPath = path2

cmpFator  = 1

else:

longPath = path2

shortPath = path1

cmpFator  = -1

shortPathLen = len(shortPath)

longPathLen = len(longPath)

i = 0

j = 0

while i 

c1 = shortPath[i]

c2 = longPath[j]

if isSlash(c1):

if not isSlash(c2):

return 2

while i 

i += 1

while j 

j += 1

else:

if c1 != c2:

if i == shortPathLen:

return cmpFator

else:

return 2

i += 1

j += 1

if i == shortPathLen:

if j == longPathLen:

return 0

while j 

if not isSlash(longPath[j]):

return cmpFator

j += 1

return 0

else:

return 2

def isSlash(c):

return c == '/' or c == '\\'

print comparePath('C:/', 'C:/')

print comparePath('C:/', 'C:/Python27')

print comparePath('C:/Python27', 'C:')

print comparePath('C:/Python27', 'D:')

result:0

-1

1

2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值