python - 赋值引发 list.index 异常

标签 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")...

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

最佳答案

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

这只是更好的软件工程。

关于python - 赋值引发 list.index 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3818923/

相关文章:

python - 使用 Python 子进程运行 SLURM 脚本将多个长作业提交到队列并等待作业完成,然后再继续 python 脚本

python - Tensorflow 将训练数据分成批处理

python - Django Rest 创建嵌套对象(ManyToMany)

python - 使用数据库配置或模型在 Flask 中运行 SQLAlchemy 查询的区别?

python - 在 Yolov5 中计算边界框的高度和宽度

python - SQLAlchemy - 映射器配置和声明性基础

python - 获取触发事件的小部件?

python - 你如何用cython编译一个 flask 应用程序?

python - 获取 TruncatedSVD.transform() 返回 float16 而不是 float64

python - Django:扩展用户模型 - UserProfile 中的内联用户字段