我如何解决IndexError:列表索引超出范围?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何解决IndexError:列表索引超出范围?相关的知识,希望对你有一定的参考价值。

我正在尝试复制此存储库:https://github.com/sujiongming/UCF-101_video_classification。运行2_extract_files.py文件时出现以下错误。

Traceback (most recent call last):
  File "2_extract_files.py", line 99, in <module>
    main()
  File "2_extract_files.py", line 96, in main
    extract_files()
  File "2_extract_files.py", line 38, in extract_files
    video_parts = get_video_parts(video_path)
  File "2_extract_files.py", line 76, in get_video_parts
    filename = parts[3]
IndexError: list index out of range

代码如下:

def extract_files():
    data_file = []
    folders = ['./train/', './test/']

    for folder in folders:
        class_folders = glob.glob(folder + '*')

        for vid_class in class_folders:
            class_files = glob.glob(vid_class + '/*.avi')

            for video_path in class_files:
                video_parts = get_video_parts(video_path)

                train_or_test, classname, filename_no_ext, filename = video_parts
                if not check_already_extracted(video_parts):

                    src = train_or_test + '/' + classname + '/' + 
                        filename
                    dest = train_or_test + '/' + classname + '/' + 
                        filename_no_ext + '-%04d.jpg'
                    call(["ffmpeg", "-i", src, dest])

                nb_frames = get_nb_frames_for_video(video_parts)

                data_file.append([train_or_test, classname, filename_no_ext, nb_frames])

                print("Generated %d frames for %s" % (nb_frames, filename_no_ext))

    with open('data_file.csv', 'w') as fout:
        writer = csv.writer(fout)
        writer.writerows(data_file)

    print("Extracted and wrote %d video files." % (len(data_file)))

def get_nb_frames_for_video(video_parts):
    train_or_test, classname, filename_no_ext, _ = video_parts
    generated_files = glob.glob(train_or_test + '/' + classname + '/' +
                                filename_no_ext + '*.jpg')
    return len(generated_files)

def get_video_parts(video_path):
    parts = video_path.split('/')
    filename = parts[3]
    filename_no_ext = filename.split('.')[0]
    classname = parts[2]
    train_or_test = parts[1]

    return train_or_test, classname, filename_no_ext, filename

谁能告诉我我在做什么错,并指导我如何正确设置列表索引。预先感谢。

Window 10
Python 3.7.6
答案

[建议仅使用os.path.split(video_path)os.path.splitext()并逐步解决,它更安全,也更便于携带:

def get_video_parts(video_path):
    head, filename = os.path.split(video_path)
    filename_no_ext, ext = os.path.splitext(filename)
    head, classname = os.path.split(head)
    head, train_or_test = os.path.split(head)

    return train_or_test, classname, filename_no_ext, filename

https://docs.python.org/2/library/os.path.html#os.path.split

以上是关于我如何解决IndexError:列表索引超出范围?的主要内容,如果未能解决你的问题,请参考以下文章

IndexError:列表索引超出范围(打印整数)

Python:IndexError:列表索引超出范围

IndexError:列表索引超出范围 - python 错误

csv,IndexError:列表索引超出范围

使用 TensorFlow 的“IndexError:列表索引超出范围”错误

Keras model.fit() IndexError:列表索引超出范围