在Python中保存列表

时间:2015-03-17 04:33:26

标签: image list python-2.7 arraylist

我想在python数组中保存字符串列表。 我的代码如下:

import os
Images_list=[os.listdir("/home/metal-machine/Pictures/new")]
print Images_list[0]

它仅在[0]和我使用

时打印列表
print Images_list[1]

它返回错误:     IndexError:列表索引超出范围

我确信这个“索引超出范围”是因为列表的所有元素都没有用逗号分隔。

为此,我也可以在Python中使用split()方法。

images_list.split()

上面的命令拆分文件但是在所有文件之间加入逗号,但是为什么Python默认情况下不在所有元素之间插入逗号? sunch类的列表在Python中是否有效?

1 个答案:

答案 0 :(得分:1)

为什么不编写更加无错误的代码

for images in Images_list:
    print images