首页 > 解决方案 > 为什么我在 Python3 中收到此错误:IndexError: list index out of range

问题描述

标题有点解释自己。

这是我的代码:

import subprocess
import os
import sys

def log_face(name):
    #create a directory under the training folder with the new persons name
    path = ("./images/train/" + name)
    if not os.path.exists(path):
        mkdirexe = ("mkdir " + path)
        subprocess.call([mkdirexe], shell=True)
    
    #copy the most recent image into the new named folder
    copyexe = ("cp image_cam.jpg " + path)
    subprocess.call([copyexe], shell=True)

#if called direct then run the function
if __name__ == '__main__':
    name = sys.argv[1]
    print(log_face(name))

我不认为它在任何模块中......

谢谢!

标签: pythonsyntax-error

解决方案


可能name = sys.argv[1]是您遇到 IndexError 的地方。

您在没有传入任何内容的情况下调用第一个命令行参数。它不会提示您输入任何内容,并且会因 IndexError 而失败。

编辑:没有看到 Grismar 的评论!


推荐阅读