I am having some trouble verifying the given data for 'user' against what is already in the users.txt file. An example of a line in the text file is...
('Ralph', '1a1dc91c907325c69271ddf0c944bc72')
Where 'Ralph' is what I want to compare against the users input for 'user'.


Here is my code so far:

#12/13/10 12:46 AM
#attempt to add new users to a users file
import md5

file = open('./users/users.txt', 'r')

user = raw_input("Please enter your username: ")

while line:
        line = file.readline()
        if line == user:
                print 'error'

file.close()

passwort = raw_input("Enter your password: ")

#MD5 hashing password and writing
passwort = md5.md5(passwort).hexdigest()
info = user, passwort
info = str(info)

#writing the user's data
#a flag means to append (if it exists)
file = open('./users/users.txt', 'a')
file.write(info)
file.write('\n')

print line would return ('Ralph', '1a1dc91c907325c69271ddf0c944bc72')
but print line[0] prints '('

If somebody wouldn't mind and help me with this simple problem because I'm sure I'm missing something simple. Thanks for your time.

Updated and fixed code. Still pretty sloppy.

#12/14/10 1:53 PM
#making a function.. and sleep

import time, hashlib

def userCreation(): 
        user = raw_input("Please enter your username: ")
        user = user.lower()
        file = open('./users/users.txt', 'r')
        line = file.readline()
        while line:
                takenUser = line.split(',')[0][2:-1]
                takenUser = takenUser.lower()
                line = file.readline()
                if user == takenUser:
                        print '\nTaken username please try again!\n'
                        time.sleep(2)
                        userCreation()


        passwort = raw_input("Enter your password: ")

        #hashing password and writing
        passwort = hashlib.sha224(passwort).hexdigest()
        info = user, passwort
        info = str(info)

        #writing the user's data
        #a flag means to append (if it exists)
        file = open('./users/users.txt', 'a')
        file.write(info)
        file.write('\n')

userCreation()

Here my version, I used though more simple format of username,password and standard lowercase Python function names:

import time
import os
import hashlib

FILENAME = 'users/users.txt'


def user_creation():
    while True:
        try:
            userfile = open(FILENAME, 'r')
        except IOError:
            # create file if it does not exist
            path = os.path.dirname(os.path.abspath(FILENAME))
            try:
                # create directories which do not exist
                os.makedirs(path)
            except:
                # directory existed
                pass
            userfile = open(FILENAME, 'w')
        else:
            user = raw_input("Please enter your username: ").lower()
            # all usernames are prepared in lowercase at input
            if "%s," % user in userfile.read():
                print 'Taken password, try another username'
            else:
                passwort = raw_input("Enter your password: ")
                #hashing password and writing
                passwort = hashlib.sha224(passwort).hexdigest()
                open(FILENAME, 'a').write("%s,%s\n" % (user, passwort))
                # success, return
                return 


user_creation()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.