Question:
Python Saving variables to a file?
Harvey
2013-06-18 10:52:13 UTC
Say I was making a little program for my Computer Science class, and I had to make something that reads usernames and passwords. Is there anyway I can make something the user inputs assigned to a variable and saved to a file, that can be read by the program so I can save the username and passwords. Its not something I have to know but I thought it would be cool and its something I might want to do some other time anyway.
If it makes any difference, I'm using python 3.2
And please dont tell me to assign the input to a variable I should put:
Variable1=input('Enter Username: ')
because that will only give me one slot to save usernames and passwords.

This might not be possible but thanks for your help if it is.
Three answers:
karljj1
2013-06-18 12:33:09 UTC
Take a look at json, it is a great way to serialise data and python has it built in:



http://docs.python.org/2/library/json.html
Russel
2013-06-19 07:48:00 UTC
It's far from impossible and there's many ways to achieve this. But for very simple personal use scripts I don't bother myself with much more than this.



def load_user_data():

...."""Return user data loaded from file or empty list"""

....try:

........with open("user_data.txt", "r") as thefile: # "r" for read access

............data_as_string = thefile.read()

............data = eval(data_as_string)

............return data

....except IOError: # happens when file not found

........return []



def save_user_data(data):

....with open("user_data.txt", "w") as thefile: # "w" for write access

........data_as_string = str(data)

........thefile.write(data_as_string)



>>> simple_user_data = [("John", "abc123$"), ("Mary", "abc123*")]

>>> save_user_data(simple_user_data)

>>> simple_user_data = load_user_data()

>>> print simple_user_data

[('John', 'abc123$'), ('Mary', 'abc123*')]

>>>



>>> name_to_password = dict([(n, p) for n, p in simple_user_data])

>>> password_to_name = dict([(p, n) for n, p in simple_user_data])

>>> save_user_data([name_to_password, password_to_name]) # note the surrounding list

>>> name_to_password, password_to_name = load_user_data()



The four lines above is just to show you when you save the data you can group them in arbitrary ways and mix different data structures however you want, because the result can still always be converted to a string and then back again with eval.



Getting user input is another question entirely. In Python 3 you use the input() builtin which you already know, and that's about it. Mix it up with some loops, functions even classes if you want. I would show you an example but it depends on the program your making otherwise "use input()" is all the answer one can give.
jplatt39
2013-06-18 19:33:33 UTC
For saving variables to files check out the file object in 7.2 of this page:



http://docs.python.org/2/tutorial/inputoutput.html



http://docs.python.org/ 2/ tutorial/ inputoutput.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...