Why am I getting this error in Python3: IndexError: list index out of range

Why am I getting this error in 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))

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

谢谢!

可能 name = sys.argv[1] 是您 运行 进入 IndexError 的地方。

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

编辑: 没看到 Grismar 的评论!