'NoneType object is not callable python

I have been encountering an error message which reads, 'TypeError: NoneType object is not callable'. This is similar to a previous question I had, but the answer to my question gave me an error, or maybe I just didn't read the answer properly.

Image files (code causing the error message is 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)

Error Message:

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 I'm importing:

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


Solution 1:[1]

os.chdir changes the working directory to the path and returns None. You're getting the error because you're trying to iterate on this None.

You probably meant to use os.listdir

Edit: os.walk fits your if condition better so that may be preferable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1