赋值会引发list.index的异常

时间:2010-09-29 05:21:50

标签: python

这段代码如何碎片......

def subInPath(origPath, subPath):
    origSplit = split(origPath, '/')
    subSplit = split(subPath, '/')

    subRoot = subSplit[0]
    origSplit.reverse()
    print origSplit.index(subRoot)
    rootIndex = origSplit.index(subRoot)

    origSplit[:rootIndex+1] = []
    origSplit.reverse()

    newPath = join(origSplit, sep)
    newPath += (sep + subPath)

    if not exists(newPath):
        raise Exception, "Path subbed in not found."
    return newPath

使用参数("C:/Users/MyName/Desktop/second_stage/Kickle_Pack/GardenLand_D.xml", "Kickle_Pack/Animations/TileAnims_48x48.xml") ...

在print语句中输出2,但在其下面的语句处抛出ValueError。我很困惑。

1 个答案:

答案 0 :(得分:1)

使用目录或路径时,始终使用os.path模块。它具有使用目录所需的所有方法,而且它具有在多个操作系统中兼容的优点。

这只是更好的软件工程。