For fun, I have been making a very basic login system in Python. I was going to look up an algorithm or something for user info storage and transfer, but then decided it would be more fun to come up with and implement it myself. What I would like to know is:Is there anyway to make it secure (i.e.-encrypting the password without revealing the algorithm in the file)?Can the algorithm I used be made any more efficient?Is it cleanly written? I tried to take into account the comments on my last question regarding simplifying code and any comments about its readability would be appreciated--by secondary goal is beautiful code.#This program will do a couple of things.#The first time it is run, it will request that you add a password. Once you do, it will change the check_e$#Every subsequent time, it will check the password in the other file and then match user input.#If user input is correct it will display a joke.#If user input is incorrect it will exit the program.#Function to help the user pick a passworddef pick_password():        file = '/home/vhx/Documents/code/pydata_test/password_dbs/pswd.txt'        print 'Please pick a password.'        password = raw_input()        target = open(file, 'w')        target.write(password)        file = '/home/vhx/Documents/code/pydata_test/password_dbs/existence_check.txt'        target = open(file, 'w')        target.write('YES')#       file.close()#Function to check the password with the password located in pswd.txtdef password_check():        file = '/home/vhx/Documents/code/pydata_test/password_dbs/pswd.txt'        pwd_check = open(file).read()        userpass = raw_input('Please input a password.\n')        if userpass == pwd_check:                print 'Password accepted!'                print 'Ready for the joke?'                raw_input()                print 'Why did the fly fly? Because the spider spied her!'        elif userpass != pwd_check:                print 'Sorry, wrong password.\n'                exit()        else:                print 'Invalid syntax.'                exit()#location of password existence check fileEC = '/home/vhx/Documents/code/pydata_test/password_dbs/existence_check.txt'PWD = '/home/vhx/Documents/code/pydata_test/password_dbs/pswd.txt' #Location of password filepswd_exist = open(EC).read() #Checking to see if the password existsif pswd_exist == 'YES':        passelse:        pick_password() #If it doesn't, user will pick a password#Checking for passwordpassword_check()Note: If you want to run it, it requires two files --pswd.txt and existence_check.txt--which you then must link (and replace all necessary file locations).If you input the correct password, the program tells a joke, though whether its funny is debatable.
Creating Original Database Algorithm--Login System
python;authentication;database
In  your function pick_password, you should close the file at the end of the file because this frees up any system resources that were being devoted to working with that file.For some reason, at the end of that function, you commented out the line f.close()You should un-comment this.In python, it is good practice to open a file using the with keyword. Using with, you can easily access the file object and the file is automatically closed at the end of the with statement.Here is how you should open the file:with open(file, w) as f:    # whatever you want to do to the file fSourceIn your code, you have three separate variables that are all set to'/home/vhx/Documents/code/pydata_test/password_dbs/pswd.txt'You should create a single global constant variable that holds this value. Here is what that would look like:PASSWORDS = '/home/vhx/Documents/code/pydata_test/password_dbs/pswd.txt'I put the variable name in all-caps because constants are usually all capital letters.In your function password_check, you make an exit call in a few places. However, you don't pass anything to it.The number you pass to an exit call is called the exit code. This number is to show how execution went when the program was running to external processes.Most commonly, an exit code of 0 means that execution was perfect, and an exit code of any non-zero number means that something went wrong.I think that you should exit your program with 1 to show that something went wrong the user was entering their password (they entered the password incorrectly).An advantage to doing it this way would be say, for example, you wrote another program and you only wanted to give access to it if the user entered the password correctly. In this program, you could read the exit code of this password program and if it's a 1, than your new program now knows that the user entered the password incorrectly.P.S. Your interpreter is probably already doing this for you, but at the top of your code you should have from sys import exit because the sys library holds the exit function that you are calling.Thanks to janos for correcting me on error exit codes.I believe that it is fairly difficult to make this more secure.The user can view the passwords fileIf you do encrypt the file, this voids the above reason but the user will always be able to see your python source.However, as BlueRaja - Danny Pflughoeft commented,-1 for impossible to make this more secure - since he's storing passwords in plain-text, I'd argue it's impossible to make this less  secure. Even if you need the passwords to be reversible (which is  usually not the case), you should be encrypting them in a way that  makes them unreadable even with the Python source (see eg. LastPass,  which encrypts all passwords using a single master password)