1

How can I fix AttributeError: 'str' object has no attribute 'read' error in my python3 script.

user = input("Enter your word : ")
script = open('wordlist.txt', "r")

for s in script:
    content = s.read()
    if user in content:
        print("Match")
    else:
        print("Not match")
2
  • 2
    content = script.read() and lose the for loop Commented Nov 15, 2020 at 10:30
  • read() is a files' method but you call it on s which is a string representing each row of the file. Either change to if user in s in the loop or simply if user in script.read() Commented Nov 15, 2020 at 10:33

1 Answer 1

0

Try checking for the whole file at once

user = input("Enter your word : ")
script = open('wordlist.txt', "r")

if user in script.read():
    print("Match")
else:
    print("Not match")
Sign up to request clarification or add additional context in comments.

5 Comments

Try changing the 'if user in content:' to 'if s in content'. Otherwise it's quite unclear what you are trying to do here.
How can i iterate text file on loop? Please tell me @Tomerikoo
@Dave please try to do some basic research. This is such a common problem in Python with literally thousands of resources online. You have one link above that should help you
@Tomerikoo then he might as well do 'if user in script.readlines():' right?
@Dave I edited the solution please check if it works now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.