2

Simple questions for those who know, pretty hard for me, as I think that it is not pratically possible.

After making a simple python program, it is possible to run it on the computer's command prompt.

I was wondering or it is possible to allow someone who runs it that way, to add an element to a list (list.insert) and have it still there the next time the program is run?(thus editting the predefined list to a new list and saving it that way)

EDIT: just giving a bit more information:

All the program has to do is allow you to choose a list. From this list it returns a random item. I was just hoping to allow to add items to this list while running the program, keeping the list updated afterwards.

2
  • 1
    It's certainly possible for a program to modify its own source on disk, but it's not necessarily a good idea. Separate the data from the code if you need to be able to edit the data. Commented Feb 25, 2013 at 13:30
  • Would that allow me to do what I want(see edit). Where can I learn how to do it? Commented Feb 25, 2013 at 14:03

1 Answer 1

6

The most basic way is to use the Pickle module to save and load your data to disk:

http://docs.python.org/2/library/pickle.html

http://docs.python.org/2/library/pickle.html#example

Here's how I would use it in a simple program

try:
    import cPickle as pickle
except ImportError:
    import pickle

class MyClass(object):

    def __init__(self, file_name):
        self.array = []
        self.file_name = file_name
        self.load_data()

    def add_element(self, element):
        self.array.append(element)
        self.save_data()

    def load_data(self):
        try:
            with open(self.file_name, "r") as f:
                self.array = pickle.load(f)
        except IOError:
            pass

    def save_data(self):
        with open(self.file_name, "w") as f:
            pickle.dump(self.array, f)

def main():
    FILE_NAME = "test.pkl"
    a = MyClass(FILE_NAME)
    print "elements in array are", a.array
    for i in range(5):
        a.add_element(i)

if __name__ == "__main__":
    main()
Sign up to request clarification or add additional context in comments.

9 Comments

Mr. E is right. Pickling is probably the easiest way to do this. But you will need to decide on how often you are reloading the data from disk. It won't be as easy as loading it once, and then it'll be constantly updated. You will need to regularly save it to disk, and reload it from disk as needed.
aren't there several security issues with the pickle module?
interesting. Yet not really understanding it. And interested in the answer to Woot4Moo too. Thanks though!
I'll give you some example code in an hour - have a meeting just now. Maybe someone else will reply in the meantime
That is very nice of you! I'll be awaiting it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.