Cubestormer Iv
Cubestormer Iv

Reputation: 21

Password program not working in Python

I have made a password prog to input the first line of the file as the password and then upon accessing the file the prog should ask the password and only if the password is right should the contents of the file be printed. But I just can’t get it to work. Pls help.

p=open("abc.txt", 'w+')
p.write("T") #< password
p.write("\n")
p.write("work pls")
p.close()
p=open("abc.txt", 'r+')
y=raw_input('pass')
if str(p.readline())==str(y):
    print p.read()
else:
    print "Password incorrect"
p.close()

Upvotes: 1

Views: 55

Answers (1)

Hai Vu
Hai Vu

Reputation: 40773

readline() returns the line, including the trailing new line. You have to strip it off:

if p.readline().rstrip() == y:

Upvotes: 2

Related Questions