#Error:TypeError:类型'bool'的参数不可迭代#

时间:2017-03-19 23:53:13

标签: python maya

第一篇文章!我对python很新,并试图改进,任何帮助将不胜感激!我看过其他有类似问题的帖子似乎仍无法解决这个问题。

这是我收到的错误,发生在第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)

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

期待听到任何人的回复,谢谢!

2 个答案:

答案 0 :(得分:0)

您目前获得的错误是由于

userInput in file.endswith('.ma')

这条线没有按照你的想象做。

file.endswith('.ma')返回bool。错误告诉您正在尝试迭代而不是bool。 in语句检查可迭代中的成员资格。有关in工作原理的详细信息,请查看this答案。

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

>>> '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

答案 1 :(得分:0)

假设您尝试仅获取以.ma结尾的文件和包含queryterm的文件名,请尝试下面的示例,看看是否有帮助

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)
相关问题