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

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

解决方法

path.exists()仅返回TrueFalse,具体取决于该路径是否存在。您应该首先通过exists()检查其存在,然后使用os.listdir()glob.glob()实际获取该路径中的项目列表,但前提是exists()返回{{1} }。