Search code examples
pythonpython-3.xpython-os

Python - OS Module 'TypeError: 'bool' object is not iterable''


I Was trying to make a checker with the OS module for the directory lib, and it raised a error, the full error log is

for folder in path.exists('./lib'):

TypeError: 'bool' object is not iterable

the code is

for folder in path.exists('./lib'):
    if folder == False:
        print("lib folder not found, please re-install the repository.")
        if folder.isfile == True:
            print("Found a file called 'lib' and not the directory. Please re-install the repository")
            quit()
        else:
            pass
    else:
        print("Found.")

i don't know what's the problem, i tried changing it multiple times but i don't know any solutions. but the rest of the code that i have is working without any errors.


Solution

  • path.exists() just returns True or False depending on if that path exists. You should first check its existence via exists(), then use os.listdir() or glob.glob() to actually get a list of items in that path, but only if exists() returned True.