Search code examples
pythonpreferences

best practice for user preferences in $HOME in Python


For some small programs in Python, I would like to set, store and retrieve user preferences in a file in a portable (multi-platform) way.

I am thinking about a very simple ConfigParser file like "~/.program" or "~/.program/program.cfg".

Is os.path.expanduser() the best way for achieving this or is there something more easy/straightforward?


Solution

  • os.path.expanduser("~")
    

    is more portable than

    os.environ['HOME']
    

    so it should be ok to use the first.