繁体   English   中英

NoneType object 不可调用 python

[英]NoneType object is not callable python

我一直遇到一条错误消息,内容为“TypeError:NoneType object 不可调用”。 这类似于我之前的一个问题,但是我的问题的答案给了我一个错误,或者我可能只是没有正确阅读答案。

图像文件(导致错误消息的代码是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

操作系统我正在导入:

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

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

您可能打算使用os.listdir

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM