•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 457,727 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,844 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 229 | Replies: 6 | Solved
![]() |
•
•
Join Date: Oct 2008
Posts: 16
Reputation:
Rep Power: 1
Solved Threads: 1
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.
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:
python Syntax (Toggle Plain Text)
newuser = "hamster201" fin = open("MyUserFile.txt", "r") text = fin.read() fin.close() if newuser in text: print "this username already used"
No one died when Clinton lied.
•
•
Join Date: Oct 2008
Posts: 16
Reputation:
Rep Power: 1
Solved Threads: 1
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:
Just a note, I wrote a separate program to store the initial use/pass file.
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()
Last edited by Andymoore88 : Oct 11th, 2008 at 2:33 pm.
python Syntax (Toggle Plain Text)
temp = p.load('systemaccess.txt')
Last edited by jlm699 : Oct 11th, 2008 at 3:04 pm.
Let's Go Pens!
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Python Forum
- Previous Thread: Idle, wxPython, Tkinter, Widget toolkit, PyGTK, PyQT, Tk GUI?
- Next Thread: 'write' attribute using 'a+'



Linear Mode