NoneType 对象不可调用 python

NoneType object is not callable python

我遇到了一条错误消息,内容为 'TypeError: NoneType object is not callable'。这与我之前的一个问题类似,但是我的问题的答案给了我一个错误,或者我没有正确阅读答案。

图像文件(导致错误消息的代码是 for _, __, img_files in os.chdir(path):

def import_folder(path):
    surface_list = []

    for _, __,img_files in os.chdir(path):
        for image in img_files:
            full_path = path + '/' + image
            image_surf = pygame.image.load(full_path).convert_alpha()
            surface_list.append(image_surf)

错误信息:

Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\Thing.py", line 13, in <module>
level = Level(level_0, window)
File "C:\Users\Daniel\Desktop\level2.py", line 32, in __init__
self.coin_sprites = self.create_tile_group(coins_layout, 'coins')
File "C:\Users\Daniel\Desktop\level2.py", line 61, in create_tile_group
sprite = Coin(tile_size, x, y, (x, y), 
'C:\Users\daniel\Desktop\game\coins\gold')
File "C:\Users\Daniel\Desktop\tiles2.py", line 46, in __init__
super().__init__(size, x, y, pos, path)
File "C:\Users\Daniel\Desktop\tiles2.py", line 28, in __init__
self.frames = import_folder(path)
File "C:\Users\Daniel\Desktop\support2.py", line 10, in import_folder
for _, __,img_files in os.chdir(path):
TypeError: 'NoneType' object is not iterable

os 我正在导入:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

os.chdir 将工作目录更改为 returns None 路径。你收到错误是因为你试图迭代这个 None.

您可能打算使用 os.listdir

编辑:os.walk 更适合您的 if 条件,因此可能更可取。