Python 在for循环后无法打印结果
我有以下Python代码:Python 在for循环后无法打印结果,python,python-imaging-library,python-2.x,pillow,Python,Python Imaging Library,Python 2.x,Pillow,我有以下Python代码: for root, dirs, files in os.walk(path): for file in files: image = Image.open(root + '/' + file).convert('L') label = label_img(file) X.append(image) y.append(label) files.append(file) print
for root, dirs, files in os.walk(path):
for file in files:
image = Image.open(root + '/' + file).convert('L')
label = label_img(file)
X.append(image)
y.append(label)
files.append(file)
print X
print y
print files
但是,我没有返回任何结果,运行程序的控制台冻结。知道我做错了什么吗?我在10个小图像上运行这个程序,所以输出应该相对较快。例如,如果我在循环中运行一些print语句,结果很快就会得到
for root, dirs, files in os.walk(path): # files is a list
for file in files: # file is out of files
image = Image.open(root + '/' + file).convert('L')
label = label_img(file)
X.append(image)
y.append(label)
files.append(file) # here you append file to files
您的文件列表越来越长-难怪它会冻结…使用
os.path.join()
而不是字符串连接。进行一些调试如何<代码>打印文件?我在你的代码中既没有看到X
也没有看到Y
也没有label\u img(…)
,但我认为它们可能很重要。。。。也许你应该把你的代码刷得整整齐齐。为什么要分解文件
,然后对其进行迭代并将iter变量追加回文件?对于文件中的文件:。。。files.append(file)可能永远不会结束…不要使用相同的文件
变量进行迭代和追加。