Python:TypeError:“builtin_function_or_method”类型的参数不可迭代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14414720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:23:04  来源:igfitidea点击:

Python: TypeError: argument of type 'builtin_function_or_method' is not iterable

pythonpython-3.x

提问by

I have the following code:

我有以下代码:

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

which always gives this error:

这总是给出这个错误:

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

can someone shed some light on why.

有人可以解释一下原因吗?

thans

采纳答案by Abhijit

Change the line

换线

content = inputFile.read().lower

to

content = inputFile.read().lower()

Your original line assigns the built-in function lower to your variable content instead of calling the function str.lowerand assigning the return value which is definitely not iterable.

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

回答by sotapme

You're using

你正在使用

content = inputFile.read().lower

instead of

代替

content = inputFile.read().lower()

ie you're getting the function lower and not the return value from lower.

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

In effect what you're getting is:

实际上你得到的是:

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