【发布时间】:2019-11-18 18:54:08
【问题描述】:
我正在尝试通过提供 with 语句和列出我要处理的文件的文件路径的文本文件来处理目录中的多个图像(处理包括显示的灰度以及一些去噪和像素强度测量)。 使用下面显示的代码,程序正确处理列表中的第一个文件,然后在处理其他文件之前结束。有谁知道为什么以及如何让它遍历列出的所有文件?
#establish loop
with open('file_list.txt') as inf:
for line in inf:
path = line
# grayscale and plot
original = io.imread(path, plugin = 'pil')
grayscale = rgb2gray(original)
【问题讨论】:
-
您的代码可能处理列表中的最后一个文件,而不是第一个。最后两行应缩进成为循环的一部分。
标签: python for-loop directory iterator with-statement