os.walk()错误-无法解包的值太多

时间:2018-12-20 00:13:58

标签: python

嗨,我在文件夹a中有一个包含彩色图像的文件夹,我想将其更改为灰度图像并保存在文件夹b中

from PIL import Image 
import os
# Changing image to gray and scaling to 256x128
WORK_DIR = 'D:/folder/data/' #working folder 

source = WORK_DIR + 'a'
target = WORK_DIR +'b'

for dirpath, filenames in os.walk(source):
    for file in filenames:
        image_file = Image.open(os.path.join(dirpath, file))
        image_file.draft('L', (256, 128)) #convert to gray and 256x128
        image_file.save(os.path.join(target, file))

我收到以下错误消息,我不确定这意味着什么以及如何解决?

----> 7 for dirpath, filenames in os.walk(source):
      8         for file in filenames:
      9                 image_file = Image.open(os.path.join(dirpath, file))

ValueError: too many values to unpack (expected 2)

谢谢!

1 个答案:

答案 0 :(得分:1)

由于os.walk()返回一个元组中的3个项(根,目录,文件),因此出现了此错误。

只是改变

/welcome

for dirpath, filenames in os.walk(source):

如Dan d。指出,您应该更改

for root, dirpath, filenames in os.walk(source):

os.path.join(dirpath, file)