首页 > 解决方案 > Python - 操作系统模块'TypeError:'bool'对象不可迭代''

问题描述

我试图用 OS 模块为目录 lib 做一个检查器,它引发了一个错误,完整的错误日志是

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

TypeError:'bool' 对象不可迭代

代码是

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.")

我不知道是什么问题,我尝试多次更改它,但我不知道任何解决方案。但我拥有的其余代码正在正常工作,没有任何错误。

标签: pythonpython-3.xpython-os

解决方案


path.exists()只是返回TrueFalse取决于该路径是否存在。您应该首先通过 来检查它的存在exists(),然后使用os.listdir()orglob.glob()来实际获取该路径中的项目列表,但前提是exists()返回True


推荐阅读