Okay, I'm doing some experimenting on file I/O using a user/password access program I'm writing. My problem right now is while the program will append the new user and password, it does so whether or not that user name exists already. I'm not worried about duplicate passwords, but I don't want duplicate usernames. My question is, when appending the new username to a data file, how do I check to see if the username variable matches any existing usernames on the file already?

I wouldn't think my code need be posted on this, but if it is, let me know.

Thanks in advance for any help you may lend, I've searched for this, but maybe I'm not using the right terminology to find anything useful.

If your file is not too large, you can go as simple as this:

newuser = "hamster201"

fin = open("MyUserFile.txt", "r")
text = fin.read()
fin.close()

if newuser in text:
    print "this username already used"

Okay still having problems. I forgot to mention that I'm using cPickle to store the data. How would I attempt this using cPickle, cause using the method above gives me the error:

TypeError: 'str' object does not support item assignment

Just for good measure, here's what I'm working with:

#User Access for Password.py
import cPickle as p

fload = open('systemaccess.txt', 'a+')
temp = fload.read()

print 'Welcome to the System Account Setup.'
Username = raw_input('Please enter the username you desire: ')
Password = raw_input('Enter your desired password: ')

if Username in temp:
    print 'That username already taken.'
else:
    temp[Username] = Password

Just a note, I wrote a separate program to store the initial use/pass file.

import cPickle as p

userpassfile = 'systemaccess.txt'
userpass = {'Andrew' : 'potato', 'Heather' : 'tomato', 'Peggy' : '0598', 'Winston' : 'potato0598'}
f = file(userpassfile, 'a')
p.dump(userpass, f)
f.close()

That gives me the error

Traceback (most recent call last):
File "C:\Python25\Python\UserAccess.py", line 5, in <module>
temp = p.load('systemaccess.txt')
TypeError: argument must have 'read' and 'readline' attributes

This question has already been answered. Start a new discussion instead.