Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 15th, 2013, 01:39 PM
doctorly doctorly is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 doctorly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 31 sec
Reputation Power: 0
Need help writing to an external dictionary

def register(users):
unr = raw_input('Desired Username: ')
if users.has_key(unr):
print 'Sorry, username is already taken'
else:
pr = raw_input('Enter Password: ')
users[unr] = pr

from usernames import users

register(users)

I want to write the new username and password to the dictionary permanently, not just in the memory. So that the file will contain that information the next time the user tries to log in.

I know that its insecure, but it is just for practice, how to write to the dictionary and external files. any help would be awesome!

Reply With Quote
  #2  
Old January 15th, 2013, 06:38 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
You could probably use memory mapped files but the usual answer would be "investigate pickle and shelve modules."

http://docs.python.org/3/library
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old January 15th, 2013, 09:16 PM
el_zorro el_zorro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 el_zorro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 47 sec
Reputation Power: 0
This probably isn't what you wanted but you could write to a file.

import ast

users = {} # initialise users dict which initialise will update

def writeToFile(): #call this whenever you change the dictionary
userFile = open('users.txt', 'w')
userFile.write(str(users))

def initialise(): #initialise dictionary from last time
usersFile = open('users.txt', 'r')
userDict = ast.literal_eval(usersFile.read())
for user in userDict: #you can edit globals within functions
users[user] = userDict[user]

Reply With Quote
  #4  
Old January 15th, 2013, 09:17 PM
el_zorro el_zorro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 el_zorro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 47 sec
Reputation Power: 0
probably also want to file.close() after each of those methods, and could put a try except around if you want too..

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need help writing to an external dictionary

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap