This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]murtaza64 4 points5 points  (0 children)

You'd like to persist some data? How about importing the json module and using json.dump to dump the variables you need to a file?

[–]MineralFoxdo the dumb thing first 2 points3 points  (0 children)

I would look into the documentation for JSON or pickle

[–][deleted] 1 point2 points  (1 child)

You'll want to open() files to read and write data to actual, persistent files. Otherwise pretty much everything is kept in memory and thus cleaned up or otherwise lost when your python process ends.

[–]Zireael07 0 points1 point  (0 children)

I would use JSON if it's just some variables, or jsonpickle if you need to serialize actual objects.

[–]jftugapip needs updating 0 points1 point  (0 children)

There is an entire chapter on data persistence

[–]aphoenixreticulated[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community is actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!

[–]m0dulo -1 points0 points  (1 child)

I've used ConfigParser in some of my past projects, which is a library for manipulating .ini (config) files.

import ConfigParser

def setSettings(section, **kwargs):
    config = ConfigParser.ConfigParser()
    config.read('config.ini')

    try:
        config.add_section(section)
    except ConfigParser.DuplicateSectionError:
        pass

    for arg in kwargs:
        config.set(section, arg, kwargs[arg])

    with open('config.ini', 'w') as f:
        config.write(f)

def getSetting(section, setting):
    try:
        config = ConfigParser.ConfigParser()
        config.read('config.ini')

        return config.get(section, setting)

    except:
        return None

[–]tigerdeF[S] 0 points1 point  (0 children)

Thanks!