# Error: TypeError: argument of type 'bool' is not iterable #

# Error: TypeError: argument of type 'bool' is not iterable #

第一个post!我是 python 的新手,正在努力改进,如有任何帮助,我们将不胜感激!我浏览了其他 post 有类似问题的人似乎仍然无法解决这个问题。

这是我收到的错误,发生在第 5 行:

    # Error: TypeError: argument of type 'bool' is not iterable # 

这是我的代码:

userInput = cmds.textFieldGrp(searchText, query = True, text=True)
path = "D:\somefolder"
for root, dirs, files in os.walk(path):
    for file in files:
        if (userInput in file.endswith('.ma')):
            print file
        else:
            break
            print "No files containing %s" (userInput)

基本上,我试图根据用户输入的关键字在目录中搜索文件。

期待任何人的回音,谢谢!

您当前遇到的错误是由于

userInput in file.endswith('.ma')

那条线并没有按照你认为的那样去做。

file.endswith('.ma')returns一个bool。该错误告诉您您正在尝试 iterate over bool。 in 语句检查可迭代对象中的 membership。查看 this 答案以获取有关 in 工作原理的更多信息。

这是一个单独的演示,向您展示错误是如何重现的:

>>> 's' in False:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable

这是一个工作示例:

>>> 's' in 'this will return true'
True

假设您只尝试获取以 .ma 结尾且文件名包含查询项的文件,请尝试下面的示例,看看是否有帮助

userInput = cmds.textFieldGrp(searchText, query = True, text=True)
path = "D:\somefolder"
for root, dirs, files in os.walk(path):
    for file in files:
        if ((userInput in file) and file.endswith('.ma')):
            print file
        else:
            break
            print "No files containing %s" (userInput)