Python:TypeError:类型为';内置函数或方法';这是不可容忍的
我有以下代码:Python:TypeError:类型为';内置函数或方法';这是不可容忍的,python,python-3.x,Python,Python 3.x,我有以下代码: 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 fi
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
有人能解释一下原因吗
thans更改行
content = inputFile.read().lower
到
原始行将内置函数lower分配给变量内容,而不是调用函数str.lower
,并分配返回值,返回值绝对不可编辑 您正在使用
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
>
>>>对于“HELLO”中的x。降低:
... 打印x
...
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:“内置函数”或“方法”对象不可编辑
@LWH91:别忘了接受并投票表决有帮助的答案。特别是在这种情况下,pythons错误消息可能会稍微有用一些:-/
>>>
>>> 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