0

I am new to Python and relatively new to coding in general, I was just trying to write a test bit of code to see if a list could be appended, and then when that .py is reloaded the changes are saved into it?

I'm not sure if this is possible with lists without a decent amount more work, but none of my attempts have managed to get me there.

The test code I have at the moment is:

lDUn = []
lDPw = []

def enterUn():
    usrName = input(str("Enter your desired username:\n"))
    if usrName in lDUn:
        print ("Username is already in use, please try again")
        enterUn()
    else:
        print ("Username created!")
        lDUn.append(usrName)

enterUn()

def enterPw():
    pW1 = input(str("Enter your desired password:\n"))
    pW2 = input(str("please re-enter your password:\n"))
    if pW1 == pW2:
        print ("Password created!")
        lDPw.append(pW1)       
    else:
        print ("Passwords do not match, please try again")
        enterPw()

enterPw()

When lDUn and lDPw are checked for inclusion later on they register no problem, but when the program is closed and re-opened later they are gone. I have tried making it writing the 'pW' and 'usrName' inputs into a .txt but have failed to get it to search those .txts for the specific strings later on. (but that is for a different question) Other than that I'm not sure what to do with my current knowledge of Python.

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
Ben Wordy
  • 49
  • 1
  • 9
  • Variables in Python (and most languages if not all) are volatile. You have to store the values somewhere, such as a text file or a proper database, and reload them when necessary if you want them to be persistent. – Selcuk Apr 04 '16 at 09:26
  • @Selcuk thank you for the speedy response, it looks like I shall have to try again at getting the program to search a .txt for the necessary strings. I have seen a lot of ways to do it on here but haven't managed to get any of them to work as yet. – Ben Wordy Apr 04 '16 at 09:28
  • @Selcuk MUMPS has superior arrays, they persist! – Ilja Everilä Apr 04 '16 at 09:29
  • @Ilja Good to learn something new. That's why I wrote "most languages" :) – Selcuk Apr 04 '16 at 09:32
  • @BenWordy You don't have to search in txt files in your case since you can load the whole file into memory (a list structure) and use standard Python methods instead. Read more about Python `csv` module and you are good to go. You can also use `cPickle` as in the answer to [this question](http://stackoverflow.com/questions/890485/python-how-do-i-write-a-list-to-file-and-then-pull-it-back-into-memory-dict-re) – Selcuk Apr 04 '16 at 09:32
  • @Selcuk thanks for that! csv looks like it should help me, I'll check out cPickle as well – Ben Wordy Apr 04 '16 at 09:50

2 Answers2

0

The values of variables (in this case, the contents of your lists) are never automatically saved when you restart the program. You would need to write some code that saves the values to a file before the program exits, and also some code that reads the values in from the file when the program starts up again.

It's up to you just how you convert the lists into bytes that you can put in a file. One common way would be to just write the strings from the lists into the file, one per line, and then read them again. You can do this using the methods of file objects.

Another option is to use the pickle module. This may save you a bit of effort, since you just give it a Python object and it handles the conversion into bytes for you, but you won't be able to view and edit the resulting file. (Well, you can, but it will look like gibberish since it's a binary file.)

David Z
  • 128,184
  • 27
  • 255
  • 279
  • thank you for the response, I will try out the 'pickle' module, it definitley looks useful. I tried using 'f.read()' but I think I got the method wrong when trying to check for inclusion. I will play around with both of them and hopefully get them properly figured out soon enough. – Ben Wordy Apr 04 '16 at 09:44
  • Thanks again for your answer, I looked back over usage of 'read' and managed to get it to work, I will try out pickle as well since it looks like it could be more useful in some circumstances. – Ben Wordy Apr 04 '16 at 10:23
0

Here's basically what you do:

  1. You set a file or files where you will store those lists.
  2. At the begining of your code you load the data from files into lists.
  3. Each time you modify one of the lists you also modify the related file (the most basic implementation would be to recreate the file).
  4. You work with in-memory data in other cases.

That will work fine as long as you deal with small files and small "traffic". If any of those increases then you will have serious peformance and/or consistency issues. To properly solve these problems you should use a proper database, starting with sqlite (which doesn't require a separate server).

freakish
  • 54,167
  • 9
  • 132
  • 169