User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2008
Posts: 16
Reputation: Andymoore88 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Andymoore88 Andymoore88 is offline Offline
Newbie Poster

File Appendage, check for duplicate first

  #1  
Oct 11th, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Posts: 1,669
Reputation: sneekula is on a distinguished road 
Rep Power: 6
Solved Threads: 47
sneekula's Avatar
sneekula sneekula is offline Offline
Posting Virtuoso

Re: File Appendage, check for duplicate first

  #2  
Oct 11th, 2008
If your file is not too large, you can go as simple as this:
  1. newuser = "hamster201"
  2.  
  3. fin = open("MyUserFile.txt", "r")
  4. text = fin.read()
  5. fin.close()
  6.  
  7. if newuser in text:
  8. print "this username already used"
No one died when Clinton lied.
Reply With Quote  
Join Date: Oct 2008
Posts: 16
Reputation: Andymoore88 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Andymoore88 Andymoore88 is offline Offline
Newbie Poster

Re: File Appendage, check for duplicate first

  #3  
Oct 11th, 2008
Wonderful, thank you. I'll see if this works.
Reply With Quote  
Join Date: Oct 2008
Posts: 16
Reputation: Andymoore88 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Andymoore88 Andymoore88 is offline Offline
Newbie Poster

Re: File Appendage, check for duplicate first

  #4  
Oct 11th, 2008
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()
Last edited by Andymoore88 : Oct 11th, 2008 at 2:33 pm.
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 322
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 49
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz

Re: File Appendage, check for duplicate first

  #5  
Oct 11th, 2008
  1. 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. **
Reply With Quote  
Join Date: Oct 2008
Posts: 16
Reputation: Andymoore88 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Andymoore88 Andymoore88 is offline Offline
Newbie Poster

Re: File Appendage, check for duplicate first

  #6  
Oct 11th, 2008
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
Reply With Quote  
Join Date: Oct 2008
Posts: 16
Reputation: Andymoore88 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Andymoore88 Andymoore88 is offline Offline
Newbie Poster

Re: File Appendage, check for duplicate first

  #7  
Oct 11th, 2008
Okay, fixed that with:

temp = p.load(open('systemaccess.txt'))
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 7:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC