Skip to main content
 首页 » 编程设计

python之使用 Pycharm 在 Python 列表中处理 gif 文件时出现问题

2025年05月04日13JeffreyZhao

所以我一直在尝试在学校的计算机和家里的笔记本电脑上运行这段代码,但我不断收到错误消息: '列表索引超出范围'
但它在我导师的笔记本电脑上运行良好,它打印出第一张图片的代码。一切都一样,但我的不会运行,我们都使用相同的解释器。
...这是怎么回事?

使用 Python 3.5.0 运行 PyCharm 4.5.4:

   import os   
   import graphics   
   from glob import glob 
 
    def get_images(subFolderName): 
      pathName = os.path.join(os.path.dirname(__file__), subFolderName, '*.gif') 
      listOfFileNames = glob(pathName) 
      return listOfFileNames 
 
    list = get_images("SampleProjectGifs") 
    listOfImages = [] 
 
    for index in list: 
      listOfImages.append(graphics.Image(graphics.Point(50, 50), index)) 
 
    print(listOfImages[0]) 

请您参考如下方法:

您没有得到任何图像文件。所以你的列表是空的 - listOfImages。这就是您收到 OutOfIndex 错误的原因。

尝试检查这行代码 - listOfFileNames = glob(pathName)