Password verification in Python programming language | applied electronics engineering

Latest

Password verification in Python programming language

By Applied Electronics - Sunday, April 9, 2017 No Comments
Python programming language can be used to do all sorts of things. Here we show how to read a file and check whether the text matches some user input. So suppose you have a file called pw.txt. This pw.txt file contains some password. You had saved it in your computer. Now with python you can open it and read it into a variable. Then you let the user enter some password and check whether it matches the password in the pw.txt file.

Here is how you would do this.

First open the file and create a file object.

f = open("pw.txt")

f is the file object.

Now read the content of the file and save it in a variable say x.

x = f.read()

Now read the user input,

y = raw_input("Enter your password: ")

Now comes making comparison and decision for checking the password match. Best way is to use the if statement in python.

if y == x:
    print('The password is correct')
else:
    print("The password is incorrect")

 So the whole python code for this would be as follows:


f = open('pw.txt')
x = f.read()

y = input('Enter your password: ')

if y == x:
    print('The password is correct')
else:
    print('The password is incorrect')

So when you run this code you would see,

Enter your password:

So when you enter some text which matches the password in the file, you will get The password is correct:

Enter your password: mypassword
The password is correct

Or if you did not enter the correct password you will get The password is incorrect.

Enter your password: nopass
The password is incorrect


Optional you can print back the password the user entered if there is no match.

f = open('pw.txt')
x = f.read()

y = input('Enter your password: ')

if y == x:
    print('The password is correct')
else:
    print("The password you entered %s is incorrect" %y)

See Python Print Statement Examples how the print statement works.

Thus using file operation statements and if else statement we can create simple password verification program in python. The if statement in python is similar to other programming language. See comparison if else statement in C, Matlab and Python programming languages.

So here we showed you a simple python program to verify password entered by user using a file. This is a useful concept. It is the same concept used for checking database stored password or username.

No Comment to " Password verification in Python programming language "