12

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

2 Answers 2

50

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.lower and assigning the return value which is definitely not iterable.

Sign up to request clarification or add additional context in comments.

1 Comment

especially in this case pythons error messages could be slightly more helpful :-/
5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.