george
george

Reputation: 188

Checking data from a text file

I've been trying to create a program that takes an input and checks to see whether that input is in some text from a file. My end goal is to create a user login GUI, but I will not show the code for the GUI here as there is quite a lot. I need to work out how to compare text from an input and from the file. This is my code so far

def check(entry):
    search = open('password.txt', 'r')
    if str(entry) in str(search): 
        return (entry, "Word found")
    else:
        return entry, ("Word not found")

with open('password.txt', 'r') as text:
    print (text.read())

while True:
    entry=input("\nSearch for word: ")
    print(check(entry)) 

When I run the code it will say that 1, 2, 5 and 12 are all in the text but none of the words that are in text are confirmed. If anyone could help it would be appreciated, thanks.

Upvotes: 1

Views: 2714

Answers (1)

Bill Letson
Bill Letson

Reputation: 571

Ignoring the security bad ideas covered in another comment, str(search) doesn't work like you think it does. If you print it, you should see something like:

<open file 'password.txt', mode 'r' at 0x0000000001EB2810>

which is a description of the object you created with the open function. You need to read the file into a string first with the .read() method.

Upvotes: 1

Related Questions