2

i have a text file with following data: deric, robert, mathew,

Now, i want the user to input a string and then compare that string with the text file..

 fo = open("d:\\ar.txt", "r")
 file_contents = fo.read()
 print('Enter your password.')
 typedPassword = input()
 for i in file_contents:
     if typedPassword == file_contents:
         print('Access granted')
     else:
         print('Access denied')

However, this doesn't seem to work. any thoughts?

Abhijeetk431
  • 847
  • 1
  • 8
  • 18
ajay m
  • 21
  • 1
  • 3

2 Answers2

3

Try this:-

fo = open("poopy.txt", "r")
file_contents = fo.read()
print('Enter your password.')
typedPassword = input()
Flag = 0
for i in file_contents.split('\n'):
    if typedPassword == i:
        Flag = 1
if Flag == 1:
    print('Access Granted')
else:
    print('Access Denied')
Abhijeetk431
  • 847
  • 1
  • 8
  • 18
0

You were checking if the inputted password maches the whole file... This checks each line

username = input("Input your username")
password = input("Input your password")
correct = None
with open("d:\\ar.txt", "r") as file:
    for line in file:
        line = line.split(",")
        if username == line[0] and password == line[1]:
            correct = True
if correct:
    print("") # placeholder
    #(logged in code)

for this to work your file should be of the fornat

username,password,

username,password,

username,password,