python - 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()
只是返回True
或False
取决于该路径是否存在。您应该首先通过 来检查它的存在exists()
,然后使用os.listdir()
orglob.glob()
来实际获取该路径中的项目列表,但前提是exists()
返回True
。
推荐阅读
- r - 当按其他数据框变量分组时,如何生成包含命名向量的列表列?
- python - 如何在python中考虑与一个单词内容相同的单词?
- flutter - 获取“TypeError:this.jsObject.database 不是函数”
- c# - 如何通过 Browserstack C# 在 Appium 中使用多个设备和软件操作系统进行并行测试
- java - 是否可以将自定义 Java 库导入 Gradle buildSrc Java 类?
- neo4j - 如何使用 neo4j 将 json 中的数组值存储到单个节点。那可能吗?
- snowflake-cloud-data-platform - 如何将存储过程作为雪花中的参数传递
- nextflow - Nextflow:向元组添加唯一 ID、哈希或行号
- javascript - 如何在 Karma 中模拟本地函数?
- javascript - 将 Chartjs 图像从 Angular 应用程序拖放到 powerpoint