Python:TypeError:“builtin_function_or_method”类型的参数不是iterab

2024-04-16 04:40:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

def search():
    os.chdir("C:/Users/Luke/Desktop/MyFiles")
    files = os.listdir(".")
    os.mkdir("C:/Users/Luke/Desktop/FilesWithString")
    string = input("Please enter the website your are looking for (in lower case):")
    for x in files:
        inputFile = open(x, "r")
        try:
            content = inputFile.read().lower
        except UnicodeDecodeError:
            continue
        inputFile.close()
        if string in content:
            shutil.copy(x, "C:/Users/Luke/Desktop/FilesWithString")

总是会出现这个错误:

line 80, in search
    if string in content:
TypeError: argument of type 'builtin_function_or_method' is not iterable

有人能解释一下原因吗。

坦斯


Tags: 代码inforsearchstringifosfiles
2条回答

换行

content = inputFile.read().lower

content = inputFile.read().lower()

原始行将内置函数分配给变量内容,而不是调用函数str.lower,并分配绝对不可iterable的返回值。

你在用

content = inputFile.read().lower

而不是

content = inputFile.read().lower()

也就是说你得到的是更低的函数,而不是更低的返回值。

实际上你得到的是:

>>> 
>>> for x in "HELLO".lower:
...     print x
... 
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object is not iterable

相关问题 更多 >